Skip to content

perf: cache consensus.size() in loops - #36

Merged
nh13 merged 1 commit into
mainfrom
fix/35-cache-size
Dec 26, 2025
Merged

perf: cache consensus.size() in loops#36
nh13 merged 1 commit into
mainfrom
fix/35-cache-size

Conversation

@nh13

@nh13 nh13 commented Dec 22, 2025

Copy link
Copy Markdown
Owner

Summary

Cache consensus.size() in a local variable instead of calling it repeatedly.

Background

From code review (research.md §2.2, LOW severity):
Multiple calls to consensus.size() in the same scope. While the compiler may optimize this, explicitly caching is clearer.

Changes

  • src/caller.cpp: Add const size_t consensus_len = consensus.size() and use it in all loops

Test plan

  • CI workflow passes
  • Coverage output unchanged

Closes #35

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Refactor
    • Optimized internal performance to reduce redundant computations.
    • Enhanced error detection with warnings for edge case handling to improve robustness.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai

coderabbitai Bot commented Dec 22, 2025

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

The pull request optimizes performance in src/caller.cpp by caching the consensus string length in a local variable instead of repeatedly calling consensus.size() in loops. Additionally, bounds checking is introduced to detect and warn when computed coverage indices fall outside vector bounds during pairwise MSA operations, while preserving existing behavior for valid indices.

Changes

Cohort / File(s) Summary
Consensus length caching and bounds checking
src/caller.cpp
Caches consensus.size() as consensus_len and replaces all loop-based calls to consensus.size(); adds warning-level bounds validation for coverage vector access with out-of-bounds index detection during pairwise MSA adjustments.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Poem

🐰 A rabbit hops through loops so fast,

With cached consensus, at long last!

No size() calls to slow the way,

And bounds checks guard us every day! 🎯

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: caching consensus.size() in loops for performance optimization.
Linked Issues check ✅ Passed The pull request fully implements the requirement from issue #35 to cache consensus.size() in a local variable and use it throughout loops.
Out of Scope Changes check ✅ Passed All changes are scoped to the objective of caching consensus.size(). The addition of out-of-bounds warning handling is a defensive measure to ensure correctness.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/35-cache-size

Comment @coderabbitai help to get the list of available commands and usage tips.

@nh13
nh13 force-pushed the fix/33-remove-string-copy branch from 3eb033c to da9eb17 Compare December 22, 2025 23:07
@nh13
nh13 force-pushed the fix/35-cache-size branch from 79738a4 to 82e67e9 Compare December 22, 2025 23:07
@nh13
nh13 force-pushed the fix/33-remove-string-copy branch from da9eb17 to 4184d2b Compare December 22, 2025 23:10
@nh13
nh13 force-pushed the fix/35-cache-size branch from 82e67e9 to 57e9e99 Compare December 22, 2025 23:10
@nh13
nh13 force-pushed the fix/33-remove-string-copy branch from 4184d2b to a12408e Compare December 22, 2025 23:13
@nh13
nh13 force-pushed the fix/35-cache-size branch from 57e9e99 to e882d1a Compare December 22, 2025 23:13
@nh13
nh13 force-pushed the fix/33-remove-string-copy branch from a12408e to 17442ec Compare December 22, 2025 23:22
@nh13
nh13 force-pushed the fix/35-cache-size branch from e882d1a to f015f56 Compare December 22, 2025 23:22
@nh13
nh13 force-pushed the fix/33-remove-string-copy branch from 17442ec to 9977f4e Compare December 22, 2025 23:23
@nh13
nh13 force-pushed the fix/35-cache-size branch from f015f56 to 2dfd5a7 Compare December 22, 2025 23:23
@nh13
nh13 force-pushed the fix/33-remove-string-copy branch from 9977f4e to 25ce8e6 Compare December 22, 2025 23:24
@nh13
nh13 force-pushed the fix/35-cache-size branch from 2dfd5a7 to 54a92c3 Compare December 22, 2025 23:24
@nh13

nh13 commented Dec 23, 2025

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Dec 23, 2025

Copy link
Copy Markdown
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@nh13
nh13 force-pushed the fix/33-remove-string-copy branch from 25ce8e6 to b72c119 Compare December 25, 2025 06:09
Base automatically changed from fix/33-remove-string-copy to main December 25, 2025 06:24
Store consensus.size() in a const local variable instead of calling
it repeatedly in loop conditions and index calculations.

Closes #35

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@nh13
nh13 force-pushed the fix/35-cache-size branch from 54a92c3 to 42c87b0 Compare December 25, 2025 06:26
@nh13

nh13 commented Dec 26, 2025

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Dec 26, 2025

Copy link
Copy Markdown
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/caller.cpp (1)

221-221: Consider using size_t for loop variable consistency.

The loop variable j is uint32_t, but consensus_len is size_t. While biological sequences are unlikely to exceed UINT32_MAX in practice, using size_t would be more consistent and correct.

🔎 Proposed fix for type consistency
-            for (uint32_t j = 0; j < consensus_len; ++j) {
+            for (size_t j = 0; j < consensus_len; ++j) {
                 fprintf(stdout, ",%u", coverage[i * consensus_len + j]);

The same change applies to line 228.

📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0c6187f and 42c87b0.

📒 Files selected for processing (1)
  • src/caller.cpp
🔇 Additional comments (3)
src/caller.cpp (3)

203-203: LGTM! Effective performance optimization.

Caching consensus.size() in a local variable eliminates repeated method calls and makes the intent clearer, aligning with the PR objective.


206-215: LGTM! Good defensive programming.

The loop correctly uses the cached consensus_len value, and the bounds checking at lines 209-214 adds valuable safety by detecting and warning about out-of-bounds indices while preserving the existing behavior for valid cases.


228-229: Same type consistency consideration applies here.

As noted in the previous comment for line 221, using size_t instead of uint32_t for the loop variable j would improve consistency with consensus_len.

@nh13
nh13 merged commit 45bb8a9 into main Dec 26, 2025
2 checks passed
@nh13
nh13 deleted the fix/35-cache-size branch December 26, 2025 06:31
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.

Cache consensus.size() in loops

1 participant