Skip to content

Commit

Permalink
Performance tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
adriangb committed Oct 1, 2021
1 parent 159cefb commit 7528b1b
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 12 deletions.
2 changes: 1 addition & 1 deletion benchmarks/di_nested.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from di import Depends
from docs.src.starlette.src import App

root = generate_dag(Depends, 15, 5, 3)
root = generate_dag(Depends, 4, 3, 3)


app = App()
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/fastapi_nested.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from benchmarks.utils import generate_dag

root = generate_dag(Depends, 15, 5, 3)
root = generate_dag(Depends, 4, 3, 3)


app = App()
Expand All @@ -24,6 +24,6 @@ async def endpoint(dag: None = Depends(root)):

with TestClient(app) as client:
start = time.time()
for _ in range(2):
for _ in range(50):
client.get("/")
print(f"{time.time()-start}")
37 changes: 37 additions & 0 deletions benchmarks/solve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Profile solving a DAG
"""

import cProfile
import pstats

from benchmarks.utils import generate_dag
from di import Container, Dependant, Depends

root = generate_dag(Depends, 1, 1, 1)


async def endpoint(dag: None = Depends(root)):
return ...


container = Container()
solved = container.solve(Dependant(endpoint))


async def bench():
await container.execute_async(solved)


async def main():
profiler = cProfile.Profile()
profiler.enable()
await bench()
profiler.disable()
stats = pstats.Stats(profiler)
stats.dump_stats(filename="bench.prof")


if __name__ == "__main__":
import asyncio

asyncio.run(main())
6 changes: 5 additions & 1 deletion di/_local_scope_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@


class LocalScopeContext(FusedContextManager[None]):
context: contextvars.ContextVar[ContainerState]
scope: Scope
token: Optional[contextvars.Token[ContainerState]]

def __init__(
self, context: contextvars.ContextVar[ContainerState], scope: Scope
) -> None:
self.context = context
self.scope = scope
self.token: Optional[contextvars.Token[ContainerState]] = None
self.token = None

def __enter__(self):
current = self.context.get()
Expand Down
6 changes: 4 additions & 2 deletions di/_scope_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ class ScopeMap(Generic[KT, VT]):
ChainMap also doesn't allow you to set values anywhere but the left mapping, and we need to set values in arbitrary mappings.
"""

mappings: Dict[Scope, Dict[KT, VT]]

__slots__ = ("mappings",)

def __init__(self) -> None:
self.mappings: Dict[Scope, Dict[KT, VT]] = {}
self.mappings = {}

def get(self, key: KT) -> VT:
for mapping in self.mappings.values():
Expand Down Expand Up @@ -68,7 +70,7 @@ def pop_scope(self, scope: Scope) -> None:
self.mappings.pop(scope)

def copy(self) -> ScopeMap[KT, VT]:
new = ScopeMap[KT, VT]()
new = type(self)()
new.mappings = self.mappings.copy()
return new

Expand Down
11 changes: 6 additions & 5 deletions di/_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@


class ContainerState(object):

__slots__ = ("binds", "cached_values", "stacks")
binds: Dict[DependencyProvider, DependantProtocol[Any]]
cached_values: ScopeMap[DependencyProvider, Any]
stacks: Dict[Scope, Union[AsyncExitStack, ExitStack]]

def __init__(self) -> None:
self.binds: Dict[DependencyProvider, DependantProtocol[Any]] = {}
self.cached_values = ScopeMap[DependencyProvider, Any]()
self.stacks: Dict[Scope, Union[AsyncExitStack, ExitStack]] = {}
self.binds = {}
self.cached_values = ScopeMap()
self.stacks = {}

def copy(self) -> "ContainerState":
new = ContainerState()
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "di"
version = "0.4.14"
version = "0.4.15"
description = "Autowiring dependency injection"
authors = ["Adrian Garcia Badaracco <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit 7528b1b

Please sign in to comment.