Commit 91a2aa2
authored
perf(rust-guard): eliminate redundant clones in
Two hot-path allocations in the WASM guard's response labeling pipeline:
`extract_mcp_response` unconditionally deep-clones the full JSON payload
on the common (already-unwrapped) path, and `first_matching_scope`
clones a `PolicyScopeEntry` (3× `Option<String>`) on every call to
`policy_private_scope_label`.
## `extract_mcp_response` → `Cow<'_, Value>` (`labels/mod.rs`)
Common path now borrows; only the rare MCP-wrapped case allocates:
```rust
// Before
pub(crate) fn extract_mcp_response(response: &Value) -> Value {
// ...
response.clone() // full payload deep-copy every call
}
// After
pub(crate) fn extract_mcp_response(response: &Value) -> Cow<'_, Value> {
// ...
Cow::Borrowed(response) // zero allocation on common path
// wrapped path: Cow::Owned(parsed)
}
```
Call site updates in `response_items.rs` where `Value` (not `Cow`) is
required: `actual_response.clone()` → `actual_response.as_ref().clone()`
and `&actual_response` → `actual_response.as_ref()`. All other call
sites auto-deref through `Deref<Target = Value>` unchanged.
## `first_matching_scope` → `Option<&'a PolicyScopeEntry>`
(`labels/helpers.rs`)
Removes `.cloned()` to eliminate 3 `String` allocations per lookup.
`policy_private_scope_label` (the only call site) is unaffected —
`ScopeKind` is `Copy` and field access works identically through a
reference.
```rust
// Before
fn first_matching_scope(owner: &str, repo: &str, ctx: &PolicyContext) -> Option<PolicyScopeEntry> {
ctx.scopes.iter().find(|scope| { /* ... */ }).cloned()
}
// After
fn first_matching_scope<'a>(owner: &str, repo: &str, ctx: &'a PolicyContext) -> Option<&'a PolicyScopeEntry> {
ctx.scopes.iter().find(|scope| { /* ... */ })
}
```
> [!WARNING]
>
> <details>
> <summary>Firewall rules blocked me from connecting to one or more
addresses (expand for details)</summary>
>
> #### I tried to connect to the following addresses, but was blocked by
firewall rules:
>
> - `example.com`
> - Triggering command: `/tmp/go-build3904760875/b513/launcher.test
/tmp/go-build3904760875/b513/launcher.test
-test.testlogfile=/tmp/go-build3904760875/b513/testlog.txt
-test.paniconexit0 -test.timeout=10m0s go_.�� ache/go/1.25.9/x-errorsas
etut/NpEHhtvPW_A-ifaceassert x_amd64/vet --gdwarf-5 --64 -o x_amd64/vet`
(dns block)
> - Triggering command: `/tmp/go-build100690393/b509/launcher.test
/tmp/go-build100690393/b509/launcher.test
-test.testlogfile=/tmp/go-build100690393/b509/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true -bool
72e203ec55626255a1c2f6351011f81be538b3bb6463e1a9d5c
ker/cli-plugins/docker-compose 72e203ec55626255bash` (dns block)
> - `invalid-host-that-does-not-exist-12345.com`
> - Triggering command: `/tmp/go-build3904760875/b495/config.test
/tmp/go-build3904760875/b495/config.test
-test.testlogfile=/tmp/go-build3904760875/b495/testlog.txt
-test.paniconexit0 -test.timeout=10m0s
/tmp/go-build3904760875/b396/vet.cfg 1154246/b196/_pkg_.a -trimpath
x_amd64/vet -p go.opentelemetry-atomic -lang=go1.25 x_amd64/vet go_.��
.cfg knP4/DXqMOEsmzZ1-ifaceassert x_amd64/vet --gdwarf-5 --64 -o
x_amd64/vet` (dns block)
> - Triggering command: `/tmp/go-build100690393/b491/config.test
/tmp/go-build100690393/b491/config.test
-test.testlogfile=/tmp/go-build100690393/b491/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true 4760875/b504/_pkg_.a
stmain.go docker-compose
by/32b5d288a6552/usr/lib/open-iscsi/net-interface-handler -ifaceassert
-nilfunc docker-compose n-me�� b6b9402ad1484ea3 -buildtags
"CURL_CA_BUNDLE=/-id by/181260d18b4dc/usr/bin/networkctl
-ifaceassert -nilfunc iginal` (dns block)
> - `nonexistent.local`
> - Triggering command: `/tmp/go-build3904760875/b513/launcher.test
/tmp/go-build3904760875/b513/launcher.test
-test.testlogfile=/tmp/go-build3904760875/b513/testlog.txt
-test.paniconexit0 -test.timeout=10m0s go_.�� ache/go/1.25.9/x-errorsas
etut/NpEHhtvPW_A-ifaceassert x_amd64/vet --gdwarf-5 --64 -o x_amd64/vet`
(dns block)
> - Triggering command: `/tmp/go-build100690393/b509/launcher.test
/tmp/go-build100690393/b509/launcher.test
-test.testlogfile=/tmp/go-build100690393/b509/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true -bool
72e203ec55626255a1c2f6351011f81be538b3bb6463e1a9d5c
ker/cli-plugins/docker-compose 72e203ec55626255bash` (dns block)
> - `slow.example.com`
> - Triggering command: `/tmp/go-build3904760875/b513/launcher.test
/tmp/go-build3904760875/b513/launcher.test
-test.testlogfile=/tmp/go-build3904760875/b513/testlog.txt
-test.paniconexit0 -test.timeout=10m0s go_.�� ache/go/1.25.9/x-errorsas
etut/NpEHhtvPW_A-ifaceassert x_amd64/vet --gdwarf-5 --64 -o x_amd64/vet`
(dns block)
> - Triggering command: `/tmp/go-build100690393/b509/launcher.test
/tmp/go-build100690393/b509/launcher.test
-test.testlogfile=/tmp/go-build100690393/b509/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true -bool
72e203ec55626255a1c2f6351011f81be538b3bb6463e1a9d5c
ker/cli-plugins/docker-compose 72e203ec55626255bash` (dns block)
> - `this-host-does-not-exist-12345.com`
> - Triggering command: `/tmp/go-build3904760875/b522/mcp.test
/tmp/go-build3904760875/b522/mcp.test
-test.testlogfile=/tmp/go-build3904760875/b522/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -o 64/src/net -trimpath
x_amd64/vet -p net/http/httptes--version -lang=go1.25 x_amd64/vet .cfg��
/opt/hostedtoolcache/go/1.25.9/xgo1.25.9 1154246/b166/ x_amd64/vet
--gdwarf-5 --64` (dns block)
> - Triggering command: `/tmp/go-build100690393/b518/mcp.test
/tmp/go-build100690393/b518/mcp.test
-test.testlogfile=/tmp/go-build100690393/b518/testlog.txt
-test.paniconexit0 -test.timeout=10m0s -test.v=true -bool y
/usr/local/bin/bash ntime.v2.task/mobash -ifaceassert 2d7/log.json bash
/usr��
6788dd938a942ab2a742ecfee664e61b/run/containerd/io.containerd.runtime.v2.task/moby/1166b6fa98e14docker
-tests /usr/sbin/bash 2d7 /tmp/go-build410/usr/bin/runc 2d7/init.pid
bash` (dns block)
>
> If you need me to access, download, or install something from one of
these locations, you can either:
>
> - Configure [Actions setup
steps](https://gh.io/copilot/actions-setup-steps) to set up my
environment, which run before the firewall is enabled
> - Add the appropriate URLs or hosts to the custom allowlist in this
repository's [Copilot coding agent
settings](https://github.com/github/gh-aw-mcpg/settings/copilot/coding_agent)
(admins only)
>
> </details>extract_mcp_response and first_matching_scope (#5103)3 files changed
Lines changed: 8 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
229 | 229 | | |
230 | 230 | | |
231 | 231 | | |
232 | | - | |
| 232 | + | |
233 | 233 | | |
234 | 234 | | |
235 | 235 | | |
236 | 236 | | |
237 | 237 | | |
238 | 238 | | |
239 | 239 | | |
240 | | - | |
241 | 240 | | |
242 | 241 | | |
243 | 242 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
| 28 | + | |
27 | 29 | | |
28 | 30 | | |
29 | 31 | | |
| |||
93 | 95 | | |
94 | 96 | | |
95 | 97 | | |
96 | | - | |
| 98 | + | |
97 | 99 | | |
98 | 100 | | |
99 | 101 | | |
| |||
128 | 130 | | |
129 | 131 | | |
130 | 132 | | |
131 | | - | |
| 133 | + | |
132 | 134 | | |
133 | 135 | | |
134 | 136 | | |
| |||
143 | 145 | | |
144 | 146 | | |
145 | 147 | | |
146 | | - | |
| 148 | + | |
147 | 149 | | |
148 | 150 | | |
149 | 151 | | |
| |||
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
128 | 128 | | |
129 | 129 | | |
130 | 130 | | |
131 | | - | |
| 131 | + | |
132 | 132 | | |
133 | 133 | | |
134 | 134 | | |
| |||
240 | 240 | | |
241 | 241 | | |
242 | 242 | | |
243 | | - | |
| 243 | + | |
244 | 244 | | |
245 | 245 | | |
246 | 246 | | |
| |||
0 commit comments