[Repo Assist] perf(rust-guard): eliminate ctx.clone() and use &'static str in NormalizedPolicy#4394
Open
github-actions[bot] wants to merge 1 commit intomainfrom
Conversation
…lizedPolicy Implements two improvements identified in issue #4380: 1. Eliminate ctx.clone() in label_agent: compute integrity labels before calling set_runtime_policy_context so ctx can be moved rather than cloned. PolicyContext holds eight heap-allocated Vec/String fields; deep-copying it on every label_agent call was pure overhead. 2. Use &'static str for NormalizedPolicy fields: both scope_kind and min_integrity are always compile-time constants. Add ScopeKind::as_str() alongside the existing Display impl (Display now delegates to as_str), change normalized_scope_kind() to return &'static str, and update NormalizedPolicy struct accordingly. Removes three unnecessary heap allocations per label_agent call. Closes #4380 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR removes avoidable heap allocations in the Rust GitHub guard’s label_agent FFI entrypoint by (1) eliminating a deep PolicyContext clone and (2) representing normalized policy string fields as compile-time constants.
Changes:
- Reordered
label_agentto compute integrity labels before setting the runtime global policy context, allowingPolicyContextto be moved instead of cloned. - Changed
NormalizedPolicy.scope_kindandNormalizedPolicy.min_integrityfromStringto&'static strto avoid per-call allocations. - Added
ScopeKind::as_str()and updated itsDisplayimplementation to reuse that canonical mapping.
Show a summary per file
| File | Description |
|---|---|
| guards/github-guard/rust-guard/src/lib.rs | Removes ctx.clone() by reordering integrity computation and switches normalized policy fields to &'static str. |
| guards/github-guard/rust-guard/src/labels/helpers.rs | Adds ScopeKind::as_str() and updates Display to delegate to it (avoids duplication and allocations). |
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: 0
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.
🤖 This PR was created by Repo Assist, an automated AI assistant.
Closes #4380
Root Cause
label_agent— the first FFI call on every gateway request — contained two unnecessary heap-allocation hotspots:ctx.clone():set_runtime_policy_context(ctx.clone())deep-copied the entirePolicyContext(eightVec/Stringfields) before theintegritymatch block usedctxwith only shared borrows. The only reason for the clone was ordering: the call site set the global before computing integrity.NormalizedPolicyownedStrings:scope_kindandmin_integritywere stored as heap-allocatedStrings even though both values are always compile-time constants (e.g."All","Composite","none","approved").Changes
guards/github-guard/rust-guard/src/labels/helpers.rsScopeKind::as_str() -> &'static stralongside the existingDisplayimpl.Display::fmtto delegate toas_str(), eliminating the duplicated match arm.guards/github-guard/rust-guard/src/lib.rsintegritylabels beforeset_runtime_policy_context. The integrity functions take&PolicyContext(shared borrows); they don't interact with the global. The global is only needed bylabel_resource/label_response, which are separate FFI calls made afterlabel_agentreturns. This reorder is semantically safe.set_runtime_policy_context(ctx.clone())withset_runtime_policy_context(ctx)(move, no clone).NormalizedPolicy.scope_kindand.min_integrityfromStringto&'static str.normalized_scope_kind()to return&'static strusingScopeKind::as_str()..to_string()fromintegrity_floor.as_str().Trade-offs
None. Both changes are zero-risk refactors: the semantics are identical, the types become more precise, and the allocations are eliminated.
Test Status
All 322 Rust guard tests pass. No production behaviour was changed.
Warning
The following domain was blocked by the firewall during workflow execution:
proxy.golang.orgTo allow these domains, add them to the
network.allowedlist in your workflow frontmatter:See Network Configuration for more information.