Use ExplicitThreadLocal for scratch int[] in PanamaVectorUtilSupport#17
Merged
Conversation
Wall-clock profiling of a HerdDB indexing workload showed PanamaVectorUtilSupport.assembleAndSum256 spending non-trivial time in java.lang.ThreadLocal.get / ThreadLocalMap.getEntry. The host process carries many ThreadLocal instances (Netty FastThreadLocalRunnable, gRPC, metrics, ...), which puts the JDK ThreadLocalMap under enough hash pressure that lookups become noticeable on this very hot path. Switch the two static scratchInt256/scratchInt512 fields to ExplicitThreadLocal, jvector's existing CHM-backed thread-local primitive (documented as a drop-in replacement for ThreadLocal and already used by GraphIndexBuilder, FusedPQ, RandomAccessVectorValues). Its lookup cost is independent of how many other thread-locals live on the host thread, sidestepping the ThreadLocalMap pathology. No call-site changes are required; .get() semantics are identical. The static lifetime matches what the ThreadLocal had, so no close() is needed. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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
PanamaVectorUtilSupport.assembleAndSum256spending non-trivial time injava.lang.ThreadLocal.get/ThreadLocalMap.getEntry. The host process carries manyThreadLocalinstances (NettyFastThreadLocalRunnable, gRPC, metrics, …), so the JDKThreadLocalMap's open-addressing hash is under enough pressure for probing to be visible on this hot path.scratchInt256/scratchInt512fields toExplicitThreadLocal, jvector's existing CHM-backed thread-local primitive — documented as a drop-in replacement forThreadLocaland already used byGraphIndexBuilder,FusedPQ,RandomAccessVectorValues. Its lookup cost is independent of how many other thread-locals live on the host thread..get()consumers inassembleAndSum*,calculatePartialSums*,squareDistance*are unchanged). Static lifetime matches what theThreadLocalhad, so noclose()plumbing is required.Test plan
./mvnw -pl jvector-twenty -am -DskipTests package— builds clean./mvnw -pl jvector-tests test -Dtest=TestVectorizationProvider—Tests run: 3, Failures: 0, Errors: 0ThreadLocalMap.getEntrydisappears from underassembleAndSum256🤖 Generated with Claude Code