← Back to Pull requests

perf(policy): read a request's header once by a name parsed ahead #9439

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 30, 2026 14:40 UTC
Author

Description

Problem


The engine reads a request's headers by the names its policy spec writes, and the header map parsed each name again at every request. A keyed manual request read its routing header twice and cost 9,115 instructions at 8 workers. The gateway's own policy costs 6,706.

Solution


The names are parsed once, where the policy spec is applied, and the header map is asked by the parsed name. The header a routing key came from is read once: the view takes the key's own text for it. No answer changes, and a host's own reader is asked by text as before, unless it provides the new HeaderRead::header_by.

Changes


The one request that reads more, in instructions a decision.

| Request | Before | After | The gateway's own policy | Cause |
|---|---|---|---|---|
| manual, a pair with no routing key, 8 workers | 10,236 | 10,252 | 12,821 | its view lists the routing header, and both of the pair's makings walk that list by the kept form: 6 more in the list's maker and 2 in the making, twice |

What a keyed manual request's header reads cost at 8 workers, by function.

| Function | Before | After |
|---|---|---|
| A name parsed and found as text: parse_hdr, find, the text's hash, header | 778 | 0 |
| A header found by its parsed name: header_by and the name's hash | 0 | 181 |
| A value checked as text: from_utf8 | 327 | 218 |

On main the compiler builds the lookup by text out of line. That had cost this request 104 instructions and a keyed consistent_hashing request 98. Both come back, with 575 and 372 more.

The second commit builds the lease guard's dispatch word into its two callers again. Counted alone, for the same request.

| Function | Before | After |
|---|---|---|
| DecisionGuard::dispatch, a function of its own | 90 | 0 |
| Placement::sent, which holds it again | 33 | 105 |
| The two, on every single request with a lease | 123 | 105: 18 fewer |

The guard's drop still stands out of line, 20 instructions: no mark of this crate reaches it.

#[inline] is a hint: the compiler weighs the function against its callers, and a landing that changed the guard moved the body out of line. #[inline(always)] takes the size bound off a direct call. This request is the cell that would show it lost: dispatch would stand as a row of its own again.

| Read | Before | After |
|---|---|---|
| A key source, the target's header, a view header | by the name's text: the map parses it at each request | by the name parsed where the spec is applied |
| The view's entry of the key's own header | the header, read a second time | the key's text, where no lineage suffix left the key |
| Under strip_lineage, or a value that is no text | the header, read a second time | the same |
| A view that lists no header | an empty list is made at each request | no list is made |
| A name longer than a header map holds | by its text | the same |
| A reader that provides header alone | each name by its text | the same |
| A request that carries no header | its reader answers nothing | the same, by either name |

| File | What changes |
|---|---|
| src/engine/in_process/place.rs | HeaderRead::header_by, a provided method. The header map answers by the parsed name, and the engine's own maker reads through one reader. The view's list has two makers: one is handed nothing of the key |
| src/document/routing_key.rs | the key sources keep each name parsed (KeptName) and read through a reader of names (SourceRead), both private to the crate. The key's two reads are loops built into their callers |
| src/document/applied.rs | the applied spec keeps the view's names parsed, and says whether a header's key is its whole value |
| src/engine/in_process/place/target.rs | the header a request names its worker by is read by its parsed name |
| src/tracking/decision_guard.rs | the second commit: dispatch is #[inline(always)] |
| docs/design.md | one paragraph on how the one call reads headers |
| tests/unit/ | eight cases in two files. One case of main probed a making by the view's list under round_robin: it now takes manual, whose view lists a header |

| Limit | What stands |
|---|---|
| A request with no key | The 12 other cells move by -53 to +0 instructions, 0.76 percent at the most |
| Under strip_lineage | the key may be shorter than its header, so the header is read for the view as before |
| A host that derives keys itself | KeySources::derive and its siblings take a function of a name, and read by text as before |
| The gateway's own read of a routing key | it is not this crate's, and it goes by a parsed name already |
| A name is parsed | once a policy spec, at its apply. No name is parsed on a request's road |

Test Plan


Every step ran on this head, cc4657f2c, which stands on main 808fdccd9.

| Gate step | Command | Result |
|---|---|---|
| Format | cargo +nightly fmt --all -- --check | clean |
| Lint, every feature | cargo clippy --locked -p smg-grand-central -p grand-central-example --all-targets --all-features -- -D warnings | clean |
| Lint, default features | cargo clippy --locked -p smg-grand-central --all-targets -- -D warnings | clean |
| Lint, the gateway's features | cargo clippy --locked -p smg-grand-central --no-default-features --features in-process,operator --all-targets -- -D warnings | clean |
| Docs | cargo doc --locked -p smg-grand-central --no-deps --all-features, warnings denied | clean |
| Suite | cargo test --locked -p smg-grand-central -p grand-central-example --features smg-grand-central/client,smg-grand-central/kubernetes,smg-grand-central/worker-host --no-fail-fast | 6,522 passed, 0 failed, 37 ignored |
| Cases, a host with no controller | cargo test --locked -p smg-grand-central --lib --no-default-features --features worker-host --no-fail-fast | 2,313 passed, 0 failed, 2 ignored |
| Cases, default features with discovery | cargo test --locked -p smg-grand-central --lib --features smg-grand-central/discovery --no-fail-fast | 5,519 passed, 0 failed, 21 ignored |
| Cases, the operator feature alone | cargo test --locked -p smg-grand-central --no-default-features --features operator --test operator_door --no-fail-fast | 5 passed, 0 failed, 0 ignored |
| The gateway's programs, the option forced on | SMG_TEST_GRAND_CENTRAL=on cargo test --locked --no-fail-fast -p smg --test routing_tests --test grpc_tests --test http_tests --test metrics_tests --test policy_text_question_test --test grpc_pd_tests | 405 passed, 0 failed, 0 ignored in 6 programs |

| Case | What it holds |
|---|---|
| the_header_a_routing_value_came_from_is_read_once_a_request | a counting reader: the key's header is asked once, with the maker's word asked ahead and with none. The view's two entries are the headers' values |
| the_views_entry_of_the_keys_header_is_the_headers_value | the entry byte for byte: the whole value, a value whose lineage suffix left the key, and a value that is no text |
| a_reader_that_answers_by_text_alone_is_asked_as_the_document_writes | a reader written before this change is asked each name by its text. Its request is the one a header map of the same headers makes, and its named worker the same |
| a_name_no_map_of_headers_holds_is_read_by_its_text | a name of 65,536 bytes hands the routing key and is shown to a stage, read by its text |
| a_map_of_headers_answers_by_a_parsed_name_as_by_its_text | a value, the first of two values, a value that is no text, and an absent name |
| a_request_with_no_header_answers_a_parsed_name_with_nothing | the reader of no header answers nothing by a parsed name and by its text |
| a_names_kept_form_reads_as_the_document_writes_it | a name in another case and a name that is no header's keep no parsed form: the reader is asked the text as written |
| the_sources_read_a_request_by_their_kept_names | a reader that answers by the parsed name alone hands the routing key, the client's value and the target's number |

Numbers


| Method | |
|---|---|
| Bench | the gateway's decision_paths: one executable for both paths, built on both sides in one build folder |
| Sides | main 808fdccd9 before, this head after. The second commit was also counted against the first |
| Count | one decision's instructions under Callgrind: a run of 60,000 decisions less a run of 20,000, over 40,000. One caller, the gRPC route, three runs a cell and a side |
| Reading | the mean of three runs, the clock's rows left out. A change is taken of the means before rounding. This change adds and removes no clock read |
| A bench cell | it is not a served request's whole route. This change claims no served cell |

The requests this change is about, with the gateway's own policy beside.

| Request | Workers | Classic | Before | After | Change | Of before | After, of classic |
|---|---|---|---|---|---|---|---|
| manual, 1,024 routing keys | 8 | 6,706 | 9,115 | 8,418 | 697 fewer | 7.6 percent | 1.26 |
| manual, 1,024 routing keys | 64 | 16,532 | 9,148 | 8,451 | 697 fewer | 7.6 percent | 0.51 |
| manual, 1,024 routing keys | 512 | 84,908 | 9,176 | 8,481 | 695 fewer | 7.6 percent | 0.10 |
| consistent_hashing, 1,024 routing keys | 8 | 8,144 | 8,966 | 8,496 | 470 fewer | 5.2 percent | 1.04 |
| consistent_hashing, 1,024 routing keys | 64 | 18,822 | 9,104 | 8,634 | 470 fewer | 5.2 percent | 0.46 |
| consistent_hashing, 1,024 routing keys | 512 | 93,428 | 9,267 | 8,799 | 469 fewer | 5.1 percent | 0.09 |
| prefix_hash, 1,024 routing keys | 8 | 9,791 | 10,856 | 10,615 | 242 fewer | 2.2 percent | 1.08 |
| prefix_hash, 1,024 routing keys | 64 | 32,312 | 22,627 | 22,385 | 242 fewer | 1.1 percent | 0.69 |
| prefix_hash, 1,024 routing keys | 512 | 191,148 | 115,720 | 115,476 | 244 fewer | 0.2 percent | 0.60 |

Every other cell counted.

| Request | Workers | Before | After | Change | Of before |
|---|---|---|---|---|---|
| consistent_hashing, a pair | 8 | 13,969 | 13,930 | 39 fewer | 0.3 percent |
| least_load, a pair | 8 | 19,867 | 19,831 | 36 fewer | 0.2 percent |
| manual, a pair | 8 | 10,236 | 10,252 | 16 more | 0.2 percent |
| bucket | 8 | 4,752 | 4,734 | 18 fewer | 0.4 percent |
| cache_aware | 8 | 12,797 | 12,745 | 52 fewer | 0.4 percent |
| consistent_hashing, no routing key | 8 | 2,862 | 2,858 | 4 fewer | 0.1 percent |
| least_load | 8 | 9,109 | 9,057 | 52 fewer | 0.6 percent |
| manual, no routing key | 8 | 2,682 | 2,676 | 6 fewer | 0.2 percent |
| manual, no routing key | 512 | 2,682 | 2,676 | 6 fewer | 0.2 percent |
| passthrough | 8 | 2,213 | 2,213 | none | 0.0 percent |
| power_of_two | 8 | 6,995 | 6,942 | 53 fewer | 0.8 percent |
| prefix_hash, no routing key | 8 | 9,127 | 9,085 | 42 fewer | 0.5 percent |
| random | 8 | 2,247 | 2,247 | none | 0.0 percent |
| round_robin | 8 | 2,206 | 2,206 | none | 0.0 percent |

The classic path holds none of this code. Its 23 cells read level. The largest change of a mean is 0.23 percent, for least_load at 8 workers. The largest in instructions is 95.05, for manual, 1,024 routing keys at 512 workers.

Release note


The engine reads a request's headers by names parsed once at the policy spec's apply, and reads the routing header once a request. A keyed manual request costs 8 percent fewer instructions at 8 workers. A host's own header reader may provide HeaderRead::header_by. Without it nothing changes.

Diff: 650 lines (+487 / -163) across 8 files Reviewer: claude[bot] Review duration: 10m
All Vouch review confidence checks passed
Residual risk 0.32 = Change Risk (0.52) × (1 − Review Confidence (0.38)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
0.52
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
0.02
Model 3 · Review Timing & Attention
Session adequacy: 63% · Reviewer familiarity: 35%
37%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.388
Shallow Review Depth shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #9439"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 37.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 52% (MEDIUM) · Higher = larger blast radius
52%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 37.5% · Lower = rushed or shallow review
37.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 32.5% · Safe to merge
32.5%