Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 35.2k
bpo-37207: Use PEP 590 vectorcall to speed up range(), list() and dict() by about 30%#13930
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
c01d5b56075fc19d86ea520eea92821af670049db88d4a250989a3d7525ff6cc319141File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Speed up calls to ``range()``, ``list()`` and ``dict()`` by about 30%, by | ||
| using the PEP 590 ``vectorcall`` calling convention. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -121,6 +121,66 @@ range_new(PyTypeObject *type, PyObject *args, PyObject *kw) | ||
| return NULL; | ||
| } | ||
| static PyObject * | ||
| range_vectorcall(PyTypeObject *type, PyObject *const *args, | ||
| size_t nargsf, PyObject *kwnames) | ||
| { | ||
| rangeobject *obj; | ||
| size_t nargs = PyVectorcall_NARGS(nargsf); | ||
| PyObject *start = NULL, *stop = NULL, *step = NULL; | ||
| if (kwnames && PyTuple_GET_SIZE(kwnames) != 0) { | ||
| PyErr_Format(PyExc_TypeError, "range() takes no keyword arguments"); | ||
| return NULL; | ||
| } | ||
| switch(nargs) { | ||
| case 0: | ||
| PyErr_Format(PyExc_TypeError, "range() expected 1 arguments, got 0"); | ||
| return NULL; | ||
| case 1: | ||
| stop = PyNumber_Index(args[0]); | ||
| if (!stop) | ||
| return NULL; | ||
| Py_INCREF(_PyLong_Zero); | ||
| start = _PyLong_Zero; | ||
| Py_INCREF(_PyLong_One); | ||
| step = _PyLong_One; | ||
| break; | ||
| case 3: | ||
| step = args[2]; | ||
| //Intentional fall through | ||
| case 2: | ||
| /* Convert borrowed refs to owned refs */ | ||
| start = PyNumber_Index(args[0]); | ||
| if (!start) | ||
| return NULL; | ||
| stop = PyNumber_Index(args[1]); | ||
| if (!stop) { | ||
| Py_DECREF(start); | ||
| return NULL; | ||
markshannon marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| step = validate_step(step); /* Caution, this can clear exceptions */ | ||
| if (!step) { | ||
| Py_DECREF(start); | ||
| Py_DECREF(stop); | ||
| return NULL; | ||
| } | ||
| break; | ||
| default: | ||
| PyErr_Format(PyExc_TypeError, "range() expected at most 3 arguments, got %zu", nargs); | ||
| return NULL; | ||
| } | ||
| obj = make_range_object(type, start, stop, step); | ||
| if (obj != NULL) | ||
| return (PyObject *) obj; | ||
| /* Failed to create object, release attributes */ | ||
| Py_DECREF(start); | ||
| Py_DECREF(stop); | ||
| Py_DECREF(step); | ||
| return NULL; | ||
| } | ||
| PyDoc_STRVAR(range_doc, | ||
| "range(stop) -> range object\n\ | ||
| range(start, stop[, step]) -> range object\n\ | ||
| @@ -702,6 +762,7 @@ PyTypeObject PyRange_Type = { | ||
| 0, /* tp_init */ | ||
| 0, /* tp_alloc */ | ||
| range_new, /* tp_new */ | ||
| .tp_vectorcall = (vectorcallfunc)range_vectorcall | ||
| }; | ||
| /*********************** range Iterator **************************/ | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -5170,6 +5170,11 @@ inherit_slots(PyTypeObject *type, PyTypeObject *base) | ||||||
| !(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) | ||||||
| { | ||||||
| type->tp_flags |= _Py_TPFLAGS_HAVE_VECTORCALL; | ||||||
| /* Also inherit tp_vectorcall if tp_call of the | ||||||
| * metaclass is type.__call__ */ | ||||||
Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
| ||||||
| if (Py_TYPE(type)->tp_call == PyType_Type.tp_call) { | ||||||
| type->tp_vectorcall = base->tp_vectorcall; | ||||||
| } | ||||||
| } | ||||||
| COPYSLOT(tp_call); | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like two same identifiers in single source file.
Please make
_Py_IDENTIFIER(keys);global static variable.