perf(dispatch): hot-path GIL coalescing and Arc-shared route metadata - #86
Merged
Conversation
Wrap RouteEntry's per-request-immutable fields (algs, dep_names, dep_factories, dep_is_async, dep_wants_request, handler_param_names) in Arc so the dispatcher can `Arc::clone` cheaply instead of copying a Vec or rebuilding a HashSet on every request. Refs #76.
Reduce the number of Python::with_gil blocks taken per RSGI request and defer expensive header/query/body extraction until the handler actually needs it: - Snapshot scope.proto/method/path/query_string and clone shared middleware/cors/security configs in a single GIL block at function entry, replacing ~4 separate getattr round-trips and a state.read. - Skip body, query, authorization, and cookie parsing for routes that don't need them; only build the kwargs dict when at least one dependency or named param is configured. - Reorder map_handler_return type checks so the hot-path PyString / PyBytes downcasts run before the more expensive __oxyroute_* attribute lookups. - Inline response mapping + header merging + RSGI send into a single GIL block via new sync helpers (send_*_sync) in src/response.rs, avoiding an additional GIL acquire/release per request. - Drop the now-unused async send_bytes / send_head_with_headers helpers. Behavior is unchanged; existing tests (and the new body-skip coverage in tests/test_dispatch_fast_fail.py) continue to pass. Refs #76.
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.
Summary
RouteEntryper-request-immutable fields inArcso the dispatcher clones a pointer instead of copying aVec/ rebuilding aHashSetper request.scope.getattr(...)calls and thestate.read()clone of CORS / security / middleware into a singlePython::with_gilblock at the start ofrun_rsgi.query_string,Authorization/Cookieheaders, request body and the kwargs dict — only when the matched route actually needs them.map_handler_returnso the cheapPyString/PyBytesdowncasts run before the more expensive__oxyroute_*attribute probes.send_*_synchelpers insrc/response.rs, removing one acquire/release per request.Test plan
cargo checkmake testCloses #76.