Keep PQ encoding on SIMD on JDK 21 when C2 compiles the stream frame first #839
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #839"
general
weight: 0.35
tlwillke
Fixes #801.
Summary
On JDK 21,
ProductQuantization.encodeAllcan run the Vector API's Java fallback instead of SIMD, depending on which method C2 compiles first in a fresh JVM. This PR makes both changes proposed in #801, one commit each.1. Point-access
VectorAccessscans loop over claimed chunks. Before,forEachcalled the consumer once per vector fromIntStream.forEach. That made the JDK frame shared by everyIntStream.forEachthe hot compile root, and C2 hit its inlining limit insideFloatVector.lanewiseTemplate:Claiming small chunks from a shared counter also does three other things:
Batched sources are unchanged.
2. The Panama provider scans 1–3-float subspaces with scalar code.
PanamaVectorUtilSupport.tryClosestCentroidnow handles subspaces of up to 3 floats, which before only the native provider (JDK 22+) did. Every caller ofclosestCentroidIndexand of the k-means assignment then skips the Vector API for these shapes, whatever the call shape above it. That includes callers outsideVectorAccess.forEach, such as single-vectorencode()andMutablePQVectors.Each distance sums its terms in the order
squareDistancedoes, so the codes don't change. Wider subspaces return-1and keep the SIMD scan, and so do vectors the provider didn't create. With-ea, the selected distance is checked byVectorUtilas of #834, so the scan needs no assertions of its own.Evidence
All numbers are from Apple M5 Pro (arm64), JDK 21.0.13,
PanamaVectorizationProvider, 4 encode threads (-Djvector.physical_core_count=4). Both builds are onmainat 7d04508e. The harness is the one from #801, with two additions:Each JVM trains its own PQ, then times 6
encodeAllrounds over 20,000 random 768-d vectors with 256 centroids. Every JVM run is reported; none were dropped or rerun.Nanoseconds per centroid distance, one fresh JVM per run
| Subspace | Build | JVMs that fell back | Steady state (rounds 2–6, median) | Round 1 |
|---|---|--:|--:|--:|
| 2 floats |
main| 2 / 10 | 0.158 with SIMD, 4.75 after a fallback (30×) | 0.15–2.49; over 1 in 7 of 10 || 2 floats | this PR | 0 / 10 | 0.166 | 0.16–0.21 |
| 4 floats |
main| 0 / 5 | 0.175 | 0.18–0.20 || 4 floats | this PR | 0 / 5 | 0.177 | 0.18–0.19 |
| 8 floats |
main| 0 / 5 | 0.473 | 0.43–0.47 || 8 floats | this PR | 0 / 5 | 0.445 | 0.44–0.49 |
| 2 floats,
-ea|main| 1 / 6 | 0.189 with SIMD, 4.91 after a fallback | 2.08–2.47 in all 6 || 2 floats,
-ea| this PR | 0 / 6 | 0.167 | 0.16–0.20 |On
main, even JVMs that recovered often ran their firstencodeAll7–15× slower, about 4 s instead of 0.3 s for 20,000 vectors. Codes were identical in every run of every build: one checksum per width.CPU profiles
These are JFR CPU samples (
-XX:StartFlightRecording=settings=profile), filtered to stacks underPQVectors.encodeAndBuildso training is left out. Red frames arejdk.incubator.vector.main, a JVM that fell back (round 2–6: 4.7–4.9 ns/distance). The samples sit in the Vector API's Java fallback, which computes one lane at a time, underFloatVector.sub:FloatVector.lanewiseTemplate;FloatVectorlambda;fromArray0Template;bOpTemplate.<img width="1600" height="798" alt="flame-before-fallback" src="https://github.com/user-attachments/assets/e8e2f32d-d928-4d6c-8e28-7aa5694382b5" />
main, a JVM that kept SIMD (same build, same command, a different JVM):<img width="1600" height="577" alt="flame-before-simd" src="https://github.com/user-attachments/assets/fecb1671-3ec1-4ecf-ae92-cc4ed1ca534b" />
This PR. 80% of samples are in
closestCentroid2, and no Vector API frames appear in the encode:<img width="1600" height="577" alt="flame-after" src="https://github.com/user-attachments/assets/17d64b8c-d9ac-4d87-baa2-3d3826b5f73b" />
Top methods from
jfr view hot-methods:The fallback allocates a vector object per operation. Young GCs during the whole run, for 5 recorded JVMs per build:
| Build | Per JVM | Outcome |
|---|---|---|
|
main| 1,608, 121, 7, 112, 1,712 | fell back throughout, round 1 fell back, kept SIMD, round 1 fell back, fell back throughout || this PR | 7, 7, 7, 7, 7 | kept SIMD |
C2 inlining (
-XX:+PrintCompilation -XX:+PrintInlining)These two excerpts were recorded on the previous base, 764a2d8b.
VectorAccessand the encode chain are the same on 7d04508e.On
main, in a JVM that fell back, the tier-4 compile ofjava.util.stream.ForEachOps$ForEachOp$OfInt::acceptinlines the encode and failssub's intrinsic, as described in #801:With this PR, using 8-float subspaces so the Vector API path is still in use, the stream frame never gets a tier-4 compile. C2 compiles the chunk loop on its own, and the encode is a separate root:
Across that whole run: 50 C2
binaryOpintrinsics, 0unbox failed. The onebinaryOpmarked "failed to inline (intrinsic)" is in a tier-3 (C1) compile, which never intrinsifies.Time to reach a safepoint, Parallel GC
-XX:+UseParallelGC, a thread dump every 20 ms during 10 encode rounds,-Xlog:safepoint, 3 JVMs per build:| Build | Worst wait for the encode threads to reach a safepoint |
|---|---|
|
main| 2.27, 0.17, 1.51 ms (all 3 fell back; the Java fallback keeps its safepoint polls) || this PR | 1.01, 0.98, 1.02 ms |
A first version of this change ran one contiguous partition per worker. On the previous base it made that wait 305–369 ms, because C2 had dropped the poll from the whole partition's loop. The 16-position chunks bound the wait to one chunk.
Tests
TestClosestCentroid(new) checks the PanamatryClosestCentroidagainst the per-centroidsquareDistancescan it replaces:- Inputs cover dimensions 0–16, 1–256 centroids, many exact ties, NaN, infinities, ±0, and invalid regions.
- Centroids that permute one difference vector differ only in summation order, so this case catches a kernel that adds the terms in another order.
- With the Panama provider, dimensions ≤ 3 must be handled. On
mainthis fails withdim 0. Actual: -1.TestVectorAccessadds two tests (new):- The first checks a point-access mapped scan: every output position is visited once with its mapped vector, each worker reads only the copy it made, and it looks the copy up once rather than once per vector. Against
main's per-vectorforEachit fails withcount 64 lookups 64.- The second checks that a consumer failure propagates.
jvector-testssuite on JDK 21 with-Pjdk20 -DTest_RequireSpecificVectorizationProvider=PanamaVectorizationProvider: 714 run, 0 failures.jvector-nativecompiles on JDK 27, andjvector-basepassesmvn javadoc:jar.Merge Danger
Door: two-way
There is no API, format or on-disk change, and codes are bit-for-bit identical, so a revert is free.
Blast Radius: throughput
-eait's about 12% faster thanmain's SIMD path.VectorAccess.forEachon point-access sources now takes one atomic increment per 16 positions.tryClosestCentroid, so change 2 doesn't affect it.benchmarks-jmhcompiles with--release 22, so neither can catch a return of #801. The harness above, run on several fresh JVMs, is the reproducer.Checklist notes
docs/release notes/4.1.0-rc.2/839.bugfix.md.mainat 7d04508e, which includes #834's-eachange.