Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c52d713

Browse files
committed
Add fast-path for comparing interned (true) string objects.
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).
1 parent f0b11d2 commit c52d713

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

Python/ceval.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,6 +1778,21 @@ eval_frame(PyFrameObject *f)
17781778
x = res ? Py_True : Py_False;
17791779
Py_INCREF(x);
17801780
}
1781+
else if (v == w && PyString_CheckExact(v)) {
1782+
/* Fast-path for comparing interned strings */
1783+
switch (oparg) {
1784+
case EQ: x = Py_True; break;
1785+
case LE: x = Py_True; break;
1786+
case GE: x = Py_True; break;
1787+
case NE: x = Py_False; break;
1788+
case GT: x = Py_False; break;
1789+
case LT: x = Py_False; break;
1790+
case IS: x = Py_True; break;
1791+
case IS_NOT: x = Py_False; break;
1792+
default: goto slow_compare;
1793+
}
1794+
Py_INCREF(x);
1795+
}
17811796
else {
17821797
slow_compare:
17831798
x = cmp_outcome(oparg, v, w);

0 commit comments

Comments
 (0)