fastapi icon indicating copy to clipboard operation
fastapi copied to clipboard

Faster start-up when using many nested routers

Open jvllmr opened this issue 1 year ago • 9 comments

First of all: Thanks a lot for your on this project! I never thought that Python Web Development (especially for back-ends) could be this much more fun.

Now to the main part: In my FastAPI project I have a lot of nested routers. Because the amount of nested routers I soon reached a FastAPI start-up time of 30 seconds or more. This hurt my developer experience a lot and I soon began digging in to why FastAPI takes so long to load.

After some research I found out that each router calculates all relevant infos on initialization. A router, which includes another router, will re-calculate these values without re-using information from the router it includes. When having a flat project structure this is not a problem.

So the solution was simple: Defer the calculation of values until they are actually needed. By doing that the start-up time was shortened significantly.

One caveat remains: Deferring the calculation results to Exceptions not being raised at start-up and they are only raised at the first call of a route. So they might not be caught if a route is not tested at least once.

This pull request is my proposal to integrate this performance optimization into FastAPI without breaking user space and having to modify tests. I mitigated the Exception's issue by introducing an argument to the APIRouter and APIRoute which decides if values are deferred or not and never deferring values in the top-level (app-integrated) router.

I'm sure there might be a better way. So let me hear if you have suggestions for improvement!

You can find my research and testing environment for this here: https://github.com/jvllmr/fastapi-deferred-init

jvllmr avatar Nov 04 '23 23:11 jvllmr

Thanks you, @jvllmr! This PR and #10546 might perfectly complement each other 🚀

Jamim avatar Dec 28 '23 11:12 Jamim