use of Unsafe for ASCII detection

Aleksey Shipilev aleksey.shipilev at oracle.com
Tue Jan 12 20:49:29 UTC 2016


On 01/07/2016 02:21 AM, Martin Buchholz wrote:
> A stupid benchmark (sorry Aleksey - maybe you already have a jmh 
> version of this?) http://cr.openjdk.java.net/~martin/Utf8Bench.java

Okay, I'll bite:
 http://cr.openjdk.java.net/~shade/scratch/UTF8Scan.java

As Andrew already pointed out, the trick is to get the ByteBuffer byte
order to native, and use JDK 9 with getLongUnaligned bells and whistles.
As Paul suggested, VarHandles views could be used to always avoid
ByteBuffer allocation, even though compilers are moderately successful
in removing them.

> It's hard to resist Unsafe when it gives 3x improvement in a
> performance hotspot. Maybe C2 can optimize the simple loop even more
> than it is today?

It would seem that with JDK 9, you don't need Unsafe in this scenario.
ByteBuffers provide a comparable performance, and VarHandles would push
the envelope a bit further, since we are implicitly optimizing the
internal APIs to boost them up.

Thanks,
-Aleksey




More information about the core-libs-dev mailing list