Skip to content

Generate time and memory improvements - #16

Merged
alex-sandercock merged 3 commits into
developmentfrom
generate-time-and-memory
Dec 3, 2026
Merged

alex-sandercock merged 3 commits into
developmentfrom
generate-time-and-memory

Conversation

@alex-sandercock

Copy link
Copy Markdown
Contributor

This pull request introduces several performance optimizations and memory improvements to the core pipeline, especially in the handling of gapped alignment, primer matching, and N-tag resolution. It also updates documentation to reflect these improvements and adds new tests to ensure correctness of shortcut paths that save time or memory.

Performance and memory improvements:

  • Optimized gapped alignment: The gapped_prescreen function now accepts an enough parameter, allowing early termination when the lower bound is already within the threshold, and caches design pattern masks for faster repeated use. This change speeds up edit distance checks and reduces redundant computation. [1] [2] [3] [4]
  • Enhanced primer matching: The PrimerIndex.match method now memoizes search states after a fixed number of leading bases, enabling shared computation across reads from the same amplicon and reducing per-sample memory usage. [1] [2] [3]
  • Improved N-tag resolution: The new _n_tag_fits function and revised resolve_n_tags logic more efficiently assign ambiguous tags to concrete sequences, distinguishing between plain and coded pools and using stretch-based comparison where possible. [1] [2]
  • Memory-efficient sample processing: The sample worker now conditionally omits quality tallies from results unless needed, reducing memory footprint for large cohorts.

Documentation and testing:

  • Updated documentation: The DEVELOPMENT.md and changelog.md files now reflect the improved run times, memory usage, and the correctness of shortcut paths, with new sections describing the rationale and measurements behind the changes. [1] [2] [3]
  • Expanded test coverage: Added a new test suite, test_same_answers.py, that verifies each shortcut path produces the same results as the plain computation, increasing the total number of tests and files. [1] [2]

These changes collectively result in 41–47% faster execution on key cohorts, a significant reduction in memory usage, and improved maintainability and correctness of the codebase.

Three things took most of the read pass, and each now does less work
for the same answer:

- A tag with an N is compared with a sequence in the stretches between
  its N's, as whole strings (`_n_tag_fits`).
- The gapped prescreen makes a design tag's bit masks once, stops at
  the first of its two passes that settles the question, and
  `_gapped_edits` skips the Alt tag when the Ref tag's bound is already
  too far.
- `PrimerIndex.match` takes its search up from its state after 12
  bases, which reads of one amplicon share.

Most of a run's memory is sequences seen once or twice:

- `apply_partial_reads` adds partial reads without copying a sample's
  tables.
- Protection is decided from the sequences that reach a route's read
  count in some sample (`_could_pass`). No other sequence can pass.
- `finalize_counts` lets each sample as read go once it is denoised.
- The per-sequence quality tallies stay in the worker unless the
  quality route is on. They are still counted and cached.

With 8 jobs: pecan (376 samples) 1,376 s to 657 s and 15.9 GB to 7.0 GB,
alfalfa (356) 464 s to 262 s and 9.4 GB to 3.8 GB, sweetpotato (80)
381 s to 204 s and 3.4 GB to 1.75 GB.  Memory is the main process's
peak footprint.  On all three the table, the variants sidecar and the
QC JSON are the same byte for byte, and so are the read-pass caches.

`tests/test_same_answers.py` sets each shortcut beside the plain
computation it stands in for.
Both sides add an argument to the sample workers: development the gzip
level of the caches, this branch whether the quality tallies come back.
The workers now carry both.
The primer search no longer keeps a read start that leads to no primer.
Reads that match no primer share few starts, and keeping theirs added
about 100 MB to a worker on a sample of random reads (189 MB against
86 MB). On real samples the store is now 5 MB to 13 MB.

The gapped prescreen runs the design tag's pass first, because its bit
masks are cached. No decision changes on 136,628 calls recorded from one
pecan, one alfalfa and one sweetpotato sample.

collect_evidence has one docstring, finalize_counts says its list must be
mutable, and _n_tag_fits says it expects one length.

Five new tests: the primer search beside a primer-by-primer comparison,
the store without dead starts, an emptied store, release across worker
processes, and Alt tags several bases from the Ref tag. The quality-tally
test now also reads a cache written by a run that returned no tallies.

The three cohorts' tables, variants sidecars and cache contents are the
same as before these changes, and the QC files are the same apart from
the time stamp and the output paths. The development record now says so,
gives back-to-back times against 0.13.0 for sweetpotato and alfalfa, and
gives the workers' memory.
@alex-sandercock alex-sandercock self-assigned this Dec 3, 2026
Copilot AI balanced review requested due to automatic review settings December 3, 2026 18:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues for the pipeline optimization changes.

No open issues 🎉

What changed in this PR

Optimizes the generate pipeline to reduce runtime and memory use while preserving results.

Changes:

  • Speeds up gapped alignment, primer matching, and N-tag resolution.
  • Reduces copying and retained sample data during processing.
  • Adds equivalence tests and documents performance measurements.
File Description
tests/​test_same_answers.py Adds 17 tests comparing shortcuts with baseline behavior.
madcats.py Implements computation shortcuts and memory-saving processing.
docs/​src/​development/​changelog.md Reports measured runtime improvements.
docs/​DEVELOPMENT.md Documents optimizations, benchmarks, and expanded test coverage.

Review effort: Balanced

@alex-sandercock
alex-sandercock merged commit f1378da into development Dec 3, 2026
17 checks passed
@alex-sandercock
alex-sandercock deleted the generate-time-and-memory branch December 3, 2026 18:54
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