-
-
Notifications
You must be signed in to change notification settings - Fork 511
Description
Summary
Benchmarks show a significant performance degradation in dependency resolution when increasing the number of flat dependencies. Specifically, moving from 1 to 2 dependencies results in a sharp drop in RPS — down to ~36%.
Investigation suggests this is due to the overhead introduced by TaskGroup usage for parallel resolution.
Basic Example
Here is a reproduction scenario with two empty dependencies and handlers using one vs. two dependencies:
from litestar import get, Litestar
async def dep0() -> int:
return 42
async def dep1() -> int:
return 42
@get("/deps-1")
async def handler1(
val0: int,
) -> str:
# Benchmark result: 41,451 req/s
return "OK"
@get("/deps-2")
async def handler2(
val0: int,
val1: int,
) -> str:
# Benchmark result: 26,492 req/s
# (Performance drops to 36% compared to "/deps-1")
return "OK"Visualizing the drop:
The full benchmark reproduction is available here: https://github.com/maximsakhno/di-benchmarks
Drawbacks and Impact
Optimizing the dependency resolution logic will prevent the performance cliff and substantially increase RPS for endpoints with multiple dependencies, making the framework much more efficient in real-world scenarios with complex dependency trees.
Unresolved questions
No response