]> granicus.if.org Git - python/commitdiff
Add fast-path for comparing interned (true) string objects.
authorMarc-André Lemburg <mal@egenix.com>
Wed, 7 Nov 2001 14:54:49 +0000 (14:54 +0000)
committerMarc-André Lemburg <mal@egenix.com>
Wed, 7 Nov 2001 14:54:49 +0000 (14:54 +0000)
This patch boosts performance for comparing identical string object
by some 20% on my machine while not causing any noticable slow-down
for other operations (according to tests done with pybench).

Python/ceval.c

index b70773434965600f004d821a4104e1b3b9a0effc..34b4cb954d88eb5a17565b14ee1beb33a5732e7c 100644 (file)
@@ -1778,6 +1778,21 @@ eval_frame(PyFrameObject *f)
                                x = res ? Py_True : Py_False;
                                Py_INCREF(x);
                        }
+                       else if (v == w && PyString_CheckExact(v)) {
+                               /* Fast-path for comparing interned strings */
+                               switch (oparg) {
+                               case EQ: x = Py_True; break;
+                               case LE: x = Py_True; break;
+                               case GE: x = Py_True; break;
+                               case NE: x = Py_False; break;
+                               case GT: x = Py_False; break;
+                               case LT: x = Py_False; break;
+                               case IS: x = Py_True; break;
+                               case IS_NOT: x = Py_False; break;
+                               default: goto slow_compare;
+                               }
+                               Py_INCREF(x);
+                       }
                        else {
                          slow_compare:
                                x = cmp_outcome(oparg, v, w);