gh-154910: Constant-fold f-strings with constant fields in the compiler - #154911
Open
overlorde wants to merge 1 commit into
Open
gh-154910: Constant-fold f-strings with constant fields in the compiler#154911overlorde wants to merge 1 commit into
overlorde wants to merge 1 commit into
Conversation
…compiler Fold FORMAT_SIMPLE on constants of exact type str, int, float or bool in the flowgraph optimizer, mirroring the instruction's own fast path: formatting an exact str is the identity, and formatting the other three types cannot call user code. BUILD_STRING over constant strings is then folded to a single string constant, so f-strings built entirely from constant fields compile to one LOAD_CONST. Fields with a conversion or a format spec are unaffected: CONVERT_VALUE is not a constant load, so the fold does not match, and FORMAT_WITH_SPEC is left for a possible follow-up.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The flowgraph optimizer folds constant expressions in most positions, but f-strings with constant fields were left unfolded. This adds two folds:
fold_format_simple:LOAD_CONST c, FORMAT_SIMPLEbecomesLOAD_CONST format(c)whencis a constant of exact typestr,int,floatorbool. This mirrors the fast path of theFORMAT_SIMPLEinstruction itself: formatting an exactstris the identity (the instruction is simply removed), and formatting the other three types cannot call user code, so the result is known at compile time.fold_build_string_of_constants:BUILD_STRINGover constant strings becomes a single string constant, the same shape asfold_tuple_of_constants.Together they compile an f-string built entirely from constant fields down to one
LOAD_CONST:Partly-constant f-strings also benefit: the constant fields drop their
FORMAT_SIMPLE, so e.g.f'{"prefix"}{x}'performs one runtime format instead of two.Fields with a conversion or a format spec are not touched:
CONVERT_VALUEis not a constant load, so the fold doesn't matchf'{1!r}', andFORMAT_WITH_SPECis left as a possible follow-up (it is pure for the same types when the spec is constant).On a debug free-threaded build,
f'{1}{2}'went from ~1770 ns/call to ~34 ns/call, the same as the literal'12'.