← Back to Pull requests

Keep Panama PQ and k-means distance loops in their own methods #843

Closed
unknown closed this pull request without merging on Nov 26, 2026 03:11 UTC
View on GitHub
unknown unknown commented on Nov 25, 2026 23:07 UTC
Author

On JDK 22 with the Panama provider, PQ training and encoding were sometimes much slower in a fresh JVM. The per-point and per-centroid distance loops for small subvectors (4 floats for 1024 dimensions at M=256) were sometimes compiled inside a large calling method, where C2 could not unbox one operand of the small-vector subtraction (-XX:+PrintIntrinsics prints unbox failed), so each subtraction became a heap-allocating call. Three slow modes showed up:

  • encode (encodeAll): about 11x slower, 179-190 s instead of about 17 s, in 7 of 12 JVMs on Ice Lake (8 cores);

  • training, k-means assignment: about 15x slower (compute 61-67 s, refine 11 s instead of about 4.5 s and 0.7 s), in 3 of 8 JVMs on GNR (48 cores);

  • training, k-means++ initialization: about 3x slower compute (12-13.5 s), refine unaffected.
  • Fix: PanamaVectorUtilSupport now implements tryClosestCentroid and tryUpdateKMeansDistances, as the native provider already does. Each one runs its whole loop in one method too large for C2 to inline into its callers (above FreqInlineSize, 325 bytecodes), so the loop is always compiled as its own unit. They use the same vector operations as squareDistance and minInPlace, keep the first centroid on ties and check their inputs like the native versions. PQ encoding, k-means assignment and k-means++ initialization use them. Native is unchanged.

    Results, fresh JVMs, alternating order, 1M x 1024d cohere, M=256, times in seconds:

    | Machine | JDK, provider | Phase | main | this PR |
    |---|---|---|---|---|
    | Ice Lake (8 cores) | 22 Panama | encode | 16.8 (fast JVMs), 179-190 (slow, 7 of 12) | 15.0-15.3, 0 of 9 slow |
    | Ice Lake (8 cores) | 22 Panama | train (compute / refine) | 19.4 / 4.37 | 17.8 / 3.93 |
    | Ice Lake (8 cores) | 25 Panama | encode | 13.9 | 12.6 |
    | Ice Lake (8 cores) | 25 Panama | train (compute / refine) | 15.9 / 3.53 | 14.7 / 3.16 |
    | GNR (48 cores) | 22 Panama | train compute | slow in 4 of 8 JVMs (61-67 s 15x, 11.8 s 3x) | 3.6-4.9, 0 of 24 slow |
    | both | 22 / 25 native | all | unchanged | unchanged |

    k-means++ initialization alone (GNR, 8 subspaces of 128k real subvectors, k=256, one thread, 4 JVMs each; mean of each JVM's steady-state median, ms; "before" runs the same loop as main):

    | JDK | dims | before | this PR |
    |---|---|---|---|
    | 22 | 2 / 4 / 8 | 766 / 1078 / 1140 | 630 / 834 / 1107 |
    | 25 | 2 / 4 / 8 | 839 / 1011 / 1166 | 754 / 824 / 981 |

    The unchanged assignment step, timed in the same JVMs, is identical in both builds.

    Anisotropic PQ encoding, which does not use these loops, showed no slow mode on main (5 of 5 JVMs within 0.5%).

    Tests: new PanamaClosestCentroidTest checks both methods against the loops they replace (dimensions 0-65, 1-300 points or centroids, integer and Gaussian data), ties, non-finite values and input checks. KMeansInitializationTest now expects Panama to handle every dimension. They pass with the PQ, k-means and closest-centroid tests on JDK 22 and 25 at 512-bit, AVX2 and 128-bit vector widths, Panama and native.

    Diff: 379 lines (+284 / -95) across 4 files Reviewer: MarkWolters Review duration: 243m
    All Vouch review confidence checks passed
    Residual risk 0.19 = Change Risk (0.43) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.43 · 379 lines diff across 4 files
    0.43
    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%
    56%
    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.355
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #843"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% 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: 42.9% (MEDIUM) · Higher = larger blast radius
    42.9%
    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: 56.8% · Lower = rushed or shallow review
    56.8%
    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: 18.6% · Safe to merge
    18.6%