﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
27513	Optimize Signal.send a tiny bit	Adam Johnson	Adam Johnson	"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
}}}
"	Cleanup/optimization	closed	Utilities	dev	Normal	fixed		me@…	Ready for checkin	1	0	0	0	0	0
