-
Notifications
You must be signed in to change notification settings - Fork 178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf(robot-server): Flatten FastAPI routers #17169
Open
SyntaxColoring
wants to merge
11
commits into
edge
Choose a base branch
from
flatten_fastapi_routers
base: edge
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains 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
This excludes service/session/router.py because it's using `APIRouter.url_path_for()`. That looks like something we can remove, but I'm not attempting it here.
These routers have a custom 422 response, and modify the OpenAPI spec accordingly at the router level, and FastBuildRouter isn't smart enough to know what to do with that right now.
Specifically, doing the equivalent of e.g. FastAPI.get(app, *args, **kwargs) instead of app.get(*args, **kwargs) was making the mocking pretty confusing.
SyntaxColoring
changed the title
perf(robot-server): Flatten fastapi routers
perf(robot-server): Flatten FastAPI routers
Dec 21, 2024
TamarZanzouri
approved these changes
Dec 23, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this approach is clear. The only rename I would change is the _SomethingCallableLike
to _CallableLike
but no biggy. Thank you for fixing this!
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.
Overview
The Pydantic v1->v2 update makes server startup much slower. It turns out that a surprisingly large portion of that problem is due to inefficiencies in FastAPI's
.include_router()
method. This works around that.Closes EXEC-1067.
Performance testing
import robot_server.app
on my laptopimport robot_server.app
on a robot (estimated)*Note that the comparison against Pydantic v1 is a bit apples-to-oranges, because Pydantic v2 does more things during import time so it can be faster after import time.
Raw flame graph files here.
Correctness testing
I could also add more focused unit tests for this if we think it's worth the time.
Changelog
Our constraints are basically:
.include_router()
as little as possible, for performance.Therefore:
server_utils
, build a drop-in replacement for FastAPI'sAPIRouter
. It is basically a subset ofAPIRouter
's interface and functionality. There is some "cleverness" here to avoid having to duplicate the giant method signatures of.get()
,.post()
, etc.robot_server
, switch as much as we can over to the drop-in. This is basically just a find+replace job, but there are a couple of small gotchas.Review requests
Risk assessment
Medium.
If something goes wrong here, I'd bet it would be in the part where we merge kwargs from different levels of the router tree. We want to match FastAPI behavior there, which seems like it can be pretty nuanced. Also, it is just some ugly dict manipulation.