Skip to content

Commit

Permalink
Merge pull request #298 from reagento/fix/optimize_container
Browse files Browse the repository at this point in the history
Optimize context entrance by using const container key
  • Loading branch information
Tishka17 authored Nov 5, 2024
2 parents 6b4ba0e + fb0936b commit 6be79f6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/dishka/async_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __init__(
):
self.registry = registry
self.child_registries = child_registries
self._context = {DependencyKey(type(self), DEFAULT_COMPONENT): self}
self._context = {CONTAINER_KEY: self}
if context:
for key, value in context.items():
if not isinstance(key, DependencyKey):
Expand Down Expand Up @@ -227,7 +227,7 @@ def make_async_container(
) -> AsyncContainer:
registries = RegistryBuilder(
scopes=scopes,
container_type=AsyncContainer,
container_key=CONTAINER_KEY,
providers=providers,
skip_validation=skip_validation,
validation_settings=validation_settings,
Expand Down Expand Up @@ -257,3 +257,6 @@ def make_async_container(
close_parent=True,
)
return container


CONTAINER_KEY = DependencyKey(AsyncContainer, DEFAULT_COMPONENT)
7 changes: 5 additions & 2 deletions src/dishka/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __init__(
):
self.registry = registry
self.child_registries = child_registries
self._context = {DependencyKey(type(self), DEFAULT_COMPONENT): self}
self._context = {CONTAINER_KEY: self}
if context:
for key, value in context.items():
if not isinstance(key, DependencyKey):
Expand Down Expand Up @@ -223,7 +223,7 @@ def make_container(
) -> Container:
registries = RegistryBuilder(
scopes=scopes,
container_type=Container,
container_key=CONTAINER_KEY,
providers=providers,
skip_validation=skip_validation,
validation_settings=validation_settings,
Expand Down Expand Up @@ -252,3 +252,6 @@ def make_container(
close_parent=True,
)
return container


CONTAINER_KEY = DependencyKey(Container, DEFAULT_COMPONENT)
6 changes: 3 additions & 3 deletions src/dishka/registry_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def __init__(
*,
scopes: type[BaseScope],
providers: Sequence[BaseProvider],
container_type: type,
container_key: DependencyKey,
skip_validation: bool,
validation_settings: ValidationSettings,
) -> None:
Expand All @@ -131,7 +131,7 @@ def __init__(
self.components: set[Component] = {DEFAULT_COMPONENT}
self.alias_sources: dict[DependencyKey, Any] = {}
self.aliases: dict[DependencyKey, Alias] = {}
self.container_type = container_type
self.container_key = container_key
self.decorator_depth: dict[DependencyKey, int] = defaultdict(int)
self.skip_validation = skip_validation
self.validation_settings = validation_settings
Expand Down Expand Up @@ -174,7 +174,7 @@ def _init_registries(self) -> None:
for scope in self.scopes:
registry = Registry(scope, has_fallback=has_fallback)
context_var = ContextVariable(
provides=DependencyKey(self.container_type, DEFAULT_COMPONENT),
provides=self.container_key,
scope=scope,
override=False,
)
Expand Down

0 comments on commit 6be79f6

Please sign in to comment.