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
co_stacksize is calculated from unoptimized code #70736
Comments
When answering a question on StackOverflow, I noticed that a function that only loads a constant tuple to a local variable still has a large e.g. >>> def foo():
... a = (1,2,3,4,5,6,7,8,9,10)
...
>>> foo.__code__.co_stacksize
10
>>> dis.dis(foo)
2 0 LOAD_CONST 11 ((1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
3 STORE_FAST 0 (a)
6 LOAD_CONST 0 (None)
9 RETURN_VALUE I suspect it is because in the |
I also suspect you're right, Antti, that the stack size is calculated prior to the bytecode being passed to through the peepholer which would have made the built tuple a value in the const array. Off the top of my head I don't remember where the stack size calculation is made, but my suspicion is it's in the AT -> bytecode step, which would mean making it work from bytecode would mean re-implementing that calculation to work from the bytecode itself (assuming I'm right). |
This also affects co_consts, which includes constants that are no longer used by the optimized code: In [8]: def f(): In [9]: f.func_code.co_consts In [12]: dis.dis(f) |
Strictly speaking, the stack size is calculated *after* the peephole optimizer is run, but the code that computes the stack depth operates on the basic block graph instead of the assembled and optimized byte code. Anyway, the conclusion is the same as Brett noted -- the stack depth analysis would need to be re-written to operate on the optimized bytecode array. |
See also bpo-24340 |
Seems moving constant folding to the AST level (bpo-29469) have solved this issue. >>> def foo():
... a = (1,2,3,4,5,6,7,8,9,10)
...
>>> foo.__code__.co_stacksize
1 |
Wow, it's cool to see this bug finally fixed! |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: