commit d6f4803402221b6e0b4a52ffc3ee586b0dd6a93c Author: Zach Byrne Date: Tue Feb 16 08:37:25 2016 -0700 BINARY_SUBSCR fast path for lists and tuples with small integers diff --git a/Python/ceval.c b/Python/ceval.c index 8904d7a..b5e9bb5 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -1591,7 +1591,30 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) TARGET(BINARY_SUBSCR) { PyObject *sub = POP(); PyObject *container = TOP(); - PyObject *res = PyObject_GetItem(container, sub); + PyObject *res; + Py_ssize_t index; + int slow_path = 1; + if (PyLong_CheckExact(sub) && Py_ABS(Py_SIZE(sub)) <= 1) { + index = PyLong_AsSsize_t(sub); + if (index < 0) { + index += Py_SIZE(container); + } + if (index >= 0 && index < Py_SIZE(container)) { + if (PyList_CheckExact(container)) { + res = PyList_GetItem(container, index); + Py_INCREF(res); + slow_path = 0; + } + else if (PyTuple_CheckExact(container)) { + res = PyTuple_GetItem(container, index); + Py_INCREF(res); + slow_path = 0; + } + } + } + if (slow_path) { + res = PyObject_GetItem(container, sub); + } Py_DECREF(container); Py_DECREF(sub); SET_TOP(res);