LUCENE-6003: speedups for SparseFixedBitSet

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1630764 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2014-10-10 11:30:33 +00:00
parent b47a3602fb
commit 4e4d809f2d
1 changed files with 26 additions and 27 deletions

View File

@ -153,25 +153,29 @@ public class SparseFixedBitSet extends DocIdSet implements Bits {
final int i4096 = i >>> 12;
final long index = indices[i4096];
final int i64 = i >>> 6;
if (index == 0) {
if ((index & (1L << i64)) != 0) {
// in that case the sub 64-bits block we are interested in already exists,
// we just need to set a bit in an existing long: the number of ones on
// the right of i64 gives us the index of the long we need to update
bits[i4096][Long.bitCount(index & ((1L << i64) - 1))] |= 1L << i; // shifts are mod 64 in java
} else if (index == 0) {
// if the index is 0, it means that we just found a block of 4096 bits
// that has no bit that is set yet. So let's initialize a new block:
insertBlock(i4096, i64, i);
} else {
// in that case we found a block of 4096 bits that has some values, but
// the sub-block of 64 bits that we are interested in has no value yet,
// so we need to insert a new long
insertLong(i4096, i64, i, index);
}
}
private void insertBlock(int i4096, int i64, int i) {
indices[i4096] = 1L << i64; // shifts are mod 64 in java
assert bits[i4096] == null;
bits[i4096] = new long[] { 1L << i }; // shifts are mod 64 in java
++nonZeroLongCount;
ramBytesUsed += SINGLE_ELEMENT_ARRAY_BYTES_USED;
} else if ((index & (1L << i64)) == 0) {
// in that case we found a block of 4096 bits that has some values, but
// the sub-block of 64 bits that we are interested in has no value yet,
// so we need to insert a new long
insertLong(i4096, i64, i, index);
} else {
// in that case the sub 64-bits block we are interested in already exists,
// we just need to set a bit in an existing long: the number of ones on
// the right of i64 gives us the index of the long we need to update
bits[i4096][Long.bitCount(index & ((1L << i64) - 1))] |= 1L << i; // shifts are mod 64 in java
}
}
private void insertLong(int i4096, int i64, int i, long index) {
@ -244,7 +248,7 @@ public class SparseFixedBitSet extends DocIdSet implements Bits {
@Override
public int nextDoc() throws IOException {
if (doc == NO_MORE_DOCS || ++doc >= length) {
if (++doc >= length) {
return doc = NO_MORE_DOCS;
}
return currentOrNextDoc();
@ -253,22 +257,17 @@ public class SparseFixedBitSet extends DocIdSet implements Bits {
private int currentOrNextDoc() {
final int i4096 = doc >>> 12;
final long index = indices[i4096];
if (index == 0) {
int i64 = doc >>> 6;
long indexBits = index >>> i64;
if (indexBits == 0) {
// if the index is zero, it means that there is no value in the
// current block, so return the first document of the next block
return firstDoc(i4096 + 1);
} else {
// now we are on a block that contains at least one document
assert Long.bitCount(index) <= bits[i4096].length;
int i64 = doc >>> 6;
long indexBits = index >>> i64; // shifts are mod 64 in java
if (indexBits == 0) {
// or
// if neither the i64-th bit or any other bit on its left is set then
// it means that there are no more documents in this block, go to the
// next one
return firstDoc(i4096 + 1);
}
} else {
// We know we still have some 64-bits blocks that have bits set, let's
// advance to the next one by skipping trailing zeros of the index
int i1 = doc & 0x3F;