diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index aea1bacf45b..3517883cd78 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -188,6 +188,9 @@ Optimizations * LUCENE-5841: Improve performance of block tree terms dictionary when assigning terms to blocks. (Mike McCandless) +* LUCENE-5856: Optimize Fixed/Open/LongBitSet to remove unnecessary AND. + (Robert Muir) + Bug Fixes * LUCENE-5796: Fixes the Scorer.getChildren() method for two combinations diff --git a/lucene/core/src/java/org/apache/lucene/util/FixedBitSet.java b/lucene/core/src/java/org/apache/lucene/util/FixedBitSet.java index 72649746244..cf69d3efe12 100644 --- a/lucene/core/src/java/org/apache/lucene/util/FixedBitSet.java +++ b/lucene/core/src/java/org/apache/lucene/util/FixedBitSet.java @@ -63,8 +63,7 @@ public final class FixedBitSet extends DocIdSet implements Bits { return doc = NO_MORE_DOCS; } int i = doc >> 6; - final int subIndex = doc & 0x3f; // index within the word - long word = bits[i] >> subIndex; // skip all the bits to the right of index + long word = bits[i] >> doc; // skip all the bits to the right of index if (word != 0) { return doc = doc + Long.numberOfTrailingZeros(word); @@ -96,8 +95,7 @@ public final class FixedBitSet extends DocIdSet implements Bits { return doc = NO_MORE_DOCS; } int i = target >> 6; - final int subIndex = target & 0x3f; // index within the word - long word = bits[i] >> subIndex; // skip all the bits to the right of index + long word = bits[i] >> target; // skip all the bits to the right of index if (word != 0) { return doc = target + Long.numberOfTrailingZeros(word); @@ -243,24 +241,21 @@ public final class FixedBitSet extends DocIdSet implements Bits { int i = index >> 6; // div 64 // signed shift will keep a negative index and force an // array-index-out-of-bounds-exception, removing the need for an explicit check. - int bit = index & 0x3f; // mod 64 - long bitmask = 1L << bit; + long bitmask = 1L << index; return (bits[i] & bitmask) != 0; } public void set(int index) { assert index >= 0 && index < numBits: "index=" + index + ", numBits=" + numBits; int wordNum = index >> 6; // div 64 - int bit = index & 0x3f; // mod 64 - long bitmask = 1L << bit; + long bitmask = 1L << index; bits[wordNum] |= bitmask; } public boolean getAndSet(int index) { assert index >= 0 && index < numBits; int wordNum = index >> 6; // div 64 - int bit = index & 0x3f; // mod 64 - long bitmask = 1L << bit; + long bitmask = 1L << index; boolean val = (bits[wordNum] & bitmask) != 0; bits[wordNum] |= bitmask; return val; @@ -269,16 +264,14 @@ public final class FixedBitSet extends DocIdSet implements Bits { public void clear(int index) { assert index >= 0 && index < numBits; int wordNum = index >> 6; - int bit = index & 0x03f; - long bitmask = 1L << bit; + long bitmask = 1L << index; bits[wordNum] &= ~bitmask; } public boolean getAndClear(int index) { assert index >= 0 && index < numBits; int wordNum = index >> 6; // div 64 - int bit = index & 0x3f; // mod 64 - long bitmask = 1L << bit; + long bitmask = 1L << index; boolean val = (bits[wordNum] & bitmask) != 0; bits[wordNum] &= ~bitmask; return val; @@ -290,8 +283,7 @@ public final class FixedBitSet extends DocIdSet implements Bits { public int nextSetBit(int index) { assert index >= 0 && index < numBits : "index=" + index + ", numBits=" + numBits; int i = index >> 6; - final int subIndex = index & 0x3f; // index within the word - long word = bits[i] >> subIndex; // skip all the bits to the right of index + long word = bits[i] >> index; // skip all the bits to the right of index if (word!=0) { return index + Long.numberOfTrailingZeros(word); diff --git a/lucene/core/src/java/org/apache/lucene/util/LongBitSet.java b/lucene/core/src/java/org/apache/lucene/util/LongBitSet.java index 16aa8c5c38b..175ccf5a691 100644 --- a/lucene/core/src/java/org/apache/lucene/util/LongBitSet.java +++ b/lucene/core/src/java/org/apache/lucene/util/LongBitSet.java @@ -101,24 +101,21 @@ public final class LongBitSet { int i = (int) (index >> 6); // div 64 // signed shift will keep a negative index and force an // array-index-out-of-bounds-exception, removing the need for an explicit check. - int bit = (int) (index & 0x3f); // mod 64 - long bitmask = 1L << bit; + long bitmask = 1L << index; return (bits[i] & bitmask) != 0; } public void set(long index) { assert index >= 0 && index < numBits: "index=" + index + " numBits=" + numBits; int wordNum = (int) (index >> 6); // div 64 - int bit = (int) (index & 0x3f); // mod 64 - long bitmask = 1L << bit; + long bitmask = 1L << index; bits[wordNum] |= bitmask; } public boolean getAndSet(long index) { assert index >= 0 && index < numBits; int wordNum = (int) (index >> 6); // div 64 - int bit = (int) (index & 0x3f); // mod 64 - long bitmask = 1L << bit; + long bitmask = 1L << index; boolean val = (bits[wordNum] & bitmask) != 0; bits[wordNum] |= bitmask; return val; @@ -127,16 +124,14 @@ public final class LongBitSet { public void clear(long index) { assert index >= 0 && index < numBits; int wordNum = (int) (index >> 6); - int bit = (int) (index & 0x03f); - long bitmask = 1L << bit; + long bitmask = 1L << index; bits[wordNum] &= ~bitmask; } public boolean getAndClear(long index) { assert index >= 0 && index < numBits; int wordNum = (int) (index >> 6); // div 64 - int bit = (int) (index & 0x3f); // mod 64 - long bitmask = 1L << bit; + long bitmask = 1L << index; boolean val = (bits[wordNum] & bitmask) != 0; bits[wordNum] &= ~bitmask; return val; @@ -148,8 +143,7 @@ public final class LongBitSet { public long nextSetBit(long index) { assert index >= 0 && index < numBits; int i = (int) (index >> 6); - final int subIndex = (int) (index & 0x3f); // index within the word - long word = bits[i] >> subIndex; // skip all the bits to the right of index + long word = bits[i] >> index; // skip all the bits to the right of index if (word!=0) { return index + Long.numberOfTrailingZeros(word); diff --git a/lucene/core/src/java/org/apache/lucene/util/OpenBitSet.java b/lucene/core/src/java/org/apache/lucene/util/OpenBitSet.java index 153bcb2a3f0..6874a736a2e 100644 --- a/lucene/core/src/java/org/apache/lucene/util/OpenBitSet.java +++ b/lucene/core/src/java/org/apache/lucene/util/OpenBitSet.java @@ -172,8 +172,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable { // array-index-out-of-bounds-exception, removing the need for an explicit check. if (i>=bits.length) return false; - int bit = index & 0x3f; // mod 64 - long bitmask = 1L << bit; + long bitmask = 1L << index; return (bits[i] & bitmask) != 0; } @@ -186,8 +185,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable { int i = index >> 6; // div 64 // signed shift will keep a negative index and force an // array-index-out-of-bounds-exception, removing the need for an explicit check. - int bit = index & 0x3f; // mod 64 - long bitmask = 1L << bit; + long bitmask = 1L << index; return (bits[i] & bitmask) != 0; } @@ -198,8 +196,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable { public boolean get(long index) { int i = (int)(index >> 6); // div 64 if (i>=bits.length) return false; - int bit = (int)index & 0x3f; // mod 64 - long bitmask = 1L << bit; + long bitmask = 1L << index; return (bits[i] & bitmask) != 0; } @@ -209,8 +206,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable { public boolean fastGet(long index) { assert index >= 0 && index < numBits; int i = (int)(index >> 6); // div 64 - int bit = (int)index & 0x3f; // mod 64 - long bitmask = 1L << bit; + long bitmask = 1L << index; return (bits[i] & bitmask) != 0; } @@ -233,8 +229,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable { public int getBit(int index) { assert index >= 0 && index < numBits; int i = index >> 6; // div 64 - int bit = index & 0x3f; // mod 64 - return ((int)(bits[i]>>>bit)) & 0x01; + return ((int)(bits[i]>>>index)) & 0x01; } @@ -250,8 +245,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable { /** sets a bit, expanding the set size if necessary */ public void set(long index) { int wordNum = expandingWordNum(index); - int bit = (int)index & 0x3f; - long bitmask = 1L << bit; + long bitmask = 1L << index; bits[wordNum] |= bitmask; } @@ -262,8 +256,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable { public void fastSet(int index) { assert index >= 0 && index < numBits; int wordNum = index >> 6; // div 64 - int bit = index & 0x3f; // mod 64 - long bitmask = 1L << bit; + long bitmask = 1L << index; bits[wordNum] |= bitmask; } @@ -273,8 +266,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable { public void fastSet(long index) { assert index >= 0 && index < numBits; int wordNum = (int)(index >> 6); - int bit = (int)index & 0x3f; - long bitmask = 1L << bit; + long bitmask = 1L << index; bits[wordNum] |= bitmask; } @@ -319,8 +311,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable { public void fastClear(int index) { assert index >= 0 && index < numBits; int wordNum = index >> 6; - int bit = index & 0x03f; - long bitmask = 1L << bit; + long bitmask = 1L << index; bits[wordNum] &= ~bitmask; // hmmm, it takes one more instruction to clear than it does to set... any // way to work around this? If there were only 63 bits per word, we could @@ -337,8 +328,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable { public void fastClear(long index) { assert index >= 0 && index < numBits; int wordNum = (int)(index >> 6); // div 64 - int bit = (int)index & 0x3f; // mod 64 - long bitmask = 1L << bit; + long bitmask = 1L << index; bits[wordNum] &= ~bitmask; } @@ -346,8 +336,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable { public void clear(long index) { int wordNum = (int)(index >> 6); // div 64 if (wordNum>=wlen) return; - int bit = (int)index & 0x3f; // mod 64 - long bitmask = 1L << bit; + long bitmask = 1L << index; bits[wordNum] &= ~bitmask; } @@ -432,8 +421,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable { public boolean getAndSet(int index) { assert index >= 0 && index < numBits; int wordNum = index >> 6; // div 64 - int bit = index & 0x3f; // mod 64 - long bitmask = 1L << bit; + long bitmask = 1L << index; boolean val = (bits[wordNum] & bitmask) != 0; bits[wordNum] |= bitmask; return val; @@ -445,8 +433,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable { public boolean getAndSet(long index) { assert index >= 0 && index < numBits; int wordNum = (int)(index >> 6); // div 64 - int bit = (int)index & 0x3f; // mod 64 - long bitmask = 1L << bit; + long bitmask = 1L << index; boolean val = (bits[wordNum] & bitmask) != 0; bits[wordNum] |= bitmask; return val; @@ -458,8 +445,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable { public void fastFlip(int index) { assert index >= 0 && index < numBits; int wordNum = index >> 6; // div 64 - int bit = index & 0x3f; // mod 64 - long bitmask = 1L << bit; + long bitmask = 1L << index; bits[wordNum] ^= bitmask; } @@ -469,16 +455,14 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable { public void fastFlip(long index) { assert index >= 0 && index < numBits; int wordNum = (int)(index >> 6); // div 64 - int bit = (int)index & 0x3f; // mod 64 - long bitmask = 1L << bit; + long bitmask = 1L << index; bits[wordNum] ^= bitmask; } /** flips a bit, expanding the set size if necessary */ public void flip(long index) { int wordNum = expandingWordNum(index); - int bit = (int)index & 0x3f; // mod 64 - long bitmask = 1L << bit; + long bitmask = 1L << index; bits[wordNum] ^= bitmask; } @@ -488,8 +472,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable { public boolean flipAndGet(int index) { assert index >= 0 && index < numBits; int wordNum = index >> 6; // div 64 - int bit = index & 0x3f; // mod 64 - long bitmask = 1L << bit; + long bitmask = 1L << index; bits[wordNum] ^= bitmask; return (bits[wordNum] & bitmask) != 0; } @@ -500,8 +483,7 @@ public class OpenBitSet extends DocIdSet implements Bits, Cloneable { public boolean flipAndGet(long index) { assert index >= 0 && index < numBits; int wordNum = (int)(index >> 6); // div 64 - int bit = (int)index & 0x3f; // mod 64 - long bitmask = 1L << bit; + long bitmask = 1L << index; bits[wordNum] ^= bitmask; return (bits[wordNum] & bitmask) != 0; }