feat: make index granularity configurable via block_bits

Replaces the hardcoded BLOCK_SIZE constant with a configurable block_bits parameter, enabling variable index granularity to balance index size and sequential scan cost. Both the reader and writer now store block_bits and a precomputed mask for branchless offset arithmetic, while the index file format is upgraded to UIX3 to persist the configuration. Comprehensive unit tests verify serialization, chunk offset indexing, random access consistency, and kmer count accuracy across various block sizes.
This commit is contained in:
Eric Coissac committed 2026-05-23 12:57:56 +02:00
1 parent 4a5ab0b8c2
commit 8478072b78
2 files changed
+159 -42

No files matched your search

+91
View File
@@ -269,3 +269,94 @@ fn long_unitig_split_no_kmer_lost() {
// First kmer of chunk 1 = original nucl 256..260 — a different, adjacent kmer.
assert!(r.verify_canonical_kmer(1, 0, canonical_of(&seq[256..260])));
}
// ── block_bits parametrisation ────────────────────────────────────────────────
fn write_read_bb(seqs: &[&[u8]], block_bits: u8) -> (tempfile::TempDir, UnitigFileReader) {
let dir = tempdir().unwrap();
let path = dir.path().join("unitigs.bin");
let mut w = UnitigFileWriter::create_with_block_bits(&path, block_bits).unwrap();
for s in seqs {
w.write(&make_unitig(s)).unwrap();
}
w.close().unwrap();
let r = UnitigFileReader::open(&path).unwrap();
(dir, r)
}
#[test]
fn block_bits_stored_and_read_back() {
set_k(4);
for bb in [0u8, 1, 2, 3, 6] {
let dir = tempdir().unwrap();
let path = dir.path().join("unitigs.bin");
let w = UnitigFileWriter::create_with_block_bits(&path, bb).unwrap();
assert_eq!(w.block_bits(), bb);
w.close().unwrap();
let r = UnitigFileReader::open(&path).unwrap();
assert_eq!(r.block_bits(), bb, "block_bits={bb} not preserved");
}
}
#[test]
fn block_bits_zero_exact_offsets() {
// block_bits=0 → one offset per chunk, no sequential scan in chunk_start
set_k(4);
let seqs: &[&[u8]] = &[b"ACGTACGT", b"TTTTCCCC", b"GGGAAA", b"AAAACG"];
let (_dir, r) = write_read_bb(seqs, 0);
assert_eq!(r.len(), seqs.len());
for (i, s) in seqs.iter().enumerate() {
assert_eq!(r.unitig(i), make_unitig(s), "block_bits=0: unitig {i} mismatch");
}
}
#[test]
fn block_bits_one_block_size_two() {
// block_bits=1 → BLOCK_SIZE=2: every other chunk gets an offset
set_k(4);
let seqs: &[&[u8]] = &[b"ACGTACGT", b"TTTTCCCC", b"GGGAAA", b"AAAACG", b"CCCCGT"];
let (_dir, r) = write_read_bb(seqs, 1);
assert_eq!(r.len(), seqs.len());
for (i, s) in seqs.iter().enumerate() {
assert_eq!(r.unitig(i), make_unitig(s), "block_bits=1: unitig {i} mismatch");
}
}
#[test]
fn block_bits_larger_than_chunk_count() {
// block_bits=6 (BLOCK_SIZE=64) with only 3 chunks: all in block 0
set_k(4);
let seqs: &[&[u8]] = &[b"ACGTACGT", b"TTTTCCCC", b"GGGAAA"];
let (_dir, r) = write_read_bb(seqs, 6);
assert_eq!(r.len(), seqs.len());
for (i, s) in seqs.iter().enumerate() {
assert_eq!(r.unitig(i), make_unitig(s), "block_bits=6: unitig {i} mismatch");
}
}
#[test]
fn block_bits_random_access_matches_sequential() {
// Write many chunks with block_bits=2 (BLOCK_SIZE=4), verify random access
// against sequential iteration for every chunk.
set_k(4);
let seqs: Vec<Vec<u8>> = (0..20_usize)
.map(|i| (0..8_usize).map(|j| b"ACGT"[(i + j) % 4]).collect())
.collect();
let seq_refs: Vec<&[u8]> = seqs.iter().map(|s| s.as_slice()).collect();
let (_dir, r) = write_read_bb(&seq_refs, 2);
assert_eq!(r.len(), seqs.len());
let sequential: Vec<Unitig> = r.iter_chunks_sequential().map(|(_, u)| u).collect();
for i in 0..seqs.len() {
assert_eq!(r.unitig(i), sequential[i], "random vs sequential mismatch at chunk {i}");
}
}
#[test]
fn block_bits_kmer_count_preserved() {
set_k(4);
// "AAAACG" → 3 kmers, "CCCCAG" → 3 kmers; total = 6
for bb in [0u8, 1, 2, 6] {
let (_dir, r) = write_read_bb(&[b"AAAACG", b"CCCCAG"], bb);
assert_eq!(r.n_kmers(), 6, "block_bits={bb}: n_kmers mismatch");
}
}