**BUSTED** How to speed up a byte[] lookup to be faster using sun.misc.Unsafe?

后端 未结 3 1194
醉话见心
醉话见心 2021-01-12 04:43

I am experimenting with Unsafe to iterate over memory instead of iterating over the values in a byte[]. A memory block is allocated using unsafe. The memory is sufficient to

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-12 05:04

    I thought Unsafe could access the memory faster than using a regular array access with the index check it does for each index...

    One possible reason why the range checking might not be a factor is the JIT compiler's optimizer. Since the array's size never changes, it may be possible for the optimizer to "hoist" all of the range checking and perform it once at the start of the loop.

    By contrast, the JIT compiler might be unable to optimize (e.g. inline) the Unsafe.getByte() call. Or maybe the getByte method has a read barrier ...)

    However this is speculation. The way to be sure is to get the JVM to dump out the JIT-compiled native code for the two cases, and compare them instruction by instruction.

提交回复
热议问题