From 86eca03f5d13e52c1ecceb33a4b376983b66c4c3 Mon Sep 17 00:00:00 2001 From: Enrico Olivelli Date: Fri, 22 May 2026 22:52:37 +0200 Subject: [PATCH] Use ExplicitThreadLocal for scratch int[] in PanamaVectorUtilSupport 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) --- .../jbellis/jvector/vector/PanamaVectorUtilSupport.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/jvector-twenty/src/main/java/io/github/jbellis/jvector/vector/PanamaVectorUtilSupport.java b/jvector-twenty/src/main/java/io/github/jbellis/jvector/vector/PanamaVectorUtilSupport.java index 51b9eceea..e95ad1323 100644 --- a/jvector-twenty/src/main/java/io/github/jbellis/jvector/vector/PanamaVectorUtilSupport.java +++ b/jvector-twenty/src/main/java/io/github/jbellis/jvector/vector/PanamaVectorUtilSupport.java @@ -16,6 +16,7 @@ package io.github.jbellis.jvector.vector; +import io.github.jbellis.jvector.util.ExplicitThreadLocal; import io.github.jbellis.jvector.util.MathUtil; import io.github.jbellis.jvector.vector.types.ByteSequence; import io.github.jbellis.jvector.vector.types.VectorFloat; @@ -40,8 +41,8 @@ class PanamaVectorUtilSupport implements VectorUtilSupport { static final IntVector BYTE_TO_INT_MASK_512 = IntVector.broadcast(IntVector.SPECIES_512, 0xff); static final IntVector BYTE_TO_INT_MASK_256 = IntVector.broadcast(IntVector.SPECIES_256, 0xff); - static final ThreadLocal scratchInt512 = ThreadLocal.withInitial(() -> new int[IntVector.SPECIES_512.length()]); - static final ThreadLocal scratchInt256 = ThreadLocal.withInitial(() -> new int[IntVector.SPECIES_256.length()]); + static final ExplicitThreadLocal scratchInt512 = ExplicitThreadLocal.withInitial(() -> new int[IntVector.SPECIES_512.length()]); + static final ExplicitThreadLocal scratchInt256 = ExplicitThreadLocal.withInitial(() -> new int[IntVector.SPECIES_256.length()]); protected FloatVector fromVectorFloat(VectorSpecies SPEC, VectorFloat vector, int offset) { if (vector instanceof BufferVectorFloat bv) {