[Python-Dev] Micro-optimizations by adding special-case bytecodes?

Ben Hoyt benhoyt at gmail.com
Wed May 24 14:07:30 EDT 2017


Hi folks,

I was looking at some `dis` output today, and I was wondering if anyone has
investigated optimizing Python (slightly) by adding special-case bytecodes
for common expressions or statements involving constants?

For example, I (and, based on a quick grep of the stdlib, many others)
write "x is None" and "x is not None" very often. Or "return True" or
"return None" or "return 1" and things like that. These all expand into two
bytecodes, which seems pretty non-optimal (LOAD_CONST + COMPARE_OP or
LOAD_CONST + RETURN_VALUE). It seems we could get an easy speedup for these
common cases by adding a peephole optimization and some new opcodes (maybe
COMPARE_IS_SMALL_CONST and RETURN_SMALL_CONST for these cases).

I'm not proposing to do this yet, as I'd need to benchmark to see how much
of a gain (if any) it would amount to, but I'm just wondering if there's
any previous work on this kind of thing. Or, if not, any other thoughts
before I try it?

Thanks,
Ben
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20170524/0f74a701/attachment.html>


More information about the Python-Dev mailing list