Skip to content

rust-guard: eliminate hot-path allocations in file secrecy and baseline scope inference#5679

Merged
lpcox merged 5 commits into
mainfrom
copilot/rust-guard-eliminate-heap-allocations
May 14, 2026
Merged

rust-guard: eliminate hot-path allocations in file secrecy and baseline scope inference#5679
lpcox merged 5 commits into
mainfrom
copilot/rust-guard-eliminate-heap-allocations

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 14, 2026

✨ Enhancement

This change removes two avoidable heap allocations in Rust Guard hot paths used during tool labeling. It keeps behavior unchanged while reducing per-call overhead in get_file_contents secrecy checks and baseline scope inference.

What does this improve?
Lower allocation pressure in frequently executed labeling paths.

Why is this valuable?
These paths run on nearly every label_resource / label_response dispatch, so small per-call savings compound quickly.

Implementation approach:

  • check_file_secrecy now uses lazy path iteration (labels/tool_rules.rs)
    • Removed Vec<&str> collection from path_lower.split('/').collect().
    • Replaced segment checks with path_lower.split('/').any(...).
    • Replaced filename extraction via last() with path_lower.rsplit('/').next().
  • infer_scope_for_baseline now returns Cow<'_, str> (lib.rs)
    • Switched return type from String to Cow<'_, str>.
    • Common branches now borrow (repo_id, scope_names::GITHUB, and empty scope).
    • Only search_* query extraction returns owned data.
  • Focused test updates
    • Added branch-level assertions for Cow::Borrowed/Cow::Owned behavior in infer_scope_for_baseline.
fn infer_scope_for_baseline<'a>(
    tool_name: &str,
    tool_args: &Value,
    repo_id: &'a str,
) -> Cow<'a, str> {
    if !repo_id.is_empty() {
        return Cow::Borrowed(repo_id);
    }

    match tool_name {
        "create_repository" | "fork_repository" => Cow::Borrowed(scope_names::GITHUB),
        "search_code" | "search_issues" | "search_pull_requests" => {
            let query = tool_args.get("query").and_then(|v| v.as_str()).unwrap_or("");
            let (_, _, repo_from_query) = extract_repo_info_from_search_query(query);
            Cow::Owned(repo_from_query)
        }
        _ => Cow::Borrowed(""),
    }
}

Copilot AI changed the title [WIP] Fix avoidable heap allocations in check_file_secrecy rust-guard: eliminate hot-path allocations in file secrecy and baseline scope inference May 14, 2026
Copilot AI requested a review from lpcox May 14, 2026 13:31
Copilot finished work on behalf of lpcox May 14, 2026 13:31
@lpcox lpcox marked this pull request as ready for review May 14, 2026 13:38
Copilot AI review requested due to automatic review settings May 14, 2026 13:38
Copy link
Copy Markdown
Contributor

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

This PR reduces allocations in Rust Guard labeling hot paths by using borrowed scope strings where possible and avoiding segment collection during file secrecy checks.

Changes:

  • infer_scope_for_baseline now returns Cow<str> and borrows common baseline scopes.
  • File secrecy path checks avoid collecting split segments into a Vec.
  • Tests add assertions for borrowed vs owned baseline inference behavior.
Show a summary per file
File Description
guards/github-guard/rust-guard/src/lib.rs Updates baseline scope inference to use Cow and adds branch-level tests.
guards/github-guard/rust-guard/src/labels/tool_rules.rs Replaces eager path segment collection with lazy path splitting.

Copilot's findings

Tip

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

  • Files reviewed: 2/2 changed files
  • Comments generated: 2

Comment thread guards/github-guard/rust-guard/src/labels/tool_rules.rs Outdated
Comment thread guards/github-guard/rust-guard/src/lib.rs Outdated
lpcox and others added 2 commits May 14, 2026 06:41
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@lpcox lpcox merged commit 1056f46 into main May 14, 2026
12 checks passed
@lpcox lpcox deleted the copilot/rust-guard-eliminate-heap-allocations branch May 14, 2026 13:44
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.

[rust-guard] Rust Guard: Eliminate two avoidable heap allocations in hot paths

3 participants