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

Stephane Wirtel stephane at wirtel.be
Wed May 24 14:12:15 EDT 2017


there is the Peephole for that, it's a small optimizer in the compiler.

On 05/24/2017 08:07 PM, Ben Hoyt wrote:
> 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
> 
> 
> 
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/stephane%40wirtel.be
> 


More information about the Python-Dev mailing list