GH-115506: Improve handling of constants in tier two#124809
Closed
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds a
refstuple to executor objects, which contains constants created during optimization of tier two traces. This isn't deduplicated at all, since not all of our "constants" are actually "constant", and arbitrary hashes/comparisons can open up a whole can of worms. If we want to go that route, we can probably re-use_PyCode_ConstantKeyfor known safe, immutable types, and compare everything else by identity.This also updates some parts of the optimizer to improve the handling of known constants (such as adding peepholing for
_POP_TOP_LOAD_CONST_INLINE_BORROW,_REPLACE_WITH_TRUE, and_COPY/_LOAD_FASTwith known constant values).Performance and memory are in the noise... perhaps a bit faster if you squint hard enough. But it's working: the stats show lots of instructions like
_REPLACE_WITH_TRUE,_POP_TOP_LOAD_CONST_INLINE_BORROW,_LOAD_FAST,_COPY, and_BINARY_OP_ADD_INTbeing replaced with simpler instructions like_LOAD_CONST_INLINE_BORROW,_POP_TOP, and_LOAD_CONST_INLINE.My next step will be experimenting with no-refcount variants of
_COPY,_LOAD_FAST,_STORE_FAST, and_POP_TOPwith known immortal values.