-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Slightly improve TrackingPostingsInMemoryBytesCodec #132905
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
Merged
martijnvg
merged 8 commits into
elastic:main
from
martijnvg:TrackingPostingsInMemoryBytesCodec_tweak2
Aug 18, 2025
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0f93844
Replace int hashmap by a counter in TrackingPostingsInMemoryBytesCodec
martijnvg 480ce7b
added comments
martijnvg 1ac98d9
Merge remote-tracking branch 'es/main' into TrackingPostingsInMemoryB…
martijnvg 8c61ace
iter comments
martijnvg e8c9974
Merge remote-tracking branch 'es/main' into TrackingPostingsInMemoryB…
martijnvg 9b7b0ce
Merge remote-tracking branch 'es/main' into TrackingPostingsInMemoryB…
martijnvg 5a773da
Merge remote-tracking branch 'es/main' into TrackingPostingsInMemoryB…
martijnvg 6421003
Merge remote-tracking branch 'es/main' into TrackingPostingsInMemoryB…
martijnvg 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 hidden or 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 |
|---|---|---|
|
|
@@ -22,7 +22,7 @@ | |
| import org.apache.lucene.index.SegmentWriteState; | ||
| import org.apache.lucene.index.Terms; | ||
| import org.apache.lucene.index.TermsEnum; | ||
| import org.apache.lucene.internal.hppc.IntIntHashMap; | ||
| import org.apache.lucene.internal.hppc.IntHashSet; | ||
| import org.apache.lucene.util.BytesRef; | ||
| import org.elasticsearch.common.util.FeatureFlag; | ||
|
|
||
|
|
@@ -63,22 +63,22 @@ public FieldsProducer fieldsProducer(SegmentReadState state) throws IOException | |
| static final class TrackingLengthFieldsConsumer extends FieldsConsumer { | ||
| final SegmentWriteState state; | ||
| final FieldsConsumer in; | ||
| final IntIntHashMap termsBytesPerField; | ||
| final IntHashSet seenFields; | ||
| final long[] totalBytes; | ||
|
|
||
| TrackingLengthFieldsConsumer(SegmentWriteState state, FieldsConsumer in) { | ||
| this.state = state; | ||
| this.in = in; | ||
| this.termsBytesPerField = new IntIntHashMap(state.fieldInfos.size()); | ||
| this.totalBytes = new long[1]; | ||
| // Alternatively, we can consider using a FixedBitSet here and size to max(fieldNumber). T | ||
| // his should be faster without worrying too much about memory usage. | ||
| this.seenFields = new IntHashSet(state.fieldInfos.size()); | ||
| } | ||
|
|
||
| @Override | ||
| public void write(Fields fields, NormsProducer norms) throws IOException { | ||
| in.write(new TrackingLengthFields(fields, termsBytesPerField, state.fieldInfos), norms); | ||
| long totalBytes = 0; | ||
| for (int bytes : termsBytesPerField.values) { | ||
| totalBytes += bytes; | ||
| } | ||
| state.segmentInfo.putAttribute(IN_MEMORY_POSTINGS_BYTES_KEY, Long.toString(totalBytes)); | ||
| in.write(new TrackingLengthFields(fields, state.fieldInfos, seenFields, totalBytes), norms); | ||
| state.segmentInfo.putAttribute(IN_MEMORY_POSTINGS_BYTES_KEY, Long.toString(totalBytes[0])); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -88,13 +88,15 @@ public void close() throws IOException { | |
| } | ||
|
|
||
| static final class TrackingLengthFields extends FilterLeafReader.FilterFields { | ||
| final IntIntHashMap termsBytesPerField; | ||
| final FieldInfos fieldInfos; | ||
| final IntHashSet seenFields; | ||
| final long[] totalBytes; | ||
|
|
||
| TrackingLengthFields(Fields in, IntIntHashMap termsBytesPerField, FieldInfos fieldInfos) { | ||
| TrackingLengthFields(Fields in, FieldInfos fieldInfos, IntHashSet seenFields, long[] totalBytes) { | ||
| super(in); | ||
| this.termsBytesPerField = termsBytesPerField; | ||
| this.seenFields = seenFields; | ||
| this.fieldInfos = fieldInfos; | ||
| this.totalBytes = totalBytes; | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -104,10 +106,13 @@ public Terms terms(String field) throws IOException { | |
| return null; | ||
| } | ||
| int fieldNum = fieldInfos.fieldInfo(field).number; | ||
| return new TrackingLengthTerms( | ||
| terms, | ||
| bytes -> termsBytesPerField.put(fieldNum, Math.max(termsBytesPerField.getOrDefault(fieldNum, 0), bytes)) | ||
| ); | ||
| if (seenFields.add(fieldNum)) { | ||
| return new TrackingLengthTerms(terms, bytes -> totalBytes[0] += bytes); | ||
| } else { | ||
| // As far as I know only when bloom filter for _id filter gets written this method gets invoked twice for the same field. | ||
| // So maybe we can get rid of the seenFields here? And just keep track of whether _id field has been seen? | ||
| return terms; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's difficult to guarantee that we'll only ever invoke this method twice for |
||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
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.
I agree it's probably better to prioritize speed over memory efficiency here.