oracle / truffleruby

A high performance implementation of the Ruby programming language, built on GraalVM.
https://www.graalvm.org/ruby/
Other
2.99k stars 180 forks source link

FFI::Pointer#order not implemented #2054

Open chrisseaton opened 3 years ago

chrisseaton commented 3 years ago

Implemented in https://github.com/ffi/ffi/commit/f8cfe29f2e4e1968651b165648886722173719c6.

eregon commented 3 years ago

There doesn't seem to be any spec for this in FFI. It would be helpful if some are added.

I guess you want to use the network endian (aka big endian), right? (https://github.com/ffi/ffi/issues/813)

I wonder what's an efficient way to implement that. Maybe a subclass of FFI::Pointer which swaps the resulting bytes (since order returns a new pointer)?

chrisseaton commented 3 years ago

Yes network order - specifically reading a BGV file.

I'll write specs, look at how it's implemented in MRI, and get back to you.

eregon commented 3 years ago

https://github.com/ffi/ffi/commit/f8cfe29f2e4e1968651b165648886722173719c6#diff-682df063515101c03cfa458badf35dbcR49 is how it's implemented in that commit you linked. So checking everytime if the flag if set, and if so use some bswap function to swap the bytes.

Seems some overhead for non-swapped cases, so I think the subclass is better if compatible enough.

chrisseaton commented 3 years ago

Specifying upstream first in order to clarify the behaviour https://github.com/ffi/ffi/pull/830.

chrisseaton commented 3 years ago

I'm waiting for a release of ffi to update for it.

eregon commented 3 years ago

I'll update to the latest FFI specs, I think we don't need a FFI release for that.

eregon commented 3 years ago

@chrisseaton Specs updated in b06882f97697269ca218b6cba86f579f6a74df8d, that includes the order specs. FWIW, I noticed there is things like Long.reverseBytes() in Java which might be useful. pack/unpack1 can also be useful to reverse, but seems much more heavy.

I'd recommend returning an instance of a FFI::Pointer subclass with reversed access, much like https://github.com/oracle/graal/blob/4bbaeaaa8d0b58134040127e416bf57419b6c09c/truffle/src/com.oracle.truffle.api/src/com/oracle/truffle/api/memory/ReversedByteArraySupport.java, so we don't slow down the common case of accessing in native order.