[1.8.x] Optimized allow_lazy() by not generating a new lazy wrapper on each invocation.
This dramatically improves performance on PyPy. The following benchmark:
python -mtimeit -s "from django.utils.functional import allow_lazy; from django.utils.translation import ugettext_lazy; f = allow_lazy(lambda s: s, str)" "f(ugettext_lazy('abc'))"
goes from 390us per loop to 165us.
Backport of 82e0cd1571
from master
This commit is contained in:
parent
b44a56c308
commit
ee86bf24d2
|
@ -205,6 +205,8 @@ def allow_lazy(func, *resultclasses):
|
||||||
immediately, otherwise a __proxy__ is returned that will evaluate the
|
immediately, otherwise a __proxy__ is returned that will evaluate the
|
||||||
function when needed.
|
function when needed.
|
||||||
"""
|
"""
|
||||||
|
lazy_func = lazy(func, *resultclasses)
|
||||||
|
|
||||||
@wraps(func)
|
@wraps(func)
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
for arg in list(args) + list(six.itervalues(kwargs)):
|
for arg in list(args) + list(six.itervalues(kwargs)):
|
||||||
|
@ -212,7 +214,7 @@ def allow_lazy(func, *resultclasses):
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
return func(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
return lazy(func, *resultclasses)(*args, **kwargs)
|
return lazy_func(*args, **kwargs)
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
empty = object()
|
empty = object()
|
||||||
|
|
Loading…
Reference in New Issue