gh-77273: Better bytecodes for f-strings by markshannon · Pull Request #6132 · python/cpython (original) (raw)
Simplify and speed up interpreter for f-strings.
tldr; Do work in the compiler instead of the interpreter.
Splits the FORMAT_VALUE opcode into CONVERT_VALUE, FORMAT_SIMPLE and FORMAT_WITH_SPEC.
The compiler can then emit the optimal sequence for each format expression.
The three new opcodes are much simpler than the one they replace, actually removing a few lines of code from the interpreter.
The CONVERT_VALUE is emitted only if a conversion is present.
In the standard library only (approx) 20% of f-strings include a conversion.
FORMAT_WITH_SPEC is emitted if a format specifier is present.
Otherwise FORMAT_SIMPLE is emitted.
In the standard library about 70% of format expressions can be formatted with just the simple (and fast)
FORMAT_SIMPLE opcode.