Skip to content

Commit 020e25b

Browse files
committed
final nt conversion mem spike fix
1 parent c90c1cc commit 020e25b

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

‎src/nt.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ impl Hdt {
5656
let num_triples = encoded_triples.len();
5757
// Sort by final HDT ID (SPO order) before feeding into TriplesBitmap.
5858
encoded_triples.par_sort_unstable();
59-
let triples = TriplesBitmap::from_triples(&encoded_triples);
59+
// Move (don't borrow) the encoded triples in so TriplesBitmap can free
60+
// them after its build loop, before its op-index peak.
61+
let triples = TriplesBitmap::from_triples(encoded_triples);
6062

6163
let header = Header { format: "ntriples".to_owned(), length: 0, body: BTreeSet::new() };
6264
let mut hdt = Hdt { header, dict, triples };

‎src/triples.rs‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -477,8 +477,13 @@ impl TriplesBitmap {
477477
Self { order, bitmap_y: InMemoryBitmap::new(bitmap_y), adjlist_z, op_index, wavelet_y }
478478
}
479479

480-
/// Creates a new TriplesBitmap from a list of sorted RDF triples
481-
pub fn from_triples(triples: &[TripleId]) -> Self {
480+
/// Creates a new TriplesBitmap from a list of sorted RDF triples.
481+
///
482+
/// Takes the triples by value so the encoded-triple buffer (24 B ×
483+
/// num_triples) can be freed as soon as the y/z arrays and bitmaps are
484+
/// built, instead of staying resident through the memory-heavy op-index
485+
/// construction in [`TriplesBitmap::new`].
486+
pub fn from_triples(triples: Vec<TripleId>) -> Self {
482487
let mut y_bitmap = BitVectorMut::new();
483488
let mut z_bitmap = BitVectorMut::new();
484489
let mut array_y = Vec::new();
@@ -515,6 +520,10 @@ impl TriplesBitmap {
515520
last_y = y;
516521
last_z = z;
517522
}
523+
// The encoded triples have been fully read into the y/z arrays and
524+
// bitmaps; free them (~24 B × num_triples) before the op-index build
525+
// peak in TriplesBitmap::new rather than holding them until return.
526+
drop(triples);
518527
y_bitmap.push(true);
519528
let n = y_bitmap.len();
520529
y_bitmap.extend_with_zeros(n.div_ceil(64) * 64 - n);
@@ -523,6 +532,10 @@ impl TriplesBitmap {
523532
let bitmap_z = Bitmap::from(z_bitmap);
524533
let sequence_y = Sequence::new(&array_y);
525534
let sequence_z = Sequence::new(&array_z);
535+
// The plain usize arrays are now redundant with the bit-packed
536+
// sequences; free them (~8 B × len each) before the op-index build.
537+
drop(array_y);
538+
drop(array_z);
526539
let adjlist_z = AdjList::new(sequence_z, bitmap_z);
527540
TriplesBitmap::new(Order::SPO, &sequence_y, bitmap_y, adjlist_z)
528541
}

0 commit comments

Comments
 (0)