[issue6690] BUILD_SET followed by COMPARE_OP (in) can be optimized if all items are consts

Dave Malcolm report at bugs.python.org
Tue Jan 12 20:36:22 CET 2010


Dave Malcolm <dmalcolm at redhat.com> added the comment:

Attaching a probably over-simplistic attempt at this patch, against the py3k branch.

This patch attempts to extend the replacement of
  LOAD_CONST, ...., LOAD_CONST, BUILD_LIST, COMPARE_OP(in)
with
  LOAD_CONST(tuple), COMPAREOP(in)

so that it also replaces:
  LOAD_CONST, ...., LOAD_CONST, BUILD_SET, COMPARE_OP(in)
with 
  LOAD_CONST(tuple), COMPAREOP(in)

i.e. using a tuple, not a frozenset (on the grounds that the "in" conditions should at least still work); caveat: I'm relatively new to the innards of this code.

With this patch:
>>> dis.dis(lambda o: o in {1,2,3})
  1           0 LOAD_FAST                0 (o) 
              3 LOAD_CONST               3 ((1, 2, 3)) 
              6 COMPARE_OP               6 (in) 
              9 RETURN_VALUE         
but:
>>> dis.dis(lambda o: o in {1,2,3,3,2,1})
  1           0 LOAD_FAST                0 (o) 
              3 LOAD_CONST               3 ((1, 2, 3, 3, 2, 1)) 
              6 COMPARE_OP               6 (in) 
              9 RETURN_VALUE         

Is it worth me working on a rewrite to use a frozenset.

Hope this is helpful
Dave

----------
keywords: +patch
nosy: +dmalcolm
Added file: http://bugs.python.org/file15840/simple-optimization-of-BUILD_SET-COMPARE_OP(IN)-to-LOAD_CONST(tuple)_COMPARE_OP(IN)-py3k.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue6690>
_______________________________________


More information about the Python-bugs-list mailing list