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