[go 1.27rc2] perf: BM25 SIMD/scalar accumulator dispatch with struct-aware binary search#8
Open
shoriwe wants to merge 10 commits into
Open
[go 1.27rc2] perf: BM25 SIMD/scalar accumulator dispatch with struct-aware binary search#8shoriwe wants to merge 10 commits into
shoriwe wants to merge 10 commits into
Conversation
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.
Summary
This PR refactors the BM25 hot path into two separate accumulator implementations, scalar and AVX2, selected at runtime based on CPU capability. It also eliminates all
slices.BinarySearchFunccalls in the scoring and storage layers, replacing them with inlined, struct-aware binary search methods that avoid function call overhead and closure allocations.Changes
accumulateBM25intoScalarAccumulateBM25(query/bm5_acc_scalar.go) andAVX2AccumulateBM25(query/bm25_acc_avx2.go). The dispatch wrapperAccumulateBM25selects the AVX2 path whenarchsimd.X86.AVX2()is true andForceScalaris not set; falls back to scalar otherwise.binarysearchpackage dependency fromstorage/storage.go. All binary search calls acrossTokens,TokenFrequencies,DocumentsLengths, andScoringnow use methods defined directly on their respective types (BinarySearchString,BinarySearchBytes,BinarySearch). This eliminates pointer indirection and closure overhead on every call in the hot path.TokenFrequenciesandDocumentsLengthsnamed slice types instorage/headers.goto host the new methods. UpdatedField.DocumentLengthsandStorage.TokenFrequenciesfield types accordingly.binarysearch/pointer.goas it is no longer used.ForceScalarenv var (FORCE_SCALAR=1) to force the scalar path for benchmarking and debugging.ForceScalar boolfield toSearcherfor programmatic override.NormalizedTFandScoreTermBM25are no longer public API; tests that exercised them independently have been removed. Correctness is now enforced at the end-to-end level (TestPropertyInvariantsExtended), where the forward/reverse scoring delta tolerance was relaxed from1e-12to1e-5to accommodate SIMD rounding.go.modtogo 1.27rc2.Performance context (i7-1280P, 46M hits, n=1)
The struct-aware binary search accounts for most of the gain. The AVX2 path is currently slower than scalar 8x on this workload, which is expected given gather overhead dominates at this hit count. The AVX2 path is kept for future tuning and for hardware where the balance shifts.
Raw output