Repository navigation
perf(bam): reuse record buffers and decompress on helper threads in the noodles shim - #209
Merged
ewels merged 4 commits intoDec 2, 2026
Conversation
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
approved these changes
Dec 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
RecordBuf/Recordfor every record (~2x the allocations per record of htslib);set_threadswas a no-op, so BGZF blocks were inflated inline on each worker, where htslib gives each reader its own decompression thread;Three commits, one per cause:
perf(bam): reuse the record buffer across reads—fill_from_bam/fill_from_alignmentfill the caller'sRecordin place, reusing its vectors;perf(bam): drop the per-record quality and CIGAR copies—qual()borrows from theRecordBuf,cigar()returns a borrowed view;perf(bam): honour set_threads with noodles' multithreaded BGZF reader—set_threads(N)switches the reader tobgzf::io::MultithreadedReaderwith 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:
Peak memory is unchanged by these commits (still ~30% below main). Also:
cargo test --releaseon 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