Replies: 7 comments
-
|
Oh... This is sad. I did expect a performance regression overall, and it was even mentioned on the Starlette PR that introduced anyio. I did not expect those high numbers here, tho. @adriangb has been working on performance optimizations on the dependencies' setup, but we might need to find more alternatives anyway... Would you mind sharing the numbers with an ASGI server instead of the TestClient i.e. uvicorn with an HTTP benchmarking tool? |
Beta Was this translation helpful? Give feedback.
-
|
We had some discussion on AnyIO's gitter regarding this. I have no idea how to link to a gitter message, so I'll summarize here. I made a gist comparing anyio to asyncio's performance: https://gist.github.com/adriangb/4769659899abd24f5d184332a2cdbee8 I'd guess the use case you present is even more pathological @crea since not only is async stuff being done, but there's also thread pools and stuff involved. Quoting @agronholm:
The TLDR is that AnyIO does considerably more stuff so for small workloads it has quite a bit of overhead. For something like sending an HTTP request, the overhead is negligible. In your test @crea , I suspect that if you add a Regarding my work @Kludex mentioned, I have a proposal in #3641 but that may be a bit ambitious, so I think FastAPI's best concrete bet is #3902 (it doesn't directly address the performance regression you are noticing - in fact it would probably make things worse for the toy use case above, but it does improve performance of the DI system overall). |
Beta Was this translation helpful? Give feedback.
-
|
Thank you for your first thoughts on this issue. I attached a comparison using the benchmarking tool hey: There the results are not that drastic, just around 16% latency increase. |
Beta Was this translation helpful? Give feedback.
-
|
@crea this won't resolve the overall performance regression, but I wonder if we can solve your particular use case? What does your DAG look like? Are the dependencies parallelizable (A -> B and A -> C and A -D, etc. like in your example above) or are they just nested (A -> B -> C)? Would you be willing to abandon FastAPI's DI system? |
Beta Was this translation helpful? Give feedback.
-
|
In most instances our dependency graph is shallow with only a few dependences where the depth is 2-3. So there might be the opportunity to parallelize. When it comes to switching the DI system, we really like the simplicity and good integration of fastapi, so having fast resolving out of the box (as before) would be preferred. |
Beta Was this translation helpful? Give feedback.
-
Maybe give #3902 a try? It should be a drop-in replacement.
Unfortunately, I do not see any way of going back for FastAPI. AnyIO would have to improve their overhead. |
Beta Was this translation helpful? Give feedback.
-
|
One thing that I hadn't noticed until now is that you are using TestClient. I think the findings are pretty much the same, but at lest we know it's not the TestClient that's slower now. I think it'd be nice if FastAPI at least gave you the option to disable moving sync dependencies to threads, I'm guessing that's a big part of the performance hit your seeing (moving a sync function that does no IO to a thread is bad, and anyio only exacerbated the problem because it has more overhead than asyncio). I benchmarked this using Xpresso (which does give you that option) and got |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
The update to fastapi 0.69.0 introduced a performance regression of nearly when using dependencies. The runtime of the sync version with 10 dependencies as shown in the code example increased by around 70%. The runtime of the async version increased by 30%.
This was likely introduced by the starlette upgrade to version 0.15 and their switch to anyio. As a workaround dependencies can be switched to async calls with a smaller performance hit.
Are there any plans to optimize the sync version or to document this behavior?
Operating System
macOS
Operating System Details
No response
FastAPI Version
0.69.0
Python Version
Python 3.9.7
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions