[Python-checkins] bpo-45636: Remove the old %-formatting fast-path (GH-29532)

brandtbucher webhook-mailer at python.org
Mon Nov 15 11:58:32 EST 2021


https://github.com/python/cpython/commit/ec382fac0db6d9159c2d3496a70b7a605545957e
commit: ec382fac0db6d9159c2d3496a70b7a605545957e
branch: main
author: Brandt Bucher <brandt at python.org>
committer: brandtbucher <brandtbucher at gmail.com>
date: 2021-11-15T08:58:23-08:00
summary:

bpo-45636: Remove the old %-formatting fast-path (GH-29532)

files:
A Misc/NEWS.d/next/Core and Builtins/2021-11-11-19-11-57.bpo-45636.2fyIVm.rst
M Python/ceval.c
M Python/specialize.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-11-11-19-11-57.bpo-45636.2fyIVm.rst b/Misc/NEWS.d/next/Core and Builtins/2021-11-11-19-11-57.bpo-45636.2fyIVm.rst
new file mode 100644
index 0000000000000..f705b41fd8c2d
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-11-11-19-11-57.bpo-45636.2fyIVm.rst	
@@ -0,0 +1,2 @@
+Remove an existing "fast path" for old-style string formatting, since
+it no longer appears to have any measurable impact.
diff --git a/Python/ceval.c b/Python/ceval.c
index 0b24cbdcc2cc4..e808aeed73a22 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -4711,14 +4711,6 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, InterpreterFrame *frame, int thr
                     res = PyNumber_Multiply(lhs, rhs);
                     break;
                 case NB_REMAINDER:
-                    if (PyUnicode_CheckExact(lhs) &&
-                        (!PyUnicode_Check(rhs) || PyUnicode_CheckExact(rhs)))
-                    {
-                        // bpo-28598: Fast path for string formatting (but not
-                        // if the RHS is a str subclass).
-                        res = PyUnicode_Format(lhs, rhs);
-                        break;
-                    }
                     res = PyNumber_Remainder(lhs, rhs);
                     break;
                 case NB_OR:
diff --git a/Python/specialize.c b/Python/specialize.c
index 7e72013cd8359..cfc21bf70ad6b 100644
--- a/Python/specialize.c
+++ b/Python/specialize.c
@@ -1380,13 +1380,13 @@ _Py_Specialize_BinaryOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr,
                         SpecializedCacheEntry *cache)
 {
     _PyAdaptiveEntry *adaptive = &cache->adaptive;
-    if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) {
-        SPECIALIZATION_FAIL(BINARY_OP, SPEC_FAIL_DIFFERENT_TYPES);
-        goto failure;
-    }
     switch (adaptive->original_oparg) {
         case NB_ADD:
         case NB_INPLACE_ADD:
+            if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) {
+                SPECIALIZATION_FAIL(BINARY_OP, SPEC_FAIL_DIFFERENT_TYPES);
+                goto failure;
+            }
             if (PyUnicode_CheckExact(lhs)) {
                 if (_Py_OPCODE(instr[1]) == STORE_FAST && Py_REFCNT(lhs) == 2) {
                     *instr = _Py_MAKECODEUNIT(BINARY_OP_INPLACE_ADD_UNICODE,
@@ -1409,6 +1409,10 @@ _Py_Specialize_BinaryOp(PyObject *lhs, PyObject *rhs, _Py_CODEUNIT *instr,
             break;
         case NB_MULTIPLY:
         case NB_INPLACE_MULTIPLY:
+            if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) {
+                SPECIALIZATION_FAIL(BINARY_OP, SPEC_FAIL_DIFFERENT_TYPES);
+                goto failure;
+            }
             if (PyLong_CheckExact(lhs)) {
                 *instr = _Py_MAKECODEUNIT(BINARY_OP_MULTIPLY_INT,
                                           _Py_OPARG(*instr));



More information about the Python-checkins mailing list