sun.nio.ch.DirectBuffer and jdk9/jigsaw

Vitaly Davidovich vitalyd at gmail.com
Thu Feb 23 18:04:05 UTC 2017


On Thu, Feb 23, 2017 at 12:45 PM, Uwe Schindler <uschindler at apache.org>
wrote:

> Hi,
>
>
>
> Why do you need the address at all in the Java code? Java code can use
> the official ByteBuffer methods to access the memory you are wrapping. In
> Java 9 that’s optimized very good by Hotspot and should be almost as fast
> as array accesses (we proved that in Apache Lucene - congrats to the
> Hotspot committers). If you need special access modes like volatile access,
> then you can use Java 9's VarHandles. You can get a VarHandle to the
> backing direct buffer using the MethodHandles API.
>
> I mentioned this upthread - the base address is used for index
> calculations to read/write data using Unsafe directly.  I don't know about
> Java 9 as I've not tried it yet, but the generated assembly for using BB
> methods vs Unsafe did not favor BB.  There are also various safety checks
> in DBB/Buffer internals that won't get optimized away.
>
>
>
> Also, my general experience with looking at C2 optimizations has led me to
> the conclusion that the optimizations are "unstable" - innocent code
> changes, differences in order of how classes are loaded, differences in
> what callsites trigger compilation first, and a bunch of other otherwise
> benign things can interfere with inlining decisions, which is typically the
> reason things go sideways in terms of optimization.
>
>
>
> As for MethodHandles and VarHandles, that's one possibility I'm
> considering as a way to migrate off using DirectBuffer (although I'd still
> like to know if there's any plan to standardize/formalize some notion of a
> direct buffer).  However, my understanding is that using MH will still
> require me to crack into jdk code (to get access to the DBB and friends)
> and thus require addExports.  DirectBuffer is still accessible if using
> addExports, but it's a wrinkle I was hoping to iron out, hence what started
> this email chain.
>
>
>
> with VarHandles you can directly access the underlying native memory. You
> can get a ByteBufferView varhandle - no cracks and Unsafe needed:
>
> <http://download.java.net/java/jdk9/docs/api/java/lang/
> invoke/MethodHandles.html#byteBufferViewVarHandle-java.
> lang.Class-java.nio.ByteOrder->
>
Thanks Uwe - I didn't realize there was a special case for BB in VH; I'll
need to play with it.  Is access to a DBB via VH supposed to be faster than
using the BB API itself? I vaguely recall seeing some email(s) on one of
the openjdk lists about VH not matching Unsafe in terms of generated code
quality, and VH is supposed to be the successor/replacement for Unsafe
(which we also use, as mentioned).

>
>
> Uwe
>


More information about the jigsaw-dev mailing list