ii.filesMerge: did O(n^2) re-merge of sequences (chunks of same key)#19680
Merged
Conversation
JkLondon
approved these changes
Mar 6, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Story: i noticed on profiler that during InvertedIndex files merge
deriveFields()fun did many re-allocs. Initially i had intent to 2x over-alloc to amortize it - but realized that we doing multiple merges of same key (multiple chunks from multiple small files) - each such merge produced "bigger sequence". So, clearly higher-level logic of merge files is wrong and need merge all chunks at once.Before: incremental pairwise merge — for a key in N files, reads C + 2C + 3C + ... + NC = O(N²·C) elements and calls EliasFano.ResetForWrite(→ deriveFields → make([]uint64, ...)) N times, allocating a new backing array each time since the merged size always exceeds the previous capacity.
After: single-pass accumulation — collects all N items for the current key, computes totalCount and maxOff in one scan, initialises the builder once with the correct size (so deriveFields only allocates once per key at the right capacity), then iterates the N files in ascending txNum order adding all values in a single pass. O(N·C) reads, O(1) allocations per key.
BenchmarkInvertedIndexMergeFiles: