Commit fb48631
authored
rust-guard: remove hot-path scope/integrity allocations via
## ✨ Enhancement
`policy_scope_token` and `normalize_scope` were allocating on common
single-scope/fallthrough paths, and `integrity_rank_normalized` was
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?**
- **Scope token + normalization fast path**
- `policy_scope_token` now returns `Cow<'_, str>`:
- empty scopes → borrowed `""`
- single scope → borrowed scope label
- multi-scope → owned joined token
- `normalize_scope` now returns `Cow<'a, str>`, borrowing `scope` when
no scoped replacement is needed instead of cloning.
- **Integrity rank matching fast path**
- `integrity_rank_normalized` now uses `label_matches_normalized(...)`
instead of constructing formatted labels for each rank.
- Common non-multi-scope comparisons are allocation-free; multi-scope
keeps canonical formatting fallback.
- **Call-site/lifetime adjustment**
- In `label_agent`, scope token derivation was moved to read from
`ctx.scopes` after context construction to satisfy borrowing with the
new `Cow`-based flow.
**Why is this valuable?**
- Cuts repeated small heap allocations in per-item integrity computation
(`reader/writer/merged/none/max/cap` paths), especially in the dominant
single-scope case and rank comparisons.
**Implementation approach:**
- Introduced `Cow` return types for scope helpers and propagated usage
through existing integrity builders without behavior changes.
- Added a non-allocating label predicate with rare-path fallback for
multi-scope canonical formatting.
- Added focused helper tests for:
- borrowed vs owned `policy_scope_token` behavior,
- borrowed `normalize_scope` fallthrough behavior,
- `label_matches_normalized` parity across empty/single/multi-scope
forms.
```rust
pub(crate) fn policy_scope_token(scopes: &[PolicyScopeEntry]) -> Cow<'_, str> {
let mut labels = scopes.iter().map(|s| s.scope_label.as_str()).filter(|s| !s.is_empty());
match (labels.next(), labels.next()) {
(None, _) => Cow::Borrowed(""),
(Some(first), None) => Cow::Borrowed(first),
(Some(first), Some(second)) => {
let mut value = String::from(first);
value.push_str(" | ");
value.push_str(second);
for rest in labels {
value.push_str(" | ");
value.push_str(rest);
}
Cow::Owned(value)
}
}
}
```Cow<str> and zero-alloc rank matching (#5754)2 files changed
Lines changed: 128 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
156 | 157 | | |
157 | 158 | | |
158 | 159 | | |
159 | | - | |
| 160 | + | |
160 | 161 | | |
161 | 162 | | |
162 | | - | |
| 163 | + | |
163 | 164 | | |
164 | 165 | | |
165 | 166 | | |
| |||
176 | 177 | | |
177 | 178 | | |
178 | 179 | | |
179 | | - | |
| 180 | + | |
180 | 181 | | |
181 | 182 | | |
182 | | - | |
| 183 | + | |
183 | 184 | | |
184 | 185 | | |
185 | 186 | | |
| |||
191 | 192 | | |
192 | 193 | | |
193 | 194 | | |
194 | | - | |
195 | | - | |
| 195 | + | |
| 196 | + | |
196 | 197 | | |
197 | 198 | | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
204 | 213 | | |
205 | 214 | | |
206 | 215 | | |
| |||
1284 | 1293 | | |
1285 | 1294 | | |
1286 | 1295 | | |
1287 | | - | |
| 1296 | + | |
| 1297 | + | |
| 1298 | + | |
| 1299 | + | |
| 1300 | + | |
| 1301 | + | |
| 1302 | + | |
| 1303 | + | |
| 1304 | + | |
| 1305 | + | |
| 1306 | + | |
| 1307 | + | |
1288 | 1308 | | |
1289 | | - | |
1290 | | - | |
| 1309 | + | |
| 1310 | + | |
| 1311 | + | |
| 1312 | + | |
1291 | 1313 | | |
1292 | 1314 | | |
1293 | 1315 | | |
1294 | 1316 | | |
1295 | 1317 | | |
1296 | 1318 | | |
| 1319 | + | |
| 1320 | + | |
| 1321 | + | |
| 1322 | + | |
| 1323 | + | |
| 1324 | + | |
| 1325 | + | |
| 1326 | + | |
| 1327 | + | |
| 1328 | + | |
| 1329 | + | |
| 1330 | + | |
| 1331 | + | |
| 1332 | + | |
1297 | 1333 | | |
1298 | 1334 | | |
1299 | 1335 | | |
| |||
1917 | 1953 | | |
1918 | 1954 | | |
1919 | 1955 | | |
| 1956 | + | |
1920 | 1957 | | |
1921 | 1958 | | |
1922 | 1959 | | |
1923 | 1960 | | |
1924 | 1961 | | |
| 1962 | + | |
| 1963 | + | |
| 1964 | + | |
| 1965 | + | |
| 1966 | + | |
| 1967 | + | |
| 1968 | + | |
| 1969 | + | |
| 1970 | + | |
| 1971 | + | |
| 1972 | + | |
| 1973 | + | |
| 1974 | + | |
| 1975 | + | |
| 1976 | + | |
| 1977 | + | |
| 1978 | + | |
| 1979 | + | |
| 1980 | + | |
| 1981 | + | |
| 1982 | + | |
| 1983 | + | |
| 1984 | + | |
| 1985 | + | |
| 1986 | + | |
| 1987 | + | |
| 1988 | + | |
| 1989 | + | |
| 1990 | + | |
| 1991 | + | |
| 1992 | + | |
| 1993 | + | |
| 1994 | + | |
| 1995 | + | |
| 1996 | + | |
| 1997 | + | |
| 1998 | + | |
| 1999 | + | |
| 2000 | + | |
| 2001 | + | |
| 2002 | + | |
| 2003 | + | |
1925 | 2004 | | |
1926 | 2005 | | |
1927 | 2006 | | |
| |||
2317 | 2396 | | |
2318 | 2397 | | |
2319 | 2398 | | |
| 2399 | + | |
| 2400 | + | |
| 2401 | + | |
| 2402 | + | |
| 2403 | + | |
| 2404 | + | |
| 2405 | + | |
| 2406 | + | |
| 2407 | + | |
| 2408 | + | |
| 2409 | + | |
| 2410 | + | |
| 2411 | + | |
| 2412 | + | |
| 2413 | + | |
| 2414 | + | |
| 2415 | + | |
| 2416 | + | |
| 2417 | + | |
| 2418 | + | |
| 2419 | + | |
| 2420 | + | |
| 2421 | + | |
| 2422 | + | |
| 2423 | + | |
| 2424 | + | |
| 2425 | + | |
| 2426 | + | |
| 2427 | + | |
| 2428 | + | |
| 2429 | + | |
| 2430 | + | |
| 2431 | + | |
2320 | 2432 | | |
2321 | 2433 | | |
2322 | 2434 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
529 | 529 | | |
530 | 530 | | |
531 | 531 | | |
532 | | - | |
533 | 532 | | |
534 | 533 | | |
535 | 534 | | |
| |||
547 | 546 | | |
548 | 547 | | |
549 | 548 | | |
| 549 | + | |
550 | 550 | | |
551 | 551 | | |
552 | 552 | | |
| |||
0 commit comments