← Back to Pull requests

Fix PQ build slowdown and the Panama "slow mode" #850

Closed
unknown closed this pull request without merging on Nov 30, 2026 08:32 UTC
View on GitHub
unknown unknown commented on Nov 27, 2026 18:25 UTC
Author

Fixes #845 (native PQ builds about 1.8x slower since #836).

The regression in #845 and the Panama "slow mode" have the same root: vector work split into many small calls. Natively, each subspace of a PQ code pair was its own call. In Panama, a value that crosses a call C2 doesn't inline is put on the heap, so the same build or search ran up to about 2x slower in one JVM than in the next. This PR does each job in one call, with the native and Panama providers sharing one design: the same entry points, layouts and thresholds, with only the language of the inner loop differing. PQ training had the same problem: the k-means assignment branched on the subspace width, and in some JVMs C2 left those paths allocating.

Themes

1. PQ code pairs in one call (the #845 fix). Both providers score a PQ code pair from a copy of the codebooks padded with zeros, so odd and mixed subspace widths run like even ones: one native call per pair, or one allocation-free Java loop. Subspaces of 16 floats or more use a centroid-pair table, kept with the PQ vectors being built and counted in their ramBytesUsed(), and released, with the padded copy, when the build's cleanup() or close() runs. Saved and in-progress codes both use it. jvector.pq.pairScoring, or PqPairScoring on GraphIndexBuilderConfig at runtime (JMX), can force table or direct scoring instead of the default auto; an invalid property value logs a warning and uses auto. Builds running at the same time may share a PQ model (one build's release makes the others recreate its padded copy, which costs time, not accuracy), but not one set of PQ vectors.

2. One call per job. PQ query tables are built in one call per query from transposed codebooks, with the query centered inside the kernel, on both providers. ASH on Panama gets the same treatment that native has: whole fused neighborhoods (nibble codes and INT8 field blocks), all rows of a symmetric comparison, and a whole batch of queued query projections in one call each, plus INT8 and FP16 query projection on request. The one-call query table adds its terms in a different order, so some entries differ in their last bits; recall is unaffected.

3. No Panama slow mode. Panama kernels are written so C2 keeps their vectors in registers however a JVM compiles them: each body does its whole job and is compiled on its own (above the 325-bytecode inline limit), never takes or returns a vector, names its species directly, calls lanewise at its own call sites, including the short dot and distance paths (the JDK's fma/add wrappers share one type profile across all callers), and states a vector's exact class where a store or reduction needs it. Without that, C2 guessed the class from a profile and in some JVMs lost it for one accumulator, so the PQ query table boxed in about one JVM in four.

4. Guards. PanamaNoBoxingTest checks that no Panama method takes or returns vectors and that the bulk bodies stay above the inline limit; an opt-in PanamaAllocationProbe (-Djvector.test.allocationProbe=true) measures bytes per operation from callers 0-10 deep, alone or after every other case (-Dprobe.mixed=true), and includes the k-means kernels at widths 3, 4, 8 and 16, which the test runs in mixed mode. The size guard keys on name and descriptor, so overloads such as dotProduct are guarded too. The probe confirms a kernel allocates nothing beyond its design, but it does not reproduce every profile-pollution failure: the k-means boxing found in review showed only in real builds, so a suspect kernel still needs fresh-JVM runs; PanamaNativeParityTest checks the new Panama paths against independent oracles and, where the library is loaded, against native.

5. PQ training and bulk encoding on blocks. k-means now stores points in blocks of 2,048, one dimension per row, so each SIMD lane holds a point and the subspace width is only a loop count: there are no width branches. One kernel finds the nearest centroid for a group of lanes (squared distance, or the anisotropic loss from c·x and the squared norms), and another updates the k-means++ seeding minima. Both are called once per group, so C2 compiles them from whole calls, and they state the exact vector class for every loop-carried and stored vector. The clusterer keeps its points only in blocks. PQ trains each subspace straight from the blocks, with no vector per subvector, and bulk encoding of in-memory vectors uses the same kernel. The anisotropic centroid update adds each point's outer product in place instead of building a matrix per point (anisotropic training allocated about 56 GB per 1M-vector build before, about 2 GB now). Codes and inertia match the per-point path apart from near-ties within float rounding. Unchanged: single-vector encoding, sources that reuse one buffer per read, and the mini-batch k-means used for ASH landmarks.

Results

On-disk PQ graph builds, Ice Lake (32 cores), JDK 22, graph insertion time in seconds, one fresh JVM per value (degree 32, beam 100, overflow 1.2, hierarchy, refinement, k=256, FP32 rerank). Recall@10 matched across builds.

| Dataset, subspaces | Panama: this PR / #833 / main | Native: this PR / #833 / main |
| --- | --- | --- |
| cohere-english-v3-1M, 512 x 2 floats | 151.9 / 242.5 / 376.4 | 148.7 / 208.7 / 239.3 |
| cohere-english-v3-1M, 128 x 8 | 58.9 / 76.0 / 82.1 | 41.8 / 45.1 / 90.1 |
| cohere-english-v3-1M, 32 x 32 (pair table) | 24.4 / 28.7 / 28.3 | 18.8 / 19.1 / 28.7 |
| dpr-gemma-1m, 256 x 3 | 86.3 / 441.9 / 227.1 | 78.2 / 90.4 / 242.4 |
| dpr-gemma-1m, 96 x 8 | 35.7 / 60.1 / 105.1 | 30.6 / 32.0 / 65.2 |
| dpr-gemma-1m, 24 x 32 | 17.9 / 30.6 / 23.0 | 18.1 / 19.4 / 24.6 |

Against main, builds are 1.2-2.9x faster with Panama and 1.4-3.1x faster with native. #833's 441.9 s run was in the slow mode (2,029 young GCs). Two native first runs of this PR were outliers (24.1 s and 35.7 s) and are replaced by their first alternating rerun. These builds predate the final review fixes, which change no arithmetic.

ASH with Panama, mpnet 1M, 2-bit codes, GNR (48 cores), JDK 22, mean of 3 fresh query JVMs (warm index, cold queries):

| | main | this PR |
| --- | --- | --- |
| Build | 18.8 s | 17.1 s |
| QPS, 1 core, rerank 1x | 7,680 | 11,453 |
| QPS, 8 cores, rerank 1x | 44,844 | 82,991 |
| QPS, 8 cores, rerank 2x | 30,973 | 62,722 |

The native provider remains faster for ASH, and the docs now recommend it: with the same build, Panama reached 60-65% of native's QPS on OpenAI 3072 1M (2-bit, 1 and 8 cores).

PQ training (128k-vector sample, 256 centroids) and bulk encoding of 1M vectors, JDK 22 Panama, median of 3 fresh JVMs, seconds (main → this PR):

| Machine, case | Training, first in JVM | Training, warm | Encoding, first in JVM |
| --- | --- | --- | --- |
| GNR (48 cores), cohere-english-v3, 261 subspaces | 4.55 → 1.72 | 3.23 → 0.50 | 2.47 → 0.73 |
| GNR (48 cores), openai3072, 773 subspaces | 9.99 → 2.72 | 7.81 → 1.38 | 8.02 → 1.76 |
| Ice Lake (32 cores), cohere-english-v3, 261 | 8.42 → 1.82 | 7.65 → 1.00 | 5.17 → 0.91 |
| Graviton4 (16 cores), cohere-english-v3, 261 | 13.94 → 3.30 | 15.48 → 3.04 | 11.33 → 2.98 |

The largest allocation by a single JVM fell from up to 5,311 GB on main (the slow mode) to at most 3.2 GB. Anisotropic PQ training (threshold 0.2) on GNR, warm: dpr-gemma 101 subspaces 24.5 s → 0.95 s, cohere-english-v3 261 subspaces 71 s → 1.1 s.

Known limits

  • In a JVM that has already run many other vector shapes ("mixed" mode in the probe), three ASH Panama paths still allocate: the nibble-code neighborhood kernel, the canonical LUT dot and the FP16 query view. A fresh JVM per index shape does not.

  • Panama INT8 ASH neighborhoods (3 and 5-9 bits) are several times slower than native: the Vector API has no 8-bit dot-product or multi-shift operation (JDK-8377386 proposes one).

  • With Panama, the first encoding burst of a build runs partly in C1 before C2 compiles the batch projection kernel.

  • Some kernels still allocate per call with Panama, exactly as much as on main: exact cosine at some dimensions, Hamming distance, the element-wise utilities, the ASH dot row and the 1-bit symmetric dot. #755 addresses all but exact cosine, which is still open.
  • Tests

    Full jvector-native and jvector-tests suites on Ice Lake (8 cores), JDK 22 and JDK 25, native and Panama providers, compared with main on the same machine: no new failures. The remaining failures also fail on main (provider-selection tests that see the harness's JDK_JAVA_OPTIONS, and JDK 25 Panama ASH tests that fail at random). After the review fixes, the PQ, ASH, parity and guard tests (243) ran again on JDK 22 and 25 with both providers: no new failures. The allocation probe ran every case in four fresh JVMs: every PQ query-table and code-pair case allocated nothing in every JVM. Parity tests also ran on GNR (48 cores). The k-means changes add an exact-match test of the block kernels against a scalar scan (PanamaKMeansBlockTest: widths 0-65, block sizes 1-2,048, ties and non-finite values) and a bulk-encoding test (TestPQBlockEncoding). TestPQBuildCaches checks that cleanup() releases the pair tables and the padded copy, and TestPQPairScoring that each scoring mode picks the expected scorer and matches the decoded vectors. The PQ, k-means, encoding, graph and compaction tests pass on GNR (48 cores) with JDK 22 and 25 Panama and JDK 22 native. After the second review round, the PQ, k-means, encoding, graph and builder-config tests pass on Ice Lake (32 cores) with JDK 22 Panama and native and JDK 25 Panama.

    Release note

    docs/release notes/4.1.0-rc.2/850.performance.md

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: MarkWolters Review duration: 3727m
    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
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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 #850"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%