Skip to content

Commit

Permalink
perf: add __slots__ to frequently created / accessed internal classes
Browse files Browse the repository at this point in the history
  • Loading branch information
adriangb committed Oct 17, 2021
1 parent 838667e commit ad3b6de
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 6 deletions.
1 change: 1 addition & 0 deletions di/_local_scope_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@


class LocalScopeContext(FusedContextManager[None]):
__slots__ = ("context", "scope", "token", "_sync_cm", "_async_cm")
context: contextvars.ContextVar[ContainerState]
scope: Scope
token: contextvars.Token[ContainerState]
Expand Down
2 changes: 2 additions & 0 deletions di/_scope_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ 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.
"""

__slots__ = ("mappings",)

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

def __init__(self) -> None:
Expand Down
3 changes: 3 additions & 0 deletions di/_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@


class ContainerState(object):
__slots__ = ("binds", "cached_values", "stacks")
binds: Dict[DependencyProvider, DependantProtocol[Any]]
cached_values: ScopeMap[DependencyProvider, Any]
stacks: Dict[Scope, Union[AsyncExitStack, ExitStack]]
Expand Down Expand Up @@ -72,6 +73,8 @@ def unbind() -> Generator[None, None, None]:


class ScopeContext(FusedContextManager[None]):
__slots__ = ("state", "scope")

def __init__(self, state: ContainerState, scope: Scope) -> None:
self.state = state
self.scope = scope
Expand Down
6 changes: 6 additions & 0 deletions di/_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@


class Task(Generic[DependencyType]):
__slots__ = ("dependant", "dependencies")

def __init__(
self,
dependant: DependantProtocol[DependencyType],
Expand Down Expand Up @@ -54,6 +56,8 @@ def use_value(


class AsyncTask(Task[DependencyType]):
__slots__ = ()

async def compute(
self,
state: ContainerState,
Expand Down Expand Up @@ -89,6 +93,8 @@ async def compute(


class SyncTask(Task[DependencyType]):
__slots__ = ()

def compute(
self,
state: ContainerState,
Expand Down
2 changes: 1 addition & 1 deletion di/dependant.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
)


class Dependant(DependantProtocol[DependencyType], object):
class Dependant(DependantProtocol[DependencyType]):
wire: bool
autowire: bool

Expand Down
37 changes: 33 additions & 4 deletions di/types/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,36 @@
from typing import AsyncContextManager, ContextManager, TypeVar
import sys
from types import TracebackType
from typing import Optional, Type, TypeVar, Union

T = TypeVar("T")
if sys.version_info >= (3, 8):
from typing import Protocol
else:
from typing_extensions import Protocol

T = TypeVar("T", covariant=True)

class FusedContextManager(AsyncContextManager[T], ContextManager[T]):
...

class FusedContextManager(Protocol[T]):
__slots__ = ()

def __enter__(self):
...

def __exit__(
self,
exc_type: Optional[Type[BaseException]],
exc_value: Optional[BaseException],
traceback: Optional[TracebackType],
) -> Union[None, bool]:
...

async def __aenter__(self):
...

async def __aexit__(
self,
exc_type: Optional[Type[BaseException]],
exc_value: Optional[BaseException],
traceback: Optional[TracebackType],
) -> Union[None, bool]:
...
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.18.5"
version = "0.18.6"
description = "Autowiring dependency injection"
authors = ["Adrian Garcia Badaracco <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit ad3b6de

Please sign in to comment.