#27513: Optimize Signal.send a tiny bit
------------------------------------------------+------------------------
Reporter: Adam Chainz | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: Utilities | Version: 1.10
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------------+------------------------
Signals often have no listeners so their `send()` is a no-op. #16639 tried
to optimize the model init signals to be faster for this case, but came
out too complicated.
One micro optimization can be done in the current no-op code is to avoid
assigning the empty list to a variable before returning it, which is less
operations in the Python virtual machine, for example:
{{{
In [5]: import dis
In [6]: def foo1():
...: responses = []
...: if True:
...: return responses
...:
In [7]: dis.dis(foo1)
2 0 BUILD_LIST 0
3 STORE_FAST 0 (responses)
3 6 LOAD_GLOBAL 0 (True)
9 POP_JUMP_IF_FALSE 16
4 12 LOAD_FAST 0 (responses)
15 RETURN_VALUE
>> 16 LOAD_CONST 0 (None)
19 RETURN_VALUE
In [8]: def foo2():
...: if True:
...: return []
...:
In [9]: dis.dis(foo2)
2 0 LOAD_GLOBAL 0 (True)
3 POP_JUMP_IF_FALSE 10
3 6 BUILD_LIST 0
9 RETURN_VALUE
>> 10 LOAD_CONST 0 (None)
13 RETURN_VALUE
}}}
Timing the two above example functions gives:
{{{
In [13]: %timeit foo1()
10000000 loops, best of 3: 160 ns per loop
In [14]: %timeit foo2()
10000000 loops, best of 3: 121 ns per loop
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27513>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/053.14c53abe049576da6b4164c58bcc185d%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.