Move inline functions from class level to module level for removing them from virtual table and enable inlining

This commit is contained in:
Roman Mogilatov 2016-11-17 23:33:25 +02:00
parent b0d507b8bf
commit 0b836b8712
12 changed files with 2549 additions and 3325 deletions

File diff suppressed because it is too large Load Diff

View File

@ -24,7 +24,12 @@ 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 class DelegatedCallable(Callable):
pass
cdef inline object __call(Callable self, tuple args, dict kwargs):
cdef tuple positional_args cdef tuple positional_args
cdef dict keyword_args cdef dict keyword_args
@ -36,7 +41,3 @@ cdef class Callable(Provider):
self.__kwargs_len) self.__kwargs_len)
return self.__provides(*positional_args, **keyword_args) return self.__provides(*positional_args, **keyword_args)
cdef class DelegatedCallable(Callable):
pass

View File

@ -6,6 +6,7 @@ Powered by Cython.
from dependency_injector.errors import Error from dependency_injector.errors import Error
from .base cimport Provider from .base cimport Provider
from .callables cimport __call
from .injections cimport ( from .injections cimport (
PositionalInjection, PositionalInjection,
NamedInjection, NamedInjection,
@ -196,7 +197,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."""
return self.__provide(args, kwargs) return __call(self, args, kwargs)
cdef class DelegatedCallable(Callable): cdef class DelegatedCallable(Callable):

File diff suppressed because it is too large Load Diff

View File

@ -4,7 +4,10 @@ Powered by Cython.
""" """
from .base cimport Provider from .base cimport Provider
from .callables cimport Callable from .callables cimport (
Callable,
__call as __call_callable,
)
from .injections cimport __inject_attributes from .injections cimport __inject_attributes
@ -16,10 +19,15 @@ cdef class Factory(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 class DelegatedFactory(Factory):
pass
cdef inline object __call(Factory self, tuple args, dict kwargs):
cdef object instance cdef object instance
instance = self.__instantiator.__provide(args, kwargs) instance = __call_callable(self.__instantiator, args, kwargs)
if self.__attributes_len > 0: if self.__attributes_len > 0:
__inject_attributes(instance, __inject_attributes(instance,
@ -28,6 +36,3 @@ cdef class Factory(Provider):
return instance return instance
cdef class DelegatedFactory(Factory):
pass

View File

@ -7,6 +7,7 @@ from dependency_injector.errors import Error
from .base cimport Provider from .base cimport Provider
from .callables cimport Callable from .callables cimport Callable
from .factories cimport __call
from .injections cimport ( from .injections cimport (
NamedInjection, NamedInjection,
parse_named_injections, parse_named_injections,
@ -247,7 +248,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."""
return self.__provide(args, kwargs) return __call(self, args, kwargs)
cdef class DelegatedFactory(Factory): cdef class DelegatedFactory(Factory):

File diff suppressed because it is too large Load Diff

View File

@ -7,32 +7,25 @@ cimport cython
cdef class Injection(object): cdef class Injection(object):
pass
cdef class PositionalInjection(Injection):
cdef object __value cdef object __value
cdef int __is_provider cdef int __is_provider
cdef int __is_delegated cdef int __is_delegated
cdef int __call cdef int __call
cdef inline object __get_value(self):
if self.__call == 0: cdef class PositionalInjection(Injection):
return self.__value pass
return self.__value()
cdef class NamedInjection(Injection): cdef class NamedInjection(Injection):
cdef object __name 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 return self.__name
cdef inline object __get_value(self):
cdef inline object __get_value(Injection self):
if self.__call == 0: if self.__call == 0:
return self.__value return self.__value
return self.__value() return self.__value()
@ -53,7 +46,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(__get_value(injection))
positional_args.extend(args) positional_args.extend(args)
return tuple(positional_args) return tuple(positional_args)
@ -71,14 +64,14 @@ cdef inline dict __provide_keyword_args(dict kwargs,
if len(kwargs) == 0: if len(kwargs) == 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]
name = kw_injection.__get_name() name = __get_name(kw_injection)
kwargs[name] = kw_injection.__get_value() kwargs[name] = __get_value(kw_injection)
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 = __get_name(kw_injection)
if name not in kwargs: if name not in kwargs:
kwargs[name] = kw_injection.__get_value() kwargs[name] = __get_value(kw_injection)
return kwargs return kwargs
@ -92,8 +85,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(), __get_name(attr_injection),
attr_injection.__get_value()) __get_value(attr_injection))
cpdef tuple parse_positional_injections(tuple args) cpdef tuple parse_positional_injections(tuple args)

View File

@ -5,6 +5,10 @@ Powered by Cython.
cimport cython cimport cython
from .injections cimport (
__get_name,
__get_value,
)
from .utils cimport ( from .utils cimport (
is_provider, is_provider,
is_delegated, is_delegated,
@ -37,7 +41,7 @@ cdef class PositionalInjection(Injection):
def get_value(self): def get_value(self):
"""Return injection value.""" """Return injection value."""
return self.__get_value() return __get_value(self)
def get_original_value(self): def get_original_value(self):
"""Return original value.""" """Return original value."""
@ -67,11 +71,11 @@ cdef class NamedInjection(Injection):
def get_name(self): def get_name(self):
"""Return injection value.""" """Return injection value."""
return self.__get_name() return __get_name(self)
def get_value(self): def get_value(self):
"""Return injection value.""" """Return injection value."""
return self.__get_value() return __get_value(self)
def get_original_value(self): def get_original_value(self):
"""Return original value.""" """Return original value."""

File diff suppressed because it is too large Load Diff

View File

@ -16,11 +16,6 @@ cdef class Singleton(BaseSingleton):
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):
if self.__storage is None:
self.__storage = self.__instantiator.__provide(args, kwargs)
return self.__storage
cdef class DelegatedSingleton(Singleton): cdef class DelegatedSingleton(Singleton):
pass pass
@ -32,12 +27,6 @@ cdef class ThreadSafeSingleton(BaseSingleton):
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):
with self.__lock:
if self.__storage is None:
self.__storage = self.__instantiator.__provide(args, kwargs)
return self.__storage
cdef class DelegatedThreadSafeSingleton(ThreadSafeSingleton): cdef class DelegatedThreadSafeSingleton(ThreadSafeSingleton):
pass pass
@ -48,17 +37,6 @@ cdef class ThreadLocalSingleton(BaseSingleton):
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
try:
instance = self.__storage.instance
except AttributeError:
instance = self.__instantiator.__provide(args, kwargs)
self.__storage.instance = instance
finally:
return instance
cdef class DelegatedThreadLocalSingleton(ThreadLocalSingleton): cdef class DelegatedThreadLocalSingleton(ThreadLocalSingleton):
pass pass

View File

@ -8,7 +8,10 @@ import threading
from dependency_injector.errors import Error from dependency_injector.errors import Error
from .base cimport Provider from .base cimport Provider
from .factories cimport Factory from .factories cimport (
Factory,
__call as __call_factory,
)
from .utils cimport ( from .utils cimport (
represent_provider, represent_provider,
deepcopy, deepcopy,
@ -251,7 +254,10 @@ cdef class Singleton(BaseSingleton):
cpdef object _provide(self, tuple args, dict kwargs): cpdef object _provide(self, tuple args, dict kwargs):
"""Return single instance.""" """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): cdef class DelegatedSingleton(Singleton):
@ -303,7 +309,11 @@ cdef class ThreadSafeSingleton(BaseSingleton):
cpdef object _provide(self, tuple args, dict kwargs): cpdef object _provide(self, tuple args, dict kwargs):
"""Return single instance.""" """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): cdef class DelegatedThreadSafeSingleton(ThreadSafeSingleton):
@ -369,7 +379,15 @@ cdef class ThreadLocalSingleton(BaseSingleton):
cpdef object _provide(self, tuple args, dict kwargs): cpdef object _provide(self, tuple args, dict kwargs):
"""Return single instance.""" """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): cdef class DelegatedThreadLocalSingleton(ThreadLocalSingleton):