Skip to content

perf(bam): reuse record buffers and decompress on helper threads in the noodles shim - #209

Merged
ewels merged 4 commits into
seqeralabs:noodles-migration-redofrom
rhassaine:perf/118-shim-alloc
Dec 2, 2026
Merged

ewels merged 4 commits into
seqeralabs:noodles-migration-redofrom
rhassaine:perf/118-shim-alloc

Conversation

@rhassaine

Copy link
Copy Markdown
Contributor

Closes the 4-thread gap from the earlier benchmarks. Profiling (perf, chr1/2/11/12 slice of the HCC1395 BAM) showed #118 doing about the same total CPU as main but finishing later, because:

  • the shim rebuilt a RecordBuf/Record for every record (~2x the allocations per record of htslib);
  • set_threads was a no-op, so BGZF blocks were inflated inline on each worker, where htslib gives each reader its own decompression thread;
  • quality and CIGAR were copied out on every access.

Three commits, one per cause:

  • perf(bam): reuse the record buffer across reads — fill_from_bam / fill_from_alignment fill the caller's Record in place, reusing its vectors;
  • perf(bam): drop the per-record quality and CIGAR copies — qual() borrows from the RecordBuf, cigar() returns a borrowed view;
  • perf(bam): honour set_threads with noodles' multithreaded BGZF reader — set_threads(N) switches the reader to bgzf::io::MultithreadedReader with N inflater threads, resuming at the current virtual position.

Full HCC1395 BAM (115M records), release builds (LTO, CGU=1, x86-64-v3) on c2d-highcpu-32, median of 3 runs, outputs identical to main in every run:

4 threads 12 threads
main (rust-htslib) 528.8 s 286.2 s
#118 as is 546.7 s (+3.4%) 266.0 s (−7.1%)
with these commits 517.5 s (−2.1%) 259.9 s (−9.2%)

Peak memory is unchanged by these commits (still ~30% below main). Also: cargo test --release on stable and 1.89, clippy and fmt pass; the 8 nf-core datasets in RustQC-benchmarks give identical output to main at 1 and 6 threads.

🤖 Generated with Claude Code

rhassaine and others added 4 commits December 1, 2026 16:56
The noodles shim rebuilt a fresh Record (RecordBuf plus packed sequence,
quality and CIGAR caches) for every read and dropped the previous one, which
cost roughly 28 extra malloc calls per record versus the htslib path.

Fill the caller's Record in place instead: RecordBuf::try_clone_from_alignment_record
already reuses the name, CIGAR, sequence, quality and data vectors, and the
packed-sequence, quality and decoded-CIGAR caches are now cleared and refilled
rather than reallocated. Output is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Record::qual() now borrows the quality scores held by the underlying
RecordBuf instead of a second cached copy, and Record::cigar() returns a
CigarStringView that borrows the decoded CIGAR rather than cloning the vector
on every call (it is called several times per read by the accumulators).
The packed 4-bit sequence is copied straight from the BAM record into a
reused buffer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
set_threads() was a no-op for indexed BAM readers, so every counting worker
inflated BGZF blocks inline on its own thread, while the htslib build gave
each worker a dedicated decompression pool.

BAM readers now wrap their BGZF stream in an enum that is either the inline
reader or noodles' MultithreadedReader (one helper thread reads raw blocks,
N threads inflate). set_threads(N) switches at the current virtual position,
so an already-read header is kept; set_threads(0) goes back to inline. The
BAM index is loaded separately, because noodles' IndexedReader is tied to the
inline BGZF reader. The per-worker thread count chosen in the counting loop
is unchanged (at least one decompression thread per worker), and CRAM and SAM
inputs are unaffected. Output is identical to the inline path at 1, 4 and 8
threads on the bundled test data.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ewels
ewels merged commit 9d6c167 into seqeralabs:noodles-migration-redo Dec 2, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants