#27513: Optimize Signal.send a tiny bit
-------------------------------------+-------------------------------------
Reporter: Adam Chainz | Owner: Adam
Type: | Chainz
Cleanup/optimization | Status: assigned
Component: Utilities | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Adam Chainz):
`send()` with receivers can be optimized too with a list comprehension,
avoiding temp var `response` and method calls on `responses`:
{{{
In [1]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:def before(self, sender, **named):
for receiver in self._live_receivers(sender):
response = receiver(signal=self, sender=sender, **named)
responses.append((receiver, response))
return responses
def after(self, sender, **named):
return [
(receiver, receiver(signal=self, sender=sender, **named))
for receiver in self._live_receivers(sender)
]
:<EOF>
In [3]: dis.dis(before)
2 0 SETUP_LOOP 66 (to 69)
3 LOAD_FAST 0 (self)
6 LOAD_ATTR 0 (_live_receivers)
9 LOAD_FAST 1 (sender)
12 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
15 GET_ITER
>> 16 FOR_ITER 49 (to 68)
19 STORE_FAST 3 (receiver)
3 22 LOAD_FAST 3 (receiver)
25 LOAD_CONST 1 ('signal')
28 LOAD_FAST 0 (self)
31 LOAD_CONST 2 ('sender')
34 LOAD_FAST 1 (sender)
37 LOAD_FAST 2 (named)
40 CALL_FUNCTION_KW 512 (0 positional, 2 keyword pair)
43 STORE_FAST 4 (response)
4 46 LOAD_GLOBAL 1 (responses)
49 LOAD_ATTR 2 (append)
52 LOAD_FAST 3 (receiver)
55 LOAD_FAST 4 (response)
58 BUILD_TUPLE 2
61 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
64 POP_TOP
65 JUMP_ABSOLUTE 16
>> 68 POP_BLOCK
5 >> 69 LOAD_GLOBAL 1 (responses)
72 RETURN_VALUE
In [4]: dis.dis(after)
10 0 LOAD_CLOSURE 0 (named)
3 LOAD_CLOSURE 1 (self)
6 LOAD_CLOSURE 2 (sender)
9 BUILD_TUPLE 3
12 LOAD_CONST 1 (<code object <listcomp> at
0x1068b5930, file "<ipython-input-1-cece5b56e5de>", line 10>)
15 LOAD_CONST 2 ('after.<locals>.<listcomp>')
18 MAKE_CLOSURE 0
11 21 LOAD_DEREF 1 (self)
24 LOAD_ATTR 0 (_live_receivers)
27 LOAD_DEREF 2 (sender)
30 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
33 GET_ITER
34 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
37 RETURN_VALUE
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27513#comment:4>
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/068.79d8b0eb6730a585e8b8cffb3ed91e7d%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.