Issue 1031667: Fold tuples of constants into a single constant (original) (raw)

Issue1031667

Created on 2004-09-21 04:23 by rhettinger, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
constfold.diff rhettinger,2004-09-21 04:23 Diff to compile.c and test.test_peepholer.py
Messages (4)
msg22504 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-09-21 04:23
Folds tuples of constants into a single constant. Results in beautiful, fast bytecode: >>> from dis import dis >>> def f(): for x in ('a', 'b', 'c'): if x in (1, 2): return False return True >>> dis(f) 2 0 SETUP_LOOP 35 (to 38) 3 LOAD_CONST 6 (('a', 'b', 'c')) 6 GET_ITER >> 7 FOR_ITER 27 (to 37) 10 STORE_FAST 0 (x) 3 13 LOAD_FAST 0 (x) 16 LOAD_CONST 7 ((1, 2)) 19 COMPARE_OP 6 (in) 22 JUMP_IF_FALSE 8 (to 33) 25 POP_TOP 4 26 LOAD_GLOBAL 1 (False) 29 RETURN_VALUE 30 JUMP_ABSOLUTE 7 >> 33 POP_TOP 34 JUMP_ABSOLUTE 7 >> 37 POP_BLOCK 5 >> 38 LOAD_GLOBAL 2 (True) 41 RETURN_VALUE
msg22505 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2004-09-21 19:08
Logged In: YES user_id=21627 There is a change in semantics, of course: the program def f(): return 1,2 print f() is f() used to print False, now prints True.
msg22506 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2004-09-21 19:29
Logged In: YES user_id=31435 That's OK. The language reference manual has always been careful to say that an expression returning an immutable object may or may not return a pre-existing object. "for immutable types, operations that compute new values may actually return a reference to any existing object with the same type and value, while for mutable objects this is not allowed" So, e.g., had your function been def f(): return () it was already true, under CPython, that f() is f(). No correct Python program could rely on that, though; neither on that only the empty tuple has been shared until now.
msg22507 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-09-22 18:44
Logged In: YES user_id=80475 See Python/compile.c 2.327
History
Date User Action Args
2022-04-11 14:56:07 admin set github: 40937
2004-09-21 04:23:02 rhettinger create