Attached patch adds a new bytecode instruction: RETURN_NONE. It is similar to "LOAD_CONST; RETURN_VALUE" but it avoids the need of adding None to constants of the code object. For function with an empty body, it reduces the stack size from 1 to 0.
Example on reference Python 3.7:
>>> def func(): return
...
>>> import dis; dis.dis(func)
1 0 LOAD_CONST 0 (None)
2 RETURN_VALUE
>>> func.__code__.co_stacksize
1
Example on patched Python 3.7:
>>> def func(): return
...
>>> import dis; dis.dis(func)
1 0 RETURN_CONST
>>> func.__code__.co_stacksize
0
If the function has a docstring, RETURN_CONST avoids adding None to code constants:
>>> def func():
... "docstring"
... return
...
>>> func.__code__.co_consts
('docstring',)
I will now run benchmarks on the patch. |