mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2025-02-07 07:00:49 +03:00
Move inline functions from class level to module level for removing them from virtual table and enable inlining
This commit is contained in:
parent
b0d507b8bf
commit
0b836b8712
File diff suppressed because it is too large
Load Diff
|
@ -24,7 +24,12 @@ cdef class Callable(Provider):
|
|||
|
||||
cpdef object _provide(self, tuple args, dict kwargs)
|
||||
|
||||
cdef inline object __provide(self, tuple args, dict kwargs):
|
||||
|
||||
cdef class DelegatedCallable(Callable):
|
||||
pass
|
||||
|
||||
|
||||
cdef inline object __call(Callable self, tuple args, dict kwargs):
|
||||
cdef tuple positional_args
|
||||
cdef dict keyword_args
|
||||
|
||||
|
@ -36,7 +41,3 @@ cdef class Callable(Provider):
|
|||
self.__kwargs_len)
|
||||
|
||||
return self.__provides(*positional_args, **keyword_args)
|
||||
|
||||
|
||||
cdef class DelegatedCallable(Callable):
|
||||
pass
|
||||
|
|
|
@ -6,6 +6,7 @@ Powered by Cython.
|
|||
from dependency_injector.errors import Error
|
||||
|
||||
from .base cimport Provider
|
||||
from .callables cimport __call
|
||||
from .injections cimport (
|
||||
PositionalInjection,
|
||||
NamedInjection,
|
||||
|
@ -196,7 +197,7 @@ cdef class Callable(Provider):
|
|||
|
||||
cpdef object _provide(self, tuple args, dict kwargs):
|
||||
"""Return result of provided callable's call."""
|
||||
return self.__provide(args, kwargs)
|
||||
return __call(self, args, kwargs)
|
||||
|
||||
|
||||
cdef class DelegatedCallable(Callable):
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -4,7 +4,10 @@ Powered by Cython.
|
|||
"""
|
||||
|
||||
from .base cimport Provider
|
||||
from .callables cimport Callable
|
||||
from .callables cimport (
|
||||
Callable,
|
||||
__call as __call_callable,
|
||||
)
|
||||
from .injections cimport __inject_attributes
|
||||
|
||||
|
||||
|
@ -16,10 +19,15 @@ cdef class Factory(Provider):
|
|||
|
||||
cpdef object _provide(self, tuple args, dict kwargs)
|
||||
|
||||
cdef inline object __provide(self, tuple args, dict kwargs):
|
||||
|
||||
cdef class DelegatedFactory(Factory):
|
||||
pass
|
||||
|
||||
|
||||
cdef inline object __call(Factory self, tuple args, dict kwargs):
|
||||
cdef object instance
|
||||
|
||||
instance = self.__instantiator.__provide(args, kwargs)
|
||||
instance = __call_callable(self.__instantiator, args, kwargs)
|
||||
|
||||
if self.__attributes_len > 0:
|
||||
__inject_attributes(instance,
|
||||
|
@ -28,6 +36,3 @@ cdef class Factory(Provider):
|
|||
|
||||
return instance
|
||||
|
||||
|
||||
cdef class DelegatedFactory(Factory):
|
||||
pass
|
||||
|
|
|
@ -7,6 +7,7 @@ from dependency_injector.errors import Error
|
|||
|
||||
from .base cimport Provider
|
||||
from .callables cimport Callable
|
||||
from .factories cimport __call
|
||||
from .injections cimport (
|
||||
NamedInjection,
|
||||
parse_named_injections,
|
||||
|
@ -247,7 +248,7 @@ cdef class Factory(Provider):
|
|||
|
||||
cpdef object _provide(self, tuple args, dict kwargs):
|
||||
"""Return new instance."""
|
||||
return self.__provide(args, kwargs)
|
||||
return __call(self, args, kwargs)
|
||||
|
||||
|
||||
cdef class DelegatedFactory(Factory):
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -7,32 +7,25 @@ cimport cython
|
|||
|
||||
|
||||
cdef class Injection(object):
|
||||
pass
|
||||
|
||||
|
||||
cdef class PositionalInjection(Injection):
|
||||
cdef object __value
|
||||
cdef int __is_provider
|
||||
cdef int __is_delegated
|
||||
cdef int __call
|
||||
|
||||
cdef inline object __get_value(self):
|
||||
if self.__call == 0:
|
||||
return self.__value
|
||||
return self.__value()
|
||||
|
||||
cdef class PositionalInjection(Injection):
|
||||
pass
|
||||
|
||||
|
||||
cdef class NamedInjection(Injection):
|
||||
cdef object __name
|
||||
cdef object __value
|
||||
cdef int __is_provider
|
||||
cdef int __is_delegated
|
||||
cdef int __call
|
||||
|
||||
cdef inline object __get_name(self):
|
||||
|
||||
cdef inline object __get_name(NamedInjection self):
|
||||
return self.__name
|
||||
|
||||
cdef inline object __get_value(self):
|
||||
|
||||
cdef inline object __get_value(Injection self):
|
||||
if self.__call == 0:
|
||||
return self.__value
|
||||
return self.__value()
|
||||
|
@ -53,7 +46,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(__get_value(injection))
|
||||
positional_args.extend(args)
|
||||
|
||||
return tuple(positional_args)
|
||||
|
@ -71,14 +64,14 @@ cdef inline dict __provide_keyword_args(dict kwargs,
|
|||
if len(kwargs) == 0:
|
||||
for index in range(inj_kwargs_len):
|
||||
kw_injection = <NamedInjection>inj_kwargs[index]
|
||||
name = kw_injection.__get_name()
|
||||
kwargs[name] = kw_injection.__get_value()
|
||||
name = __get_name(kw_injection)
|
||||
kwargs[name] = __get_value(kw_injection)
|
||||
else:
|
||||
for index in range(inj_kwargs_len):
|
||||
kw_injection = <NamedInjection>inj_kwargs[index]
|
||||
name = kw_injection.__get_name()
|
||||
name = __get_name(kw_injection)
|
||||
if name not in kwargs:
|
||||
kwargs[name] = kw_injection.__get_value()
|
||||
kwargs[name] = __get_value(kw_injection)
|
||||
|
||||
return kwargs
|
||||
|
||||
|
@ -92,8 +85,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())
|
||||
__get_name(attr_injection),
|
||||
__get_value(attr_injection))
|
||||
|
||||
|
||||
cpdef tuple parse_positional_injections(tuple args)
|
||||
|
|
|
@ -5,6 +5,10 @@ Powered by Cython.
|
|||
|
||||
cimport cython
|
||||
|
||||
from .injections cimport (
|
||||
__get_name,
|
||||
__get_value,
|
||||
)
|
||||
from .utils cimport (
|
||||
is_provider,
|
||||
is_delegated,
|
||||
|
@ -37,7 +41,7 @@ cdef class PositionalInjection(Injection):
|
|||
|
||||
def get_value(self):
|
||||
"""Return injection value."""
|
||||
return self.__get_value()
|
||||
return __get_value(self)
|
||||
|
||||
def get_original_value(self):
|
||||
"""Return original value."""
|
||||
|
@ -67,11 +71,11 @@ cdef class NamedInjection(Injection):
|
|||
|
||||
def get_name(self):
|
||||
"""Return injection value."""
|
||||
return self.__get_name()
|
||||
return __get_name(self)
|
||||
|
||||
def get_value(self):
|
||||
"""Return injection value."""
|
||||
return self.__get_value()
|
||||
return __get_value(self)
|
||||
|
||||
def get_original_value(self):
|
||||
"""Return original value."""
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -16,11 +16,6 @@ cdef class Singleton(BaseSingleton):
|
|||
|
||||
cpdef object _provide(self, tuple args, dict kwargs)
|
||||
|
||||
cdef inline object __provide(self, tuple args, dict kwargs):
|
||||
if self.__storage is None:
|
||||
self.__storage = self.__instantiator.__provide(args, kwargs)
|
||||
return self.__storage
|
||||
|
||||
|
||||
cdef class DelegatedSingleton(Singleton):
|
||||
pass
|
||||
|
@ -32,12 +27,6 @@ cdef class ThreadSafeSingleton(BaseSingleton):
|
|||
|
||||
cpdef object _provide(self, tuple args, dict kwargs)
|
||||
|
||||
cdef inline object __provide(self, tuple args, dict kwargs):
|
||||
with self.__lock:
|
||||
if self.__storage is None:
|
||||
self.__storage = self.__instantiator.__provide(args, kwargs)
|
||||
return self.__storage
|
||||
|
||||
|
||||
cdef class DelegatedThreadSafeSingleton(ThreadSafeSingleton):
|
||||
pass
|
||||
|
@ -48,17 +37,6 @@ cdef class ThreadLocalSingleton(BaseSingleton):
|
|||
|
||||
cpdef object _provide(self, tuple args, dict kwargs)
|
||||
|
||||
cdef inline object __provide(self, tuple args, dict kwargs):
|
||||
cdef object instance
|
||||
|
||||
try:
|
||||
instance = self.__storage.instance
|
||||
except AttributeError:
|
||||
instance = self.__instantiator.__provide(args, kwargs)
|
||||
self.__storage.instance = instance
|
||||
finally:
|
||||
return instance
|
||||
|
||||
|
||||
cdef class DelegatedThreadLocalSingleton(ThreadLocalSingleton):
|
||||
pass
|
||||
|
|
|
@ -8,7 +8,10 @@ import threading
|
|||
from dependency_injector.errors import Error
|
||||
|
||||
from .base cimport Provider
|
||||
from .factories cimport Factory
|
||||
from .factories cimport (
|
||||
Factory,
|
||||
__call as __call_factory,
|
||||
)
|
||||
from .utils cimport (
|
||||
represent_provider,
|
||||
deepcopy,
|
||||
|
@ -251,7 +254,10 @@ cdef class Singleton(BaseSingleton):
|
|||
|
||||
cpdef object _provide(self, tuple args, dict kwargs):
|
||||
"""Return single instance."""
|
||||
return self.__provide(args, kwargs)
|
||||
if self.__storage is None:
|
||||
self.__storage = __call_factory(self.__instantiator,
|
||||
args, kwargs)
|
||||
return self.__storage
|
||||
|
||||
|
||||
cdef class DelegatedSingleton(Singleton):
|
||||
|
@ -303,7 +309,11 @@ cdef class ThreadSafeSingleton(BaseSingleton):
|
|||
|
||||
cpdef object _provide(self, tuple args, dict kwargs):
|
||||
"""Return single instance."""
|
||||
return self.__provide(args, kwargs)
|
||||
with self.__lock:
|
||||
if self.__storage is None:
|
||||
self.__storage = __call_factory(self.__instantiator,
|
||||
args, kwargs)
|
||||
return self.__storage
|
||||
|
||||
|
||||
cdef class DelegatedThreadSafeSingleton(ThreadSafeSingleton):
|
||||
|
@ -369,7 +379,15 @@ cdef class ThreadLocalSingleton(BaseSingleton):
|
|||
|
||||
cpdef object _provide(self, tuple args, dict kwargs):
|
||||
"""Return single instance."""
|
||||
return self.__provide(args, kwargs)
|
||||
cdef object instance
|
||||
|
||||
try:
|
||||
instance = self.__storage.instance
|
||||
except AttributeError:
|
||||
instance = __call_factory(self.__instantiator, args, kwargs)
|
||||
self.__storage.instance = instance
|
||||
finally:
|
||||
return instance
|
||||
|
||||
|
||||
cdef class DelegatedThreadLocalSingleton(ThreadLocalSingleton):
|
||||
|
|
Loading…
Reference in New Issue
Block a user