mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-02-16 19:40:59 +03:00
Improve factory performance by adding direct inline C calls
This commit is contained in:
parent
b2f6a2cd1a
commit
7502fa1e89
|
@ -120,7 +120,6 @@ cdef class Provider(object):
|
||||||
if not is_provider(provider):
|
if not is_provider(provider):
|
||||||
provider = Object(provider)
|
provider = Object(provider)
|
||||||
|
|
||||||
print(self.__overridden, provider)
|
|
||||||
self.__overridden += (provider,)
|
self.__overridden += (provider,)
|
||||||
self.__overridden_len += 1
|
self.__overridden_len += 1
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,8 @@ from .base cimport Provider
|
||||||
from .injections cimport (
|
from .injections cimport (
|
||||||
PositionalInjection,
|
PositionalInjection,
|
||||||
NamedInjection,
|
NamedInjection,
|
||||||
|
__provide_positional_args,
|
||||||
|
__provide_keyword_args,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -22,6 +24,19 @@ cdef class Callable(Provider):
|
||||||
|
|
||||||
cpdef object _provide(self, tuple args, dict kwargs)
|
cpdef object _provide(self, tuple args, dict kwargs)
|
||||||
|
|
||||||
|
cdef inline object __provide(self, tuple args, dict kwargs):
|
||||||
|
cdef tuple positional_args
|
||||||
|
cdef dict keyword_args
|
||||||
|
|
||||||
|
positional_args = __provide_positional_args(args,
|
||||||
|
self.__args,
|
||||||
|
self.__args_len)
|
||||||
|
keyword_args = __provide_keyword_args(kwargs,
|
||||||
|
self.__kwargs,
|
||||||
|
self.__kwargs_len)
|
||||||
|
|
||||||
|
return self.__provides(*positional_args, **keyword_args)
|
||||||
|
|
||||||
|
|
||||||
cdef class DelegatedCallable(Callable):
|
cdef class DelegatedCallable(Callable):
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -11,8 +11,6 @@ from .injections cimport (
|
||||||
NamedInjection,
|
NamedInjection,
|
||||||
parse_positional_injections,
|
parse_positional_injections,
|
||||||
parse_named_injections,
|
parse_named_injections,
|
||||||
__provide_positional_args,
|
|
||||||
__provide_keyword_args,
|
|
||||||
)
|
)
|
||||||
from .utils import represent_provider
|
from .utils import represent_provider
|
||||||
|
|
||||||
|
@ -180,17 +178,7 @@ cdef class Callable(Provider):
|
||||||
|
|
||||||
cpdef object _provide(self, tuple args, dict kwargs):
|
cpdef object _provide(self, tuple args, dict kwargs):
|
||||||
"""Return result of provided callable's call."""
|
"""Return result of provided callable's call."""
|
||||||
cdef tuple positional_args
|
return self.__provide(args, kwargs)
|
||||||
cdef dict keyword_args
|
|
||||||
|
|
||||||
positional_args = __provide_positional_args(args,
|
|
||||||
self.__args,
|
|
||||||
self.__args_len)
|
|
||||||
keyword_args = __provide_keyword_args(kwargs,
|
|
||||||
self.__kwargs,
|
|
||||||
self.__kwargs_len)
|
|
||||||
|
|
||||||
return self.__provides(*positional_args, **keyword_args)
|
|
||||||
|
|
||||||
|
|
||||||
cdef class DelegatedCallable(Callable):
|
cdef class DelegatedCallable(Callable):
|
||||||
|
|
|
@ -4,16 +4,30 @@ Powered by Cython.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from .base cimport Provider
|
from .base cimport Provider
|
||||||
|
from .callables cimport Callable
|
||||||
|
from .injections cimport __inject_attributes
|
||||||
|
|
||||||
|
|
||||||
cdef class Factory(Provider):
|
cdef class Factory(Provider):
|
||||||
cdef object __instantiator
|
cdef Callable __instantiator
|
||||||
|
|
||||||
cdef tuple __attributes
|
cdef tuple __attributes
|
||||||
cdef int __attributes_len
|
cdef int __attributes_len
|
||||||
|
|
||||||
cpdef object _provide(self, tuple args, dict kwargs)
|
cpdef object _provide(self, tuple args, dict kwargs)
|
||||||
|
|
||||||
|
cdef inline object __provide(self, tuple args, dict kwargs):
|
||||||
|
cdef object instance
|
||||||
|
|
||||||
|
instance = self.__instantiator.__provide(args, kwargs)
|
||||||
|
|
||||||
|
if self.__attributes_len > 0:
|
||||||
|
__inject_attributes(instance,
|
||||||
|
self.__attributes,
|
||||||
|
self.__attributes_len)
|
||||||
|
|
||||||
|
return instance
|
||||||
|
|
||||||
|
|
||||||
cdef class DelegatedFactory(Factory):
|
cdef class DelegatedFactory(Factory):
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -8,13 +8,8 @@ from dependency_injector.errors import Error
|
||||||
from .base cimport Provider
|
from .base cimport Provider
|
||||||
from .callables cimport Callable
|
from .callables cimport Callable
|
||||||
from .injections cimport (
|
from .injections cimport (
|
||||||
PositionalInjection,
|
|
||||||
NamedInjection,
|
NamedInjection,
|
||||||
parse_positional_injections,
|
|
||||||
parse_named_injections,
|
parse_named_injections,
|
||||||
__provide_positional_args,
|
|
||||||
__provide_keyword_args,
|
|
||||||
__inject_attributes,
|
|
||||||
)
|
)
|
||||||
from .utils import represent_provider
|
from .utils import represent_provider
|
||||||
|
|
||||||
|
@ -88,14 +83,11 @@ cdef class Factory(Provider):
|
||||||
raise Error('{0} can provide only {1} instances'.format(
|
raise Error('{0} can provide only {1} instances'.format(
|
||||||
self.__class__, self.__class__.provided_type))
|
self.__class__, self.__class__.provided_type))
|
||||||
|
|
||||||
self.__instantiator = Callable(provides)
|
self.__instantiator = Callable(provides, *args, **kwargs)
|
||||||
|
|
||||||
self.__attributes = tuple()
|
self.__attributes = tuple()
|
||||||
self.__attributes_len = 0
|
self.__attributes_len = 0
|
||||||
|
|
||||||
self.set_args(*args)
|
|
||||||
self.set_kwargs(**kwargs)
|
|
||||||
|
|
||||||
super(Factory, self).__init__()
|
super(Factory, self).__init__()
|
||||||
|
|
||||||
|
|
||||||
|
@ -236,16 +228,7 @@ cdef class Factory(Provider):
|
||||||
|
|
||||||
cpdef object _provide(self, tuple args, dict kwargs):
|
cpdef object _provide(self, tuple args, dict kwargs):
|
||||||
"""Return new instance."""
|
"""Return new instance."""
|
||||||
cdef object instance
|
return self.__provide(args, kwargs)
|
||||||
|
|
||||||
instance = self.__instantiator._provide(args, kwargs)
|
|
||||||
|
|
||||||
if self.__attributes_len > 0:
|
|
||||||
__inject_attributes(instance,
|
|
||||||
self.__attributes,
|
|
||||||
self.__attributes_len)
|
|
||||||
|
|
||||||
return instance
|
|
||||||
|
|
||||||
|
|
||||||
cdef class DelegatedFactory(Factory):
|
cdef class DelegatedFactory(Factory):
|
||||||
|
|
|
@ -53,7 +53,7 @@ cdef inline tuple __provide_positional_args(tuple args,
|
||||||
positional_args = list()
|
positional_args = list()
|
||||||
for index in range(inj_args_len):
|
for index in range(inj_args_len):
|
||||||
injection = <PositionalInjection>inj_args[index]
|
injection = <PositionalInjection>inj_args[index]
|
||||||
positional_args.append(injection.get_value())
|
positional_args.append(injection.__get_value())
|
||||||
positional_args.extend(args)
|
positional_args.extend(args)
|
||||||
|
|
||||||
return tuple(positional_args)
|
return tuple(positional_args)
|
||||||
|
@ -66,20 +66,19 @@ cdef inline dict __provide_keyword_args(dict kwargs,
|
||||||
int inj_kwargs_len):
|
int inj_kwargs_len):
|
||||||
cdef int index
|
cdef int index
|
||||||
cdef object name
|
cdef object name
|
||||||
cdef int kwargs_len
|
|
||||||
cdef NamedInjection kw_injection
|
cdef NamedInjection kw_injection
|
||||||
|
|
||||||
kwargs_len = len(kwargs)
|
if len(kwargs) == 0:
|
||||||
if kwargs_len == 0:
|
|
||||||
for index in range(inj_kwargs_len):
|
for index in range(inj_kwargs_len):
|
||||||
kw_injection = <NamedInjection>inj_kwargs[index]
|
kw_injection = <NamedInjection>inj_kwargs[index]
|
||||||
kwargs[kw_injection.get_name()] = kw_injection.get_value()
|
name = kw_injection.__get_name()
|
||||||
|
kwargs[name] = kw_injection.__get_value()
|
||||||
else:
|
else:
|
||||||
for index in range(inj_kwargs_len):
|
for index in range(inj_kwargs_len):
|
||||||
kw_injection = <NamedInjection>inj_kwargs[index]
|
kw_injection = <NamedInjection>inj_kwargs[index]
|
||||||
name = kw_injection.get_name()
|
name = kw_injection.__get_name()
|
||||||
if name not in kwargs:
|
if name not in kwargs:
|
||||||
kwargs[name] = kw_injection.get_value()
|
kwargs[name] = kw_injection.__get_value()
|
||||||
|
|
||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
|
@ -93,8 +92,8 @@ cdef inline object __inject_attributes(object instance,
|
||||||
for index in range(attributes_len):
|
for index in range(attributes_len):
|
||||||
attr_injection = <NamedInjection>attributes[index]
|
attr_injection = <NamedInjection>attributes[index]
|
||||||
setattr(instance,
|
setattr(instance,
|
||||||
attr_injection.get_name(),
|
attr_injection.__get_name(),
|
||||||
attr_injection.get_value())
|
attr_injection.__get_value())
|
||||||
|
|
||||||
|
|
||||||
cpdef tuple parse_positional_injections(tuple args)
|
cpdef tuple parse_positional_injections(tuple args)
|
||||||
|
|
|
@ -23,7 +23,8 @@ cdef class PositionalInjection(Injection):
|
||||||
self.__value = value
|
self.__value = value
|
||||||
self.__is_provider = <int>is_provider(value)
|
self.__is_provider = <int>is_provider(value)
|
||||||
self.__is_delegated = <int>is_delegated(value)
|
self.__is_delegated = <int>is_delegated(value)
|
||||||
self.__call = <int>self.__is_provider == 1 and self.__is_delegated == 0
|
self.__call = <int>(self.__is_provider == 1 and
|
||||||
|
self.__is_delegated == 0)
|
||||||
|
|
||||||
def get_value(self):
|
def get_value(self):
|
||||||
"""Return injection value."""
|
"""Return injection value."""
|
||||||
|
@ -43,7 +44,8 @@ cdef class NamedInjection(Injection):
|
||||||
self.__value = value
|
self.__value = value
|
||||||
self.__is_provider = <int>is_provider(value)
|
self.__is_provider = <int>is_provider(value)
|
||||||
self.__is_delegated = <int>is_delegated(value)
|
self.__is_delegated = <int>is_delegated(value)
|
||||||
self.__call = <int>self.__is_provider == 1 and self.__is_delegated == 0
|
self.__call = <int>(self.__is_provider == 1 and
|
||||||
|
self.__is_delegated == 0)
|
||||||
|
|
||||||
def get_name(self):
|
def get_name(self):
|
||||||
"""Return injection value."""
|
"""Return injection value."""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user