rust-guard: remove hot-path scope/integrity allocations via Cow<str> and zero-alloc rank matching#5754
Merged
Conversation
Copilot
AI
changed the title
[WIP] Eliminate String allocations in policy_scope_token and integrity_rank_normalized
rust-guard: remove hot-path scope/integrity allocations via May 15, 2026
Cow<str> and zero-alloc rank matching
Contributor
There was a problem hiding this comment.
Pull request overview
This PR reduces allocation overhead in the Rust guard labeling path by using Cow<str> for scope tokens/normalization and avoiding formatted label construction in common integrity rank comparisons.
Changes:
- Converts scope token and normalization helpers to return borrowed values when possible.
- Adds allocation-free integrity label matching for empty/single-scope cases.
- Updates label agent borrowing flow and adds focused helper tests.
Show a summary per file
| File | Description |
|---|---|
guards/github-guard/rust-guard/src/lib.rs |
Moves scope token derivation after PolicyContext construction to support borrowing from context scopes. |
guards/github-guard/rust-guard/src/labels/helpers.rs |
Introduces Cow-based scope helpers, optimized rank matching, and tests for borrowing/matching behavior. |
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: 1
Comment on lines
+1312
to
+1314
| } else if scope.contains('|') { | ||
| // Multi-scope uses canonical "integrity=<base>;scopes=..." encoding. | ||
| label == format_integrity_label(prefix, scope, base) |
Collaborator
|
@copilot address review feedback |
Contributor
Author
Addressed in commit I moved multi-scope handling out of the per-label predicate so canonical formatting is done once per rank and then compared across labels, and updated tests accordingly. |
This was referenced May 15, 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
policy_scope_tokenandnormalize_scopewere allocating on common single-scope/fallthrough paths, andintegrity_rank_normalizedwas allocating per integrity level just to compare labels. This change removes those avoidable allocations in the main integrity-labeling path while preserving existing label semantics.What does this improve?
policy_scope_tokennow returnsCow<'_, str>:""normalize_scopenow returnsCow<'a, str>, borrowingscopewhen no scoped replacement is needed instead of cloning.integrity_rank_normalizednow useslabel_matches_normalized(...)instead of constructing formatted labels for each rank.label_agent, scope token derivation was moved to read fromctx.scopesafter context construction to satisfy borrowing with the newCow-based flow.Why is this valuable?
reader/writer/merged/none/max/cappaths), especially in the dominant single-scope case and rank comparisons.Implementation approach:
Cowreturn types for scope helpers and propagated usage through existing integrity builders without behavior changes.policy_scope_tokenbehavior,normalize_scopefallthrough behavior,label_matches_normalizedparity across empty/single/multi-scope forms.