Skip to content

Simplified and improved django.utils.functional.lazy(). - #16954

Merged
felixxm merged 6 commits into
django:mainfrom
ngnpope:simplify-lazy
Jun 12, 2023
Merged

felixxm merged 6 commits into
django:mainfrom
ngnpope:simplify-lazy

Conversation

@ngnpope

@ngnpope ngnpope commented Jun 7, 2023

Copy link
Copy Markdown
Member

Supersedes PR #11314.

All comments from there should be addressed.

Aside from removing much cruft left over from the Python 2 days related to str vs bytes and the simplicity and consistency of eagerly preparing the proxy, there is some improvement in performance too in certain cases.

Before
Python 3.10.10 (main, Mar  5 2023, 22:26:53) [GCC 12.2.1 20230201]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.12.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from django.utils.functional import lazy
   ...: def identity(x): return x
   ...: %prun for i in range(10000): str(lazy(identity)(1))
         380003 function calls in 0.172 seconds

   Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    10000    0.059    0.000    0.075    0.000 {built-in method builtins.__build_class__}
    10000    0.017    0.000    0.017    0.000 functional.py:85(__proxy__)
    10000    0.015    0.000    0.024    0.000 functools.py:35(update_wrapper)
    80000    0.014    0.000    0.014    0.000 {built-in method builtins.setattr}
   150000    0.011    0.000    0.011    0.000 {built-in method builtins.getattr}
    10000    0.010    0.000    0.035    0.000 functools.py:188(total_ordering)
    10000    0.010    0.000    0.147    0.000 functional.py:77(lazy)
    10000    0.008    0.000    0.014    0.000 functools.py:191(<setcomp>)
        1    0.007    0.007    0.172    0.172 <string>:1(<module>)
    10000    0.005    0.000    0.008    0.000 functional.py:95(__init__)
    10000    0.003    0.000    0.004    0.000 functional.py:146(__cast)
    10000    0.003    0.000    0.003    0.000 functional.py:111(__prepare_class__)
    10000    0.003    0.000    0.007    0.000 functional.py:152(__str__)
    10000    0.002    0.000    0.011    0.000 functional.py:188(__wrapper__)
    10000    0.002    0.000    0.002    0.000 functools.py:65(wraps)
    10000    0.001    0.000    0.001    0.000 {built-in method builtins.max}
    10000    0.001    0.000    0.001    0.000 {method 'update' of 'dict' objects}
    10000    0.001    0.000    0.001    0.000 <ipython-input-1-79cd33187a71>:2(identity)
        1    0.000    0.000    0.172    0.172 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

In [2]: from django.utils.functional import lazy
   ...: def identity(x): return x
   ...: %prun for i in range(10000): lazy(identity, str)("a").upper()
         2440003 function calls in 0.807 seconds

   Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    10000    0.333    0.000    0.650    0.000 functional.py:111(__prepare_class__)
   940000    0.158    0.000    0.158    0.000 {built-in method builtins.hasattr}
   550000    0.104    0.000    0.104    0.000 functional.py:132(__promise__)
   630000    0.069    0.000    0.069    0.000 {built-in method builtins.setattr}
    10000    0.031    0.000    0.052    0.000 {built-in method builtins.__build_class__}
    10000    0.021    0.000    0.021    0.000 functional.py:85(__proxy__)
    10000    0.017    0.000    0.027    0.000 functools.py:35(update_wrapper)
   160000    0.014    0.000    0.014    0.000 {built-in method builtins.getattr}
    10000    0.011    0.000    0.131    0.000 functional.py:77(lazy)
    10000    0.011    0.000    0.039    0.000 functools.py:188(total_ordering)
    10000    0.009    0.000    0.016    0.000 functools.py:191(<setcomp>)
        1    0.008    0.008    0.807    0.807 <string>:1(<module>)
    10000    0.006    0.000    0.009    0.000 functional.py:135(__wrapper__)
    10000    0.005    0.000    0.656    0.000 functional.py:95(__init__)
    10000    0.003    0.000    0.659    0.000 functional.py:188(__wrapper__)
    10000    0.002    0.000    0.002    0.000 functools.py:65(wraps)
    10000    0.001    0.000    0.001    0.000 {built-in method builtins.max}
    10000    0.001    0.000    0.001    0.000 {method 'mro' of 'type' objects}
    10000    0.001    0.000    0.001    0.000 {method 'update' of 'dict' objects}
    10000    0.001    0.000    0.001    0.000 <ipython-input-2-b74d5df0c298>:2(identity)
    10000    0.001    0.000    0.001    0.000 {method 'upper' of 'str' objects}
        1    0.000    0.000    0.807    0.807 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
After
Python 3.10.10 (main, Mar  5 2023, 22:26:53) [GCC 12.2.1 20230201]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.12.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from django.utils.functional import lazy
   ...: def identity(x): return x
   ...: %prun for i in range(10000): str(lazy(identity)(1))
         230003 function calls in 0.126 seconds

   Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    10000    0.041    0.000    0.041    0.000 functional.py:85(__proxy__)
    10000    0.029    0.000    0.070    0.000 {built-in method builtins.__build_class__}
    10000    0.016    0.000    0.026    0.000 functools.py:35(update_wrapper)
    10000    0.009    0.000    0.107    0.000 functional.py:77(lazy)
        1    0.007    0.007    0.126    0.126 <string>:1(<module>)
    70000    0.005    0.000    0.005    0.000 {built-in method builtins.getattr}
    50000    0.004    0.000    0.004    0.000 {built-in method builtins.setattr}
    10000    0.003    0.000    0.006    0.000 functional.py:151(__str__)
    10000    0.003    0.000    0.005    0.000 functional.py:195(__wrapper__)
    10000    0.002    0.000    0.003    0.000 functional.py:96(__cast)
    10000    0.002    0.000    0.002    0.000 functools.py:65(wraps)
    10000    0.002    0.000    0.002    0.000 functional.py:92(__init__)
    10000    0.001    0.000    0.001    0.000 {method 'update' of 'dict' objects}
    10000    0.001    0.000    0.001    0.000 <ipython-input-1-79cd33187a71>:2(identity)
        1    0.000    0.000    0.126    0.126 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

In [2]: from django.utils.functional import lazy
   ...: def identity(x): return x
   ...: %prun for i in range(10000): lazy(identity, str)("a").upper()
         1720003 function calls in 0.556 seconds

   Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    10000    0.250    0.000    0.537    0.000 functional.py:77(lazy)
   940000    0.146    0.000    0.146    0.000 {built-in method builtins.hasattr}
   580000    0.050    0.000    0.050    0.000 {built-in method builtins.setattr}
    10000    0.033    0.000    0.033    0.000 functional.py:85(__proxy__)
    10000    0.031    0.000    0.064    0.000 {built-in method builtins.__build_class__}
    10000    0.018    0.000    0.028    0.000 functools.py:35(update_wrapper)
        1    0.007    0.007    0.556    0.556 <string>:1(<module>)
    80000    0.006    0.000    0.006    0.000 {built-in method builtins.getattr}
    10000    0.005    0.000    0.007    0.000 functional.py:187(__wrapper__)
    10000    0.002    0.000    0.005    0.000 functional.py:195(__wrapper__)
    10000    0.002    0.000    0.002    0.000 functools.py:65(wraps)
    10000    0.002    0.000    0.002    0.000 functional.py:92(__init__)
    10000    0.001    0.000    0.001    0.000 {method 'mro' of 'type' objects}
    10000    0.001    0.000    0.001    0.000 {method 'update' of 'dict' objects}
    10000    0.001    0.000    0.001    0.000 <ipython-input-2-b74d5df0c298>:2(identity)
    10000    0.001    0.000    0.001    0.000 {method 'upper' of 'str' objects}
        1    0.000    0.000    0.556    0.556 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

In [3]: import django
   ...: from django.conf import settings
   ...: from django.utils.translation import gettext_lazy
   ...: settings.configure()
   ...: django.setup()
   ...: %prun for i in range(10000): gettext_lazy("Do you know your %s?") % "ABC"

         260530 function calls (250524 primitive calls) in 0.084 seconds

   Ordered by: internal time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
    10000    0.017    0.000    0.031    0.000 local.py:44(_get_context_id)
    10000    0.008    0.000    0.046    0.000 local.py:99(__getattr__)
    10000    0.008    0.000    0.068    0.000 trans_real.py:368(gettext)
20030/10027    0.006    0.000    0.051    0.000 {built-in method builtins.getattr}
    10000    0.005    0.000    0.037    0.000 local.py:80(_get_storage)
        1    0.004    0.004    0.084    0.084 <string>:1(<module>)
    10000    0.003    0.000    0.006    0.000 sync.py:545(get_current_task)
    10000    0.003    0.000    0.007    0.000 gettext.py:496(gettext)
    10000    0.003    0.000    0.076    0.000 functional.py:172(__mod__)
    10000    0.003    0.000    0.004    0.000 trans_real.py:113(get)
    10000    0.002    0.000    0.071    0.000 __init__.py:94(gettext)
    10000    0.002    0.000    0.004    0.000 functional.py:195(__wrapper__)
    10001    0.002    0.000    0.003    0.000 <frozen importlib._bootstrap>:404(parent)
    10000    0.002    0.000    0.073    0.000 functional.py:96(__cast)
    10000    0.002    0.000    0.003    0.000 threading.py:1430(current_thread)
    10000    0.002    0.000    0.003    0.000 tasks.py:35(current_task)
    10000    0.002    0.000    0.002    0.000 functional.py:92(__init__)
    20000    0.001    0.000    0.001    0.000 {method 'replace' of 'str' objects}
    10000    0.001    0.000    0.001    0.000 {built-in method _asyncio.get_running_loop}
    10006    0.001    0.000    0.001    0.000 {method 'rpartition' of 'str' objects}
    10024    0.001    0.000    0.001    0.000 {built-in method builtins.isinstance}
    10002    0.001    0.000    0.001    0.000 {built-in method _thread.get_ident}
    10014    0.001    0.000    0.001    0.000 {built-in method builtins.hasattr}
    10000    0.001    0.000    0.001    0.000 {built-in method sys.getrecursionlimit}
    10006    0.001    0.000    0.001    0.000 {method 'get' of 'dict' objects}
        1    0.000    0.000    0.000    0.000 gettext.py:374(_parse)
      2/1    0.000    0.000    0.084    0.084 {built-in method builtins.exec}
        2    0.000    0.000    0.000    0.000 dispatcher.py:50(connect)
        7    0.000    0.000    0.000    0.000 {built-in method posix.stat}
    18/16    0.000    0.000    0.000    0.000 functional.py:279(__getattribute__)
        5    0.000    0.000    0.000    0.000 posixpath.py:71(join)
        1    0.000    0.000    0.000    0.000 {built-in method marshal.loads}
        1    0.000    0.000    0.000    0.000 {built-in method io.open_code}
        1    0.000    0.000    0.000    0.000 gettext.py:542(find)
        1    0.000    0.000    0.000    0.000 trans_real.py:141(__init__)
        1    0.000    0.000    0.000    0.000 __init__.py:62(__getattr__)
        1    0.000    0.000    0.000    0.000 gettext.py:211(_expand_lang)
        1    0.000    0.000    0.000    0.000 <frozen importlib._bootstrap_external>:1536(find_spec)
        1    0.000    0.000    0.000    0.000 gettext.py:583(translation)
...

@felixxm

felixxm commented Jun 8, 2023

Copy link
Copy Markdown
Member

@ngnpope Thanks 👍

I've moved one of the commits to the #16957 with a regression test, as it seems to be a bugfix related to the ticket-34445.

@felixxm

felixxm commented Jun 8, 2023

Copy link
Copy Markdown
Member

I moved and reorganized the first six commit and "total ordering" removal in #16958. Please rebase when it's merged.

@felixxm

felixxm commented Jun 8, 2023

Copy link
Copy Markdown
Member

I moved and reorganized the first six commit and "total ordering" removal in #16958. Please rebase when it's merged.

Merged 🎉

@ngnpope

ngnpope commented Jun 8, 2023

Copy link
Copy Markdown
Member Author

Rebased.

Comment thread django/utils/functional.py Resolved
Comment thread django/utils/functional.py Resolved
@ngnpope

ngnpope commented Jun 8, 2023

Copy link
Copy Markdown
Member Author

Ok. I've rejigged everything, polished up the commit messages, etc. Hopefully this is in a better state now.

Comment thread tests/utils_tests/test_functional.py Resolved
@ngnpope
ngnpope force-pushed the simplify-lazy branch 2 times, most recently from 8b40efc to 2dcfe69 Compare June 11, 2023 19:40
@ngnpope
ngnpope requested a review from felixxm June 11, 2023 19:41
ngnpope and others added 6 commits June 12, 2023 05:29
This order reads more naturally and puts methods into three groups:

1. The methods required to support the implementation of __proxy__, e.g.
   __deepcopy__ doesn't come from `object` and __reduce__ is
   overridden to support behavior required explicitly for pickling of
   lazy objects.

2. Methods that are specifically overridden from `object` which we
   don't want to inherit from the provided resultclasses. These will be
   skipped later when we add methods from the resultclasses.

3. Additional methods - that is _add__, __radd__, and __mod__ - don't
   come from `object`, but typically from `str` and `int` which are the
   most common use cases.

Co-authored-by: Nick Pope <[email protected]>
Previously, the proxy class was prepared lazily:

  lazy_identity = lazy(identity, int)
  lazy_identity(10)  # prepared here
  lazy_identity(10)

This has a slight advantage that if the lazy doesn't end up getting
used, the preparation work is skipped, however that's not very likely.

Besides this laziness, it is also inconsistent in that the methods which
are wrapped directly (__str__ etc.) are prepared already when __proxy__
is defined, and there is a weird half-initialized state.

This change it so that everything is prepared already on the first line
of the example above.
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 allows for formatting of lazy objects which have a custom formatter
defined by overriding the default implementation from `object`.
@felixxm felixxm changed the title Simplified django.utils.functional.lazy(). Simplified and improved django.utils.functional.lazy(). Jun 12, 2023

@felixxm felixxm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ngnpope Thanks 👍

@felixxm
felixxm merged commit e042024 into django:main Jun 12, 2023
@ngnpope
ngnpope deleted the simplify-lazy branch June 12, 2023 06:56
@ngnpope

ngnpope commented Jun 12, 2023

Copy link
Copy Markdown
Member Author

Thanks @felixxm. Four years in the making, @bluetech, but we got there! 😅

@felixxm

felixxm commented Jun 12, 2023

Copy link
Copy Markdown
Member

Thanks @felixxm. Four years in the making, @bluetech, but we got there! sweat_smile

It's a marathon 🏃 , not sprint 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants