Why the bounds check doesn't get eliminated?

后端 未结 3 1510
忘掉有多难
忘掉有多难 2020-12-06 00:01

I wrote a simple benchmark in order to find out if bounds check can be eliminated when the array gets computed via bitwise and. This is basically what nearly all hash tables

3条回答
  •  佛祖请我去吃肉
    2020-12-06 00:43

    1. No, this is evidently an effect of not enough smart bounds check elimination.

    I've extended a benchmark by Marko Topolnik:

    @OutputTimeUnit(TimeUnit.NANOSECONDS)
    @BenchmarkMode(Mode.AverageTime)
    @OperationsPerInvocation(BCElimination.N)
    @Warmup(iterations = 5, time = 1)
    @Measurement(iterations = 10, time = 1)
    @State(Scope.Thread)
    @Threads(1)
    @Fork(2)
    public class BCElimination {
        public static final int N = 1024;
        private static final Unsafe U;
        private static final long INT_BASE;
        private static final long INT_SCALE;
        static {
            try {
                Field f = Unsafe.class.getDeclaredField("theUnsafe");
                f.setAccessible(true);
                U = (Unsafe) f.get(null);
            } catch (Exception e) {
                throw new IllegalStateException(e);
            }
    
            INT_BASE = U.arrayBaseOffset(int[].class);
            INT_SCALE = U.arrayIndexScale(int[].class);
        }
    
        private final int[] table = new int[BCElimination.N];
    
        @Setup public void setUp() {
            final Random random = new Random();
            for (int i=0; i

    Results:

    Benchmark                                Mean   Mean error    Units
    BCElimination.maskedIndex               1,235        0,004    ns/op
    BCElimination.maskedIndexUnsafe         1,092        0,007    ns/op
    BCElimination.normalIndex               1,071        0,008    ns/op
    


    2. The second question is for hotspot-dev mailing lists rather than StackOverflow, IMHO.

提交回复
热议问题