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
Optimize sequences of constants in the compiler #77506
Comments
The following PR makes three optimizations in the compiler.
For example, "{'a': 1, 'b': 2, 'c': 3}" is currently compiled to 1 0 LOAD_CONST 0 (1) With this optimization it will be compiled to: 1 0 LOAD_CONST 5 ((('a', 'b', 'c'), 3, 2, 1))
x = [1, 2, 3, 4, 5]
y = {1, 2, 3, 4, 5} currently is compiled to 1 0 LOAD_CONST 0 (1) 2 14 LOAD_CONST 0 (1) With optimization 1 it will be compiled to 1 0 LOAD_CONST 6 ((5, 4, 3, 2, 1)) 2 8 LOAD_CONST 6 ((5, 4, 3, 2, 1)) And with optimization 2 it will be compiled to 1 0 LOAD_CONST 0 ((1, 2, 3, 4, 5)) 2 6 LOAD_CONST 1 (frozenset({1, 2, 3, 4, 5}))
After folding tuples of constants created at code generation level, eliminating unreachable code, and after the above two optimizations, unused constants are left in the co_consts tuple. The third optimization removes them and reenumerate constants in the order of occurrence. The above example will be compiled to: 1 0 LOAD_CONST 0 ((1, 2, 3, 4, 5)) 2 6 LOAD_CONST 1 (frozenset({1, 2, 3, 4, 5})) See bpo-28813 for the implementation of this optimization on the level of raw bytecode (in peephole.c). These optimizations are useful not only for initializing collections of constants. Calling function with constant arguments "f(x, a=1, b=2)": Current: Optimized: This issue depends on bpo-33318. |
There seems to be an implicit assumption here that fewer bytecodes is better. But that isn't always the case. Do you have evidence that the sequence The second sequence has more bytecodes, but the first has to create a new object. I think you ought to be careful using the word "optimize" unless the output is incontrovertibly superior. |
Good point! Thank you Mark for your comment. In theory, fewer bytecodes is better (the first example doesn't create a new object, it operates with references to existing objects). But in practice the difference is very small and overwhelmed by random factors. We need hundreds or thousands of constants to get a stable result. This is far from a common use case. |
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: