-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reduce memory usage of match all bitset #92777
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 92777 | ||
summary: Reduce memory usage of match all bitset | ||
area: Search | ||
type: enhancement | ||
issues: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
server/src/main/java/org/elasticsearch/lucene/util/BitSets.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.lucene.util; | ||
|
||
import org.apache.lucene.search.DocIdSetIterator; | ||
import org.apache.lucene.util.BitSet; | ||
|
||
import java.io.IOException; | ||
|
||
public final class BitSets { | ||
private BitSets() {} | ||
|
||
/** | ||
* Build a {@link BitSet} from the content of the provided {@link DocIdSetIterator}. If the iterator matches all documents, | ||
* then this method will wrap the returned Bitset as {@link MatchAllBitSet} to reduce memory usage. | ||
*/ | ||
public static BitSet of(DocIdSetIterator iter, int maxDocs) throws IOException { | ||
final BitSet bitSet = BitSet.of(iter, maxDocs); | ||
if (bitSet.cardinality() == maxDocs) { | ||
return new MatchAllBitSet(maxDocs); | ||
} else { | ||
return bitSet; | ||
} | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
server/src/main/java/org/elasticsearch/lucene/util/MatchAllBitSet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.lucene.util; | ||
|
||
import org.apache.lucene.search.DocIdSetIterator; | ||
import org.apache.lucene.util.BitSet; | ||
import org.apache.lucene.util.FixedBitSet; | ||
import org.apache.lucene.util.RamUsageEstimator; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* An optimized implementation of {@link BitSet} that matches all documents to reduce memory usage. | ||
*/ | ||
public final class MatchAllBitSet extends BitSet { | ||
private static final long RAM_BYTES_USED = RamUsageEstimator.shallowSizeOfInstance(MatchAllBitSet.class); | ||
|
||
private FixedBitSet bits; | ||
private final int numBits; | ||
|
||
public MatchAllBitSet(int numBits) { | ||
this.numBits = numBits; | ||
} | ||
|
||
@Override | ||
public void set(int i) { | ||
if (bits != null) { | ||
bits.set(i); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean getAndSet(int i) { | ||
if (bits != null) { | ||
return bits.getAndSet(i); | ||
} else { | ||
return true; | ||
} | ||
} | ||
|
||
private void initializeBitSet() { | ||
bits = new FixedBitSet(numBits); | ||
bits.set(0, bits.length()); | ||
} | ||
|
||
@Override | ||
public void clear(int i) { | ||
if (bits == null) { | ||
initializeBitSet(); | ||
} | ||
bits.clear(i); | ||
} | ||
|
||
@Override | ||
public void clear(int startIndex, int endIndex) { | ||
if (bits == null) { | ||
initializeBitSet(); | ||
} | ||
bits.clear(startIndex, endIndex); | ||
} | ||
|
||
@Override | ||
public int cardinality() { | ||
if (bits != null) { | ||
return bits.cardinality(); | ||
} else { | ||
return numBits; | ||
} | ||
} | ||
|
||
@Override | ||
public int approximateCardinality() { | ||
if (bits != null) { | ||
return bits.approximateCardinality(); | ||
} else { | ||
return numBits; | ||
} | ||
} | ||
|
||
@Override | ||
public int prevSetBit(int index) { | ||
if (bits != null) { | ||
return bits.prevSetBit(index); | ||
} else { | ||
return index; | ||
} | ||
} | ||
|
||
@Override | ||
public int nextSetBit(int index) { | ||
if (bits != null) { | ||
return bits.nextSetBit(index); | ||
} else { | ||
return index; | ||
} | ||
} | ||
|
||
@Override | ||
public long ramBytesUsed() { | ||
return RAM_BYTES_USED + (bits != null ? bits.ramBytesUsed() : 0); | ||
} | ||
|
||
@Override | ||
public boolean get(int index) { | ||
if (bits != null) { | ||
return bits.get(index); | ||
} else { | ||
return true; | ||
} | ||
} | ||
|
||
@Override | ||
public int length() { | ||
return numBits; | ||
} | ||
|
||
@Override | ||
public void or(DocIdSetIterator iter) throws IOException { | ||
if (bits != null) { | ||
bits.or(iter); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
server/src/test/java/org/elasticsearch/lucene/util/BitSetsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.lucene.util; | ||
|
||
import org.apache.lucene.search.DocIdSetIterator; | ||
import org.apache.lucene.util.BitSet; | ||
import org.apache.lucene.util.BitSetIterator; | ||
import org.apache.lucene.util.FixedBitSet; | ||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.instanceOf; | ||
|
||
public class BitSetsTests extends ESTestCase { | ||
|
||
public void testRandomBitset() throws Exception { | ||
int maxDocs = randomIntBetween(1, 1024); | ||
int numDocs = 0; | ||
FixedBitSet matches = new FixedBitSet(maxDocs); | ||
for (int i = 0; i < maxDocs; i++) { | ||
if (numDocs < maxDocs && randomBoolean()) { | ||
numDocs++; | ||
matches.set(i); | ||
} | ||
} | ||
DocIdSetIterator it = new BitSetIterator(matches, randomIntBetween(0, numDocs)); | ||
BitSet bitSet = BitSets.of(it, maxDocs); | ||
assertThat(bitSet.cardinality(), equalTo(numDocs)); | ||
assertThat(bitSet.length(), equalTo(maxDocs)); | ||
for (int i = 0; i < maxDocs; i++) { | ||
assertThat(bitSet.get(i), equalTo(matches.get(i))); | ||
assertThat(bitSet.nextSetBit(i), equalTo(matches.nextSetBit(i))); | ||
assertThat(bitSet.prevSetBit(i), equalTo(matches.prevSetBit(i))); | ||
} | ||
} | ||
|
||
public void testMatchAllBitSet() throws Exception { | ||
int maxDocs = randomIntBetween(1, 128); | ||
FixedBitSet matches = new FixedBitSet(maxDocs); | ||
for (int i = 0; i < maxDocs; i++) { | ||
matches.set(i); | ||
} | ||
DocIdSetIterator it = new BitSetIterator(matches, randomNonNegativeLong()); | ||
BitSet bitSet = BitSets.of(it, maxDocs); | ||
assertThat(bitSet, instanceOf(MatchAllBitSet.class)); | ||
for (int i = 0; i < maxDocs; i++) { | ||
assertTrue(bitSet.get(i)); | ||
assertThat(bitSet.nextSetBit(i), equalTo(matches.nextSetBit(i))); | ||
assertThat(bitSet.prevSetBit(i), equalTo(matches.prevSetBit(i))); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we actually need to support clearing bits on this
MatchAllBitSet
? It's counter intuitive to me that something calledMatchAllBitSet
could have some of its bits not set.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call. I've pushed 0f12598 to make it read-only.