From 46cdabb9a2cc7da94c00e7daf44e2922ecec389e Mon Sep 17 00:00:00 2001 From: Mattias Persson Date: Wed, 9 Mar 2016 00:01:59 +0100 Subject: [PATCH] Cheaper ShortArray#calculateNumberOfBlocksUsed by not using Bits, but instead simple masking from the header --- .../neo4j/kernel/impl/store/ShortArray.java | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/ShortArray.java b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/ShortArray.java index ac60363af905a..b5ec08ef420e0 100644 --- a/community/kernel/src/main/java/org/neo4j/kernel/impl/store/ShortArray.java +++ b/community/kernel/src/main/java/org/neo4j/kernel/impl/store/ShortArray.java @@ -629,11 +629,12 @@ private static boolean isPrimitive( Object array ) return array.getClass().getComponentType().isPrimitive(); } - private static final Map, ShortArray> all = new IdentityHashMap<>( values().length * 2 ); + private static final ShortArray[] TYPES = ShortArray.values(); + private static final Map, ShortArray> all = new IdentityHashMap<>( TYPES.length * 2 ); static { - for ( ShortArray shortArray : values() ) + for ( ShortArray shortArray : TYPES ) { all.put( shortArray.primitiveClass, shortArray ); all.put( shortArray.boxedClass, shortArray ); @@ -772,7 +773,7 @@ public int getRequiredBits( long value ) public static ShortArray typeOf( byte typeId ) { - return ShortArray.values()[typeId - 1]; + return TYPES[typeId-1]; } public static ShortArray typeOf( Object array ) @@ -782,13 +783,14 @@ public static ShortArray typeOf( Object array ) public static int calculateNumberOfBlocksUsed( long firstBlock ) { - Bits bits = Bits.bitsFromLongs( new long[] {firstBlock} ); - // bbbb][bbll,llll][yyyy,tttt][kkkk,kkkk][kkkk,kkkk][kkkk,kkkk] - bits.getInt( 24 ); // Get rid of key - bits.getByte( 4 ); // Get rid of short array type - bits.getByte( 4 ); // Get rid of the type - int arrayLength = bits.getByte( 6 ); - int requiredBits = bits.getByte( 6 ); + // inside the high 4B of the first block of a short array sits the header + int highInt = (int) (firstBlock >>> 32); + // bits 32-37 contains number of items (length) + int arrayLength = highInt & 0b11_1111; + highInt >>>= 6; + // bits 38-43 contains number of requires bits per item + int requiredBits = highInt & 0b11_1111; + // no values can be represented by 0 bits, so we use that value for 64 instead if ( requiredBits == 0 ) { requiredBits = 64;