← Back to Pull requests

Streaming N:1 compaction #659

Closed
unknown closed this pull request without merging on Jun 18, 2026 19:55 UTC
View on GitHub
unknown unknown commented on Apr 14, 2026 22:15 UTC
Author

This PR addresses #580 that adds OnDiskGraphIndexCompactor, a streaming N:1 compaction algorithm for merging multiple on-disk HNSW graph indexes into a single compacted index.

``
source[0].index ─┐
source[1].index ─┤──► OnDiskGraphIndexCompactor ──► compacted.index
source[N].index ─┘

                                                                                                                                                             
  For a full description of the algorithm and benchmarking instructions, see docs/compaction.md and benchmarks-jmh/src/main/java/io/github/jbellis/jvector/bench/CompactorBenchmark.md.
                                                                                                                                                             
  ### Support                                                                                                                                                
  - **Streaming, low-memory**: no full in-memory graph construction; runs under `-Xmx5g` even for 10M-vector, 2560-dim datasets
  - **Deletion support**: live-node `FixedBitSet` per source excludes deleted nodes from output                                                              
  - **Ordinal remapping**: user-provided `OrdinalMapper` maps each source's local ordinals to a contiguous global ordinal space; implemented `OffsetMapper` that handles the  common sequential case                                                                                                                                     
                                                                                                                                                             
  ### Usage                                                                                                                                                  
                  
  
java
List<OnDiskGraphIndex> sources = List.of(index0, index1, index2);

// Mark all nodes live (no deletions)
List<FixedBitSet> liveNodes = sources.stream()
.map(s -> { var bs = new FixedBitSet(s.size()); bs.set(0, s.size()); return bs; })
.collect(toList());

// Sequential ordinal remapping: source[s] node i → global offset[s] + i
int offset = 0;
List<OrdinalMapper> remappers = new ArrayList<>();
for (var src : sources) {
remappers.add(new OrdinalMapper.OffsetMapper(offset, src.size()));
offset += src.size();
}

var compactor = new OnDiskGraphIndexCompactor(
sources, liveNodes, remappers,
VectorSimilarityFunction.COSINE,
/ executor= / null // null = use by default shared threadpool in compactor
);

compactor.compact(Path.of("compacted.index"));
`

Benchmarking example


java -Xmx220g --add-modules jdk.incubator.vector \
  -cp benchmarks-jmh/target/compactor-benchmark.jar \
  io.github.jbellis.jvector.bench.CompactorBenchmark \
  -p workloadMode=PARTITION_AND_COMPACT \
  -p datasetNames=ada002-100k \
  -p numPartitions=4 \
  -p splitDistribution=FIBONACCI \
  -p indexPrecision=FUSEDPQ \
  -wi 0 -i 1 -f 1
  

### Key Changes

-
OnDiskGraphIndexCompactor — core compaction algorithm with parallel ForkJoinPool execution and backpressure windowing
-
PQRetrainer — balanced proportional sampling + sequential sorted reads for efficient codebook retraining
- Minor API visibility changes to
GraphSearcher, GraphIndexBuilder, and PQVectors required by the compactor
-
CompactorBenchmark — JMH benchmark with PARTITION_AND_COMPACT, PARTITION, COMPACT, and BUILD modes
- Unit tests covering basic compaction, deletions, ordinal remapping, and FusedPQ scenarios

### Recall

Comparison against build-from-scratch (results averaged over three runs).

- Build from scratch: build with PQ, search using FusedPQ with FP reranking.
- Compaction: build source partitions with PQ, compact using FusedPQ with FP rescoring, search using FusedPQ with FP reranking. Source partitions are based on a Fibonacci distribution with 4 partitions.

| Dataset | Dim | Build from Scratch | Compaction | Delta |
|-----------------------|-----:|-------------------:|-----------:|-------:|
| cap-6M | 768 | 0.626 | 0.619 | -0.008 |
| cap-1M | 768 | 0.656 | 0.656 | 0.000 |
| gecko-100k | 768 | 0.690 | 0.701 | +0.011 |
| e5-small-v2-100k | 384 | 0.572 | 0.586 | +0.014 |
| ada002-1M | 1536 | 0.687 | 0.703 | +0.016 |
| e5-base-v2-100k | 768 | 0.676 | 0.692 | +0.016 |
| cohere-english-v3-10M | 1024 | 0.544 | 0.561 | +0.017 |
| e5-large-v2-100k | 1024 | 0.686 | 0.703 | +0.017 |
| ada002-100k | 1536 | 0.751 | 0.769 | +0.018 |
| cohere-english-v3-1M | 1024 | 0.593 | 0.612 | +0.019 |

Recall is generally comparable to build-from-scratch and often better, though some datasets show small drops. All datasets compact successfully under
-Xmx5g`; compaction has also been validated on a 2560-dim 10M-vector dataset under the same constraint.

Diff: 650 lines (+487 / -163) across 8 files Reviewer: tlwillke Review duration: 93459m
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 #659"
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%