Skip to content

Conversation

@bmesuere
Copy link
Member

@bmesuere bmesuere commented Feb 2, 2026

This pull request replaces manual vector initialization with vec![value; count] for alpha and path.

Benchmark on my laptop:

Benchmark: Short reads (NC_000913-454.fna)
Benchmark 1: /Users/bart/Code/FragGeneScanRs/target/release/FragGeneScanRs -s /Users/bart/Code/FragGeneScanRs/example/NC_000913-454.fna -t 454_10 -w 0 -o /var/folders/j3/38fskpy159v07np8syk3p_2m0000gn/T/tmp.Bw0QpkNK1h/NC_000913-454
  Time (mean ± σ):     450.7 ms ±   7.4 ms    [User: 437.3 ms, System: 10.1 ms]
  Range (min … max):   441.8 ms … 476.7 ms    20 runs


Benchmark: Complete genome (NC_000913.fna)
Benchmark 1: /Users/bart/Code/FragGeneScanRs/target/release/FragGeneScanRs -s /Users/bart/Code/FragGeneScanRs/example/NC_000913.fna -t complete -w 1 -o /var/folders/j3/38fskpy159v07np8syk3p_2m0000gn/T/tmp.Bw0QpkNK1h/NC_000913
  Time (mean ± σ):     626.8 ms ±   3.9 ms    [User: 509.3 ms, System: 111.1 ms]
  Range (min … max):   620.5 ms … 635.1 ms    20 runs


Benchmark: Long reads (contigs.fna)
Benchmark 1: /Users/bart/Code/FragGeneScanRs/target/release/FragGeneScanRs -s /Users/bart/Code/FragGeneScanRs/example/contigs.fna -t complete -w 1 -o /var/folders/j3/38fskpy159v07np8syk3p_2m0000gn/T/tmp.Bw0QpkNK1h/contigs
  Time (mean ± σ):      3.937 s ±  0.009 s    [User: 3.880 s, System: 0.050 s]
  Range (min … max):    3.928 s …  3.952 s    10 runs

short reads: 450.7 ms ± 7.4 ms
complete genome: 626.8 ms ± 3.9 ms
long reads: 3.937 s ± 0.009 s

See #17 for a comparison.

Copilot AI review requested due to automatic review settings February 2, 2026 12:59
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Replaces manual Vec::with_capacity + push loops with vec![value; count] initialization for the alpha and path buffers in the Viterbi forward pass.

Changes:

  • Pre-initialize alpha as vec![[0.0; State::COUNT]; seq.len()].
  • Pre-initialize path as vec![[Some(State::S); State::COUNT]; seq.len()].
  • Remove the manual for _ in 0..seq.len() initialization loop.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +30 to +32
let mut alpha: Vec<[f64; hmm::State::COUNT]> = vec![[0.0; hmm::State::COUNT]; seq.len()];
let mut path: Vec<[Option<hmm::State>; hmm::State::COUNT]> =
vec![[Some(hmm::State::S); hmm::State::COUNT]; seq.len()];
Copy link

Copilot AI Feb 2, 2026

Choose a reason for hiding this comment

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

forward() allocates alpha/path using seq.len(), but the function later unconditionally indexes alpha[0] and seq[0..=2] (and writes to alpha[1]/alpha[2]). Since the caller currently only guards is_empty(), inputs with length 1–2 will panic. Consider adding an early return/guard for seq.len() < 3 (either here or in viterbi()/caller) so short records are handled safely.

Copilot uses AI. Check for mistakes.
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a valid comment, but was also valid for the original code. I fixed it at the caller side in FragGeneScanRs.rs.

Copy link

@ninewise ninewise left a comment

Choose a reason for hiding this comment

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

Doesn't seem to make a time difference. Whichever way you find more readable.

Benchmark 1: ./benchmark-1 -s example/NC_000913.fna -t complete -w 1 -o /tmp/asdf/NC_000913
  Time (mean ± σ):      1.469 s ±  0.026 s    [User: 0.918 s, System: 0.543 s]
  Range (min … max):    1.432 s …  1.509 s    10 runs
 
Benchmark 2: ./benchmark-2 -s example/NC_000913.fna -t complete -w 1 -o /tmp/asdf/NC_000913
  Time (mean ± σ):      1.487 s ±  0.103 s    [User: 0.927 s, System: 0.553 s]
  Range (min … max):    1.351 s …  1.587 s    10 runs

Summary
  ./benchmark-1 -s example/NC_000913.fna -t complete -w 1 -o /tmp/asdf/NC_000913 ran
    1.01 ± 0.07 times faster than ./benchmark-2 -s example/NC_000913.fna -t complete -w 1 -o /tmp/asdf/NC_000913

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.

3 participants