← Back to Pull requests

Make the tryClosestCentroid assertion cheap #834

Closed
unknown closed this pull request without merging on Nov 25, 2026 02:23 UTC
View on GitHub
unknown unknown commented on Nov 24, 2026 17:29 UTC
Author

Some users run JVector with -ea in production, so hot-path assertions must be cheap. With -ea, VectorUtil.tryClosestCentroid rescanned every centroid through the vector provider on each call. With the native provider that is billions of extra calls during PQ encoding and k-means.

Now each call checks only the selected distance, in plain Java, which also catches a non-finite point. Centroids are checked once when they are built: k-means checks them in its constructor and once per iteration. Codebooks come only from k-means or from a saved index, so the PQ constructor adds no check of its own. TestPQClosestCentroid now tests the k-means check and the per-call check.

One difference: if finite inputs overflow to an infinite distance for a centroid that isn't chosen, native no longer fails under -ea, while the fallback loops still do. This can't change the chosen centroid.

A scan of every assertion in jvector-base, jvector-twenty and jvector-native found no other hot-path assertion that repeats its work. CONTRIBUTIONS.md now states the rule.

Testing (Ice Lake, 8 cores, -ea):

  • PQ training and encoding on siftsmall (64 subspaces × 256 centroids, JDK 22): with native, encode went from 1.54 s to 0.31 s and training from 1.60 s to 0.60 s. Panama is unchanged.

  • Quantization, provider and graph-compatibility tests (245) pass on JDK 22 and 25 with native and Panama, except 3 JDK 25 Panama TestMutableASHVectors failures that also occur on main.
  • Diff: 185 lines (+138 / -47) across 2 files Reviewer: MarkWolters Review duration: 534m
    All Vouch review confidence checks passed
    Residual risk 0.14 = Change Risk (0.36) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.36 · 185 lines diff across 2 files
    0.36
    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.313
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #834"
    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: 35.7% (MEDIUM) · Higher = larger blast radius
    35.7%
    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: 14.1% · Safe to merge
    14.1%