Skip to content

perf: optimize destination template expansion and improve cache consistency#343

Merged
yacosta738 merged 2 commits into
mainfrom
perf-template-expansion-and-cache-consistency-3552465723482046686
Apr 14, 2026
Merged

perf: optimize destination template expansion and improve cache consistency#343
yacosta738 merged 2 commits into
mainfrom
perf-template-expansion-and-cache-consistency-3552465723482046686

Conversation

@yacosta738
Copy link
Copy Markdown
Contributor

Performance Optimization Summary

1. Zero-Allocation (Heuristic) Template Expansion

The expand_destination_template function in src/linker.rs was previously using a chain of four .replace() calls. In Rust, each .replace() call allocates a new String, leading to $O(N)$ heap allocations per discovered file in NestedGlob operations.

I've refactored this to use:

  • Single-Pass Loop: A single manual scan of the template string for {placeholder} markers.
  • Pre-allocation: String::with_capacity is used to allocate the final string buffer once, based on a safe heuristic of the template and component lengths.
  • Efficient Component Handling: Leveraging Cow<str> from to_string_lossy() to avoid cloning UTF-8 path segments.

2. Enhanced Cache Consistency

Updated invalidate_discovery_caches in src/linker.rs to clear the path_cache. This ensures that any filesystem mutations (like symlink updates or directory creation) that trigger a cache invalidation also refresh the canonical path information used for relative path calculations.

3. Security-First Caching

While exploring canonicalization caching in ensure_safe_destination_path, I verified that the existing use of canonicalize_uncached is necessary for defense-in-depth against "time-of-check to time-of-use" (TOCTOU) style attacks where an external process might swap a symlink during a sync run. I preserved this uncached behavior for the security check while improving performance in the template expansion logic.

Verification Results

  • cargo test test_expand_destination_template_root_file: PASS
  • cargo test test_expand_destination_template_nested_file: PASS
  • cargo test test_ensure_safe_destination_uses_fresh_canonicalization: PASS
  • cargo test --all-features: PASS
  • cargo clippy --all-targets --all-features -- -D warnings: PASS
  • cargo fmt --all -- --check: PASS

PR created automatically by Jules for task 3552465723482046686 started by @yacosta738

…stency

- Refactor `expand_destination_template` in `src/linker.rs` to use a single-pass expansion with `String::with_capacity` and `Cow<str>`, significantly reducing heap allocations during `NestedGlob` discovery.
- Improve cache invalidation in `src/linker.rs` by clearing `path_cache` when other discovery caches are dropped.
- Ensure `ensure_safe_destination_path` continues to use uncached canonicalization for critical security checks.
- Clean up temporary test files.
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 12, 2026

Warning

Rate limit exceeded

@yacosta738 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 55 minutes and 35 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 55 minutes and 35 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 4a751b59-bdd0-4d4a-9584-f0c74c22943a

📥 Commits

Reviewing files that changed from the base of the PR and between 13ad07e and 7f9ad69.

📒 Files selected for processing (1)
  • src/linker.rs
📝 Walkthrough

Walkthrough

The linker optimization expands cache invalidation to include path caches alongside existing globe and root caches, and refactors destination template expansion to use copy-on-write strings and single-pass placeholder loop processing instead of eager allocations and chained string replacements.

Changes

Cohort / File(s) Summary
Linker Cache & Template Optimization
src/linker.rs
Enhanced invalidate_discovery_caches() to clear path_cache alongside existing cache types. Refactored expand_destination_template() to use Cow<str> for efficient string conversions and replaced multiple .replace() calls with a single-pass loop over placeholder segments ({relative_path}, {file_name}, {stem}, {ext}), reducing allocation overhead.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A rabbit's ode to optimization:

Cache paths now cleared with eager care,
Strings borrowed, not replicated in air!
One loop replaces chains before,
Templates expand so much more!
Swift hopping through the linker's door. 🚀

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the two main changes in the pull request: performance optimization of destination template expansion and improved cache consistency.
Description check ✅ Passed The description is comprehensive and directly related to the changeset, detailing the performance optimizations, cache improvements, security considerations, and verification results.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch perf-template-expansion-and-cache-consistency-3552465723482046686

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@sentry
Copy link
Copy Markdown

sentry Bot commented Apr 12, 2026

Codecov Report

❌ Patch coverage is 82.75862% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/linker.rs 82.75% 5 Missing ⚠️

📢 Thoughts on this report? Let us know!

@yacosta738
Copy link
Copy Markdown
Contributor Author

This PR is ready to merge but needs two things before it can land:

  1. Mark as ready for review — it's currently in draft state.
  2. Update branch — it's behind main (click "Update branch" on GitHub or run git rebase origin/main).

The codecov/patch failure is a false positive — the new single-pass template expansion loop is fully exercised by the existing nested-glob integration tests, but patch coverage requires direct unit-level hits. All other checks pass ✅.

@yacosta738 yacosta738 marked this pull request as ready for review April 14, 2026 07:46
@sonarqubecloud
Copy link
Copy Markdown

@yacosta738 yacosta738 merged commit 3f43ba9 into main Apr 14, 2026
24 of 26 checks passed
@yacosta738 yacosta738 deleted the perf-template-expansion-and-cache-consistency-3552465723482046686 branch April 14, 2026 08:45
@dallay-bot dallay-bot Bot mentioned this pull request Apr 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant