⚡️ Skip included routers whose prefix cannot match the request path - #16159
Open
Kludex wants to merge 1 commit into
Open
⚡️ Skip included routers whose prefix cannot match the request path#16159Kludex wants to merge 1 commit into
Kludex wants to merge 1 commit into
Conversation
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
_IncludedRouter._matchscans every candidate in the included router, and recurses into nested ones, without ever checking whether the request path could be under that router's prefix. On an app built the idiomatic way - manyinclude_router(..., prefix=...)calls - a request walks the whole route tree before it can 404, and a late route is only found after every earlier subtree has been fully scanned.Every route in an included router is registered under its prefix, so a path that does not start with that prefix cannot match any of them, and the subtree can be skipped:
A prefix may contain path params (
include_router(sub, prefix="/users/{user_id}")), so only the part before the first one is literal. Prefixes that start with a path param, and routers included without a prefix, are unaffected and still scanned as before.Benchmark
808 real route paths (the GitHub REST API description), grouped into 36 included routers by first path segment. Microseconds per ASGI dispatch, best of 3:
Measured on released
starlette1.3.1. The numbers are unchanged against the current Starlettemainbranch too, sinceAPIRouter.appdoes its own dispatch and does not go through Starlette'sRouter.app.The same 808 routes registered flat on the app, with no included routers, are unaffected (no subtree to skip). Worth noting the grouped app was previously ~2.4x slower than the flat one on a late hit, so structuring an application with
include_routercarried a real cost; it is now faster than flat in every scenario measured.Correctness
fastapi/routing.pystill at 100% coverage - the existing tests already exercise the skip branch.Allowheader so 405s are compared properly, plus misses, plus nested routers and a templatedprefix="/{user_id}".AI Disclaimer
This PR was developed with the assistance of either Claude or Codex. I've reviewed and verified the changes.