rust-guard: use Option<&'static str> for items_path and Cow<'_, str> for baseline_scope#5606
Merged
Merged
Conversation
- Change PathLabelResult.items_path from Option<String> to Option<&'static str> in response_paths.rs, eliminating 3 heap allocations per collection labeling call - Update PathLabeledOutput.items_path in lib.rs to match - Fix search_repositories branch (line 105) to use static match instead of format! - Update test assertions in mod.rs to use &'static str literals - Change baseline_scope in apply_tool_labels (tool_rules.rs) from String to Cow<'_, str>: static constant assignments use Cow::Borrowed (zero-cost), owned assignments (owner, s_repo_id) use Cow::Owned, eliminating up to 8 heap allocations per apply_tool_labels invocation
Copilot
AI
changed the title
[WIP] Change PathLabelResult.items_path to Option<&'static str> and use Cow for baseline_scope
rust-guard: use May 13, 2026
Option<&'static str> for items_path and Cow<'_, str> for baseline_scope
Contributor
There was a problem hiding this comment.
Pull request overview
This PR reduces per-call heap allocations in the Rust GitHub guard’s hot-path labeling by switching items_path to a static string reference and baseline_scope to a borrow-friendly Cow<str>, improving WASM performance.
Changes:
- Change
PathLabelResult.items_path/PathLabeledOutput.items_pathfromOption<String>toOption<&'static str>and adjust path construction accordingly. - Change
baseline_scopeinapply_tool_labelsfromStringtoCow<'_, str>to avoid repeated.to_string()allocations for constant scopes. - Update unit tests to match the new
items_pathtype and values.
Show a summary per file
| File | Description |
|---|---|
| guards/github-guard/rust-guard/src/lib.rs | Updates the serialized path-label output struct to carry items_path as Option<&'static str>. |
| guards/github-guard/rust-guard/src/labels/tool_rules.rs | Switches baseline_scope to Cow<str> to reduce allocations when scoping integrity baselines. |
| guards/github-guard/rust-guard/src/labels/response_paths.rs | Converts computed items_path values to &'static str and replaces a format! with a static match for search repos. |
| guards/github-guard/rust-guard/src/labels/mod.rs | Updates tests to assert items_path as Option<&'static str> (no .to_string()). |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comments suppressed due to low confidence (1)
guards/github-guard/rust-guard/src/labels/tool_rules.rs:600
baseline_scope = Cow::Owned(owner.clone())still clones the owner string. Sinceowneroutlivesbaseline_scopefor the rest ofapply_tool_labels, this can beCow::Borrowed(owner.as_str())to avoid the extra allocation on this hot path.
// Projects are org-scoped; write responses carry the same labels as reads.
// I = approved:<owner>
if !owner.is_empty() {
baseline_scope = Cow::Owned(owner.clone());
integrity = writer_integrity(&baseline_scope, ctx);
- Files reviewed: 4/4 changed files
- Comments generated: 2
Comment on lines
406
to
409
| let (s_owner, s_repo, s_repo_id) = resolve_search_scope(tool_args, &owner, &repo); | ||
| if !s_repo_id.is_empty() { | ||
| baseline_scope = s_repo_id.clone(); | ||
| baseline_scope = Cow::Owned(s_repo_id.clone()); | ||
| desc = format!("search_code:{}", s_repo_id); |
| // label_response_paths for list_project_items | ||
| if !owner.is_empty() { | ||
| baseline_scope = owner.clone(); | ||
| baseline_scope = Cow::Owned(owner.clone()); |
Collaborator
|
@copilot address review feedback |
- search_code branch: reorder assignments so s_repo_id is moved into Cow::Owned after its last use, avoiding an extra .clone() - projects read/write branches: change Cow::Owned(owner.clone()) to Cow::Borrowed(owner.as_str()) since owner lives for the full duration of apply_tool_labels — zero allocation on these paths
Contributor
Author
Done in 0f1e9bd:
|
This was referenced May 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
✨ Enhancement
items_pathandbaseline_scopein the hot-path labeling code were declared asString/Option<String>despite always holding either&'static strconstants or short-lived borrows, causing unnecessary heap allocations on every labeling call — especially costly in WASM.What does this improve?
items_path(response_paths.rs,lib.rs):PathLabelResult.items_pathandPathLabeledOutput.items_pathchanged fromOption<String>toOption<&'static str>. All values originate fromextract_items_array(returns&'static str) or static literals. Eliminates 3 allocations per collection labeling call (list_pull_requests,list_issues,list_project_items).baseline_scope(tool_rules.rs): Changed fromStringtoCow<'_, str>. The 8 assignments fromscope_names::USER/scope_names::GITHUBconstants becomeCow::Borrowed(zero-cost); the 3 assignments from runtime-owned strings (owner,s_repo_id) useCow::Owned. Call site (ensure_integrity_baseline(&baseline_scope, ...)) is unchanged. Eliminates up to 8 allocations perapply_tool_labelsinvocation on the most common tool paths.Implementation approach:
One additional fix in
response_paths.rs: thesearch_repositoriesbranch was usingformat!("/{}", items_key)to construct the path. Sinceitems_keyis always"items"or"repositories", this is replaced with a staticmatchto stay consistent with the&'static strtype.