mirror of
https://github.com/django/django.git
synced 2024-12-23 01:25:58 +00:00
Removed function call overhead when preparing proxy class for lazy().
We can avoid the function call and assignment overhead which could be significant when instantiating many lazy objects. It's still easy enough to read too.
This commit is contained in:
parent
ae94077e7d
commit
45466f11f2
@ -163,16 +163,6 @@ def lazy(func, *resultclasses):
|
|||||||
def __mod__(self, other):
|
def __mod__(self, other):
|
||||||
return self.__cast() % other
|
return self.__cast() % other
|
||||||
|
|
||||||
def __promise__(method_name):
|
|
||||||
# Builds a wrapper around some method.
|
|
||||||
def __wrapper__(self, *args, **kw):
|
|
||||||
# Automatically triggers the evaluation of a lazy value and
|
|
||||||
# applies the given method of the result type.
|
|
||||||
res = func(*self._args, **self._kw)
|
|
||||||
return getattr(res, method_name)(*args, **kw)
|
|
||||||
|
|
||||||
return __wrapper__
|
|
||||||
|
|
||||||
# Add wrappers for all methods from resultclasses which haven't been
|
# Add wrappers for all methods from resultclasses which haven't been
|
||||||
# wrapped explicitly above.
|
# wrapped explicitly above.
|
||||||
for resultclass in resultclasses:
|
for resultclass in resultclasses:
|
||||||
@ -182,8 +172,16 @@ def lazy(func, *resultclasses):
|
|||||||
# the correct implementation when called.
|
# the correct implementation when called.
|
||||||
if hasattr(__proxy__, method_name):
|
if hasattr(__proxy__, method_name):
|
||||||
continue
|
continue
|
||||||
meth = __promise__(method_name)
|
|
||||||
setattr(__proxy__, method_name, meth)
|
# Builds a wrapper around some method. Pass method_name to
|
||||||
|
# avoid issues due to late binding.
|
||||||
|
def __wrapper__(self, *args, __method_name=method_name, **kw):
|
||||||
|
# Automatically triggers the evaluation of a lazy value and
|
||||||
|
# applies the given method of the result type.
|
||||||
|
result = func(*self._args, **self._kw)
|
||||||
|
return getattr(result, __method_name)(*args, **kw)
|
||||||
|
|
||||||
|
setattr(__proxy__, method_name, __wrapper__)
|
||||||
|
|
||||||
@wraps(func)
|
@wraps(func)
|
||||||
def __wrapper__(*args, **kw):
|
def __wrapper__(*args, **kw):
|
||||||
|
Loading…
Reference in New Issue
Block a user