Helpers for MethodHandles.byteArrayViewVarHandle VarHandle
Paul Sandoz
paul.sandoz at oracle.com
Wed May 18 15:40:38 UTC 2016
> On 18 May 2016, at 16:50, Jaroslav Kameník <jaroslav at kamenik.cz> wrote:
>
> Hi,
>
> thank you for response!
>
> I would prefer not to spread such functionality beyond that of ByteBuffer and the VarHandle producing methods on MethodHandles. It’s an advanced feature so think ok if less visible.
>
>
> On the other side, this would be easier to use and understand than ByteBuffer in simple cases, I think.
>
> I have looked into JVM sources briefly, and this functionality is implemented often there, both inlined and methods as intAt/readLong/...
> So it could be helpful even as private api.
There certainly is in nio buffer sources (and may be for encoding/decoding in other areas, i am speculating as i have not looked), but for buffer sources we can actually remove much if not all of Bits.java via use of the internal Unsafe unaligned accessors.
>
>
> Separately, i may revisit the signatures for access to also include an alignment-offset argument (which is expected to be constant when looping) in addition to the index (e.g. byte-index = ao + (i << LOG_2_UNIT_SIZE)). Thus bringing alignment into the foreground where it is harder to forget about or ignore.
>
> It is nice idea. So it could look like:
>
> getLongFrom[LE/BE](byte[] data, int firstByteIndex) - for direct access
> getLongFrom[LE/BE](byte[] data, int firstByteOffset, int longArrayIndex) - for loops/shifted array access
>
> etc.
I was proposing just the latter, and just for VarHandles :-) it’s not just for loops but also to bring alignment issues to the foreground. (It also it fits in with something we have been pondering about for the unsafe unaligned accessors on systems that don’t support unaligned access, such as SPARC).
>
> I'd like to ask about performance - how does wrapping of VH to another call affect optimization to simple memory load?
>
Same as that of a MethodHandle getter/setter to a field. The compiler should strip away the all the “ceremony” and inline the access as if Unsafe was used directly (but with safety checks in place most of which are optimized away, although hard to fully elide array bounds checks in certain cases, but we try our best!)
Paul.
More information about the core-libs-dev
mailing list