Skip to content

Enhancement: Optimize dependency resolution overhead (TaskGroup bottleneck) #4563

@maximsakhno

Description

@maximsakhno

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:

Image

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementThis is a new feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions