-
-
Notifications
You must be signed in to change notification settings - Fork 34.3k
gh-126835: Disable tuple folding in the AST optimizer #128802
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
2f475e1
7a96d47
6d93343
02a38a3
5c05d69
05f3e62
a3d9790
9f1feb4
ac50aad
2b39a13
17dffaa
46845f0
e9631d8
53dfa93
9754820
477c784
6731dfb
aad9fb3
be40093
5aec965
ee69f0f
0801463
a268315
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -157,13 +157,27 @@ def test_folding_of_tuples_of_constants(self): | |
| ('a,b,c,d = 1,2,3,4', (1, 2, 3, 4)), | ||
| ('(None, 1, None)', (None, 1, None)), | ||
| ('((1, 2), 3, 4)', ((1, 2), 3, 4)), | ||
| ('(1, 2, (3, 4))', (1, 2, (3, 4))), | ||
| ('()', ()), | ||
| ('(1, (2, (3, (4, (5,)))))', (1, (2, (3, (4, (5,)))))), | ||
| ): | ||
| with self.subTest(line=line): | ||
| code = compile(line,'','single') | ||
| self.assertInBytecode(code, 'LOAD_CONST', elem) | ||
| self.assertNotInBytecode(code, 'BUILD_TUPLE') | ||
| self.check_lnotab(code) | ||
|
|
||
| for expr, length in ( | ||
| ('(1, a)', 2), | ||
| ('(a, b, c)', 3), | ||
| ('(a, (b, c))', 2), | ||
| ('(1, [], {})', 3), | ||
| ): | ||
| with self.subTest(expr=expr, length=length): | ||
| code = compile(expr, '', 'single') | ||
| self.assertInBytecode(code, 'BUILD_TUPLE', length) | ||
| self.check_lnotab(code) | ||
|
|
||
| # Long tuples should be folded too, but their length should not | ||
| # exceed the `STACK_USE_GUIDELINE` | ||
| code = compile(repr(tuple(range(30))),'','single') | ||
|
Comment on lines
+181
to
+183
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How much of a concern is that we can no longer create constant tuples beyond length of
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's probably not ideal in case you have some kind of large constant lookup table for instance. As Kirill pointed out, this would get compiled to a bunch of
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not a hard pattern to detect, right? Maybe we could fold it anyway? @Eclips4
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems quite predictable: >>> def foo():
... return (1,2,3, ... ,31)
...
>>> dis.dis(foo)
1 RESUME 0
2 BUILD_LIST 0
LOAD_SMALL_INT 1
LIST_APPEND 1
LOAD_SMALL_INT 2
LIST_APPEND 1
...
LOAD_SMALL_INT 31
LIST_APPEND 1
CALL_INTRINSIC_1 6 (INTRINSIC_LIST_TO_TUPLE)(Could also be
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exactly. I think we can easily fold it.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we could check with
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it's a good idea. We would be creating dependency between both.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So if we are going to fold constant tuple beyond |
||
|
|
@@ -346,28 +360,6 @@ def negzero(): | |
| self.assertInBytecode(code, opname) | ||
| self.check_lnotab(code) | ||
|
|
||
| def test_folding_of_tuples_on_constants(self): | ||
| tests =[ | ||
| ('()', True, 0), | ||
| ('(1, 2, 3)', True, 3), | ||
| ('("a", "b", "c")', True, 3), | ||
| ('(1, a)', False, 2), | ||
| ('(a, b, c)', False, 3), | ||
| ('(1, (2, 3))', True, 2), | ||
| ('(a, (b, c))', False, 2), | ||
| ('(1, [], {})', False, 3), | ||
| (repr(tuple(range(30))), True, 30), | ||
| ('(1, (2, (3, (4, (5)))))', True, 2) | ||
| ] | ||
| for expr, is_const, length in tests: | ||
| with self.subTest(expr=expr, is_const=is_const, length=length): | ||
| code = compile(expr, '', 'eval') | ||
| if is_const: | ||
| self.assertNotInBytecode(code, 'BUILD_TUPLE', length) | ||
| self.assertInBytecode(code, 'LOAD_CONST', eval(expr)) | ||
| else: | ||
| self.assertInBytecode(code, 'BUILD_TUPLE', length) | ||
|
|
||
| def test_elim_extra_return(self): | ||
| # RETURN LOAD_CONST None RETURN --> RETURN | ||
| def f(x): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we perhaps add a test that tuples longer than
STACK_USE_GUIDELINEare in fact not folded?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about it. At the moment, for constant tuples which are longer than
STACK_USE_GUIDELINEwill be generated following bytecode:Shall we assert that this intrinsic is presented in bytecode?