-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
gh-118218: Reuse return tuple in itertools.pairwise #118219
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
Changes from all commits
3bd6a58
f653f22
ea0cfd6
68b5d27
1bfddd6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Speed up :func:`itertools.pairwise` in the common case by up to 1.8x. |
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -270,6 +270,7 @@ typedef struct { | |||||||||||||||
PyObject_HEAD | ||||||||||||||||
PyObject *it; | ||||||||||||||||
PyObject *old; | ||||||||||||||||
PyObject *result; | ||||||||||||||||
} pairwiseobject; | ||||||||||||||||
|
||||||||||||||||
/*[clinic input] | ||||||||||||||||
|
@@ -301,6 +302,11 @@ pairwise_new_impl(PyTypeObject *type, PyObject *iterable) | |||||||||||||||
} | ||||||||||||||||
po->it = it; | ||||||||||||||||
po->old = NULL; | ||||||||||||||||
po->result = PyTuple_Pack(2, Py_None, Py_None); | ||||||||||||||||
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.
An alternative that might be faster:
Suggested change
(note: untested!) 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. I tested this locally. Since this line is once called once for each call to |
||||||||||||||||
if (po->result == NULL) { | ||||||||||||||||
Py_DECREF(po); | ||||||||||||||||
return NULL; | ||||||||||||||||
} | ||||||||||||||||
return (PyObject *)po; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
|
@@ -311,6 +317,7 @@ pairwise_dealloc(pairwiseobject *po) | |||||||||||||||
PyObject_GC_UnTrack(po); | ||||||||||||||||
Py_XDECREF(po->it); | ||||||||||||||||
Py_XDECREF(po->old); | ||||||||||||||||
Py_XDECREF(po->result); | ||||||||||||||||
tp->tp_free(po); | ||||||||||||||||
Py_DECREF(tp); | ||||||||||||||||
} | ||||||||||||||||
|
@@ -321,6 +328,7 @@ pairwise_traverse(pairwiseobject *po, visitproc visit, void *arg) | |||||||||||||||
Py_VISIT(Py_TYPE(po)); | ||||||||||||||||
Py_VISIT(po->it); | ||||||||||||||||
Py_VISIT(po->old); | ||||||||||||||||
Py_VISIT(po->result); | ||||||||||||||||
return 0; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
|
@@ -355,8 +363,30 @@ pairwise_next(pairwiseobject *po) | |||||||||||||||
Py_DECREF(old); | ||||||||||||||||
return NULL; | ||||||||||||||||
} | ||||||||||||||||
/* Future optimization: Reuse the result tuple as we do in enumerate() */ | ||||||||||||||||
result = PyTuple_Pack(2, old, new); | ||||||||||||||||
|
||||||||||||||||
result = po->result; | ||||||||||||||||
if (Py_REFCNT(result) == 1) { | ||||||||||||||||
Py_INCREF(result); | ||||||||||||||||
PyObject *last_old = PyTuple_GET_ITEM(result, 0); | ||||||||||||||||
PyObject *last_new = PyTuple_GET_ITEM(result, 1); | ||||||||||||||||
PyTuple_SET_ITEM(result, 0, Py_NewRef(old)); | ||||||||||||||||
PyTuple_SET_ITEM(result, 1, Py_NewRef(new)); | ||||||||||||||||
Py_DECREF(last_old); | ||||||||||||||||
Py_DECREF(last_new); | ||||||||||||||||
Comment on lines
+372
to
+375
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. Since
Suggested change
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. It's not necessarily true that |
||||||||||||||||
// bpo-42536: The GC may have untracked this result tuple. Since we're | ||||||||||||||||
// recycling it, make sure it's tracked again: | ||||||||||||||||
if (!_PyObject_GC_IS_TRACKED(result)) { | ||||||||||||||||
_PyObject_GC_TRACK(result); | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
else { | ||||||||||||||||
result = PyTuple_New(2); | ||||||||||||||||
serhiy-storchaka marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
if (result != NULL) { | ||||||||||||||||
PyTuple_SET_ITEM(result, 0, Py_NewRef(old)); | ||||||||||||||||
PyTuple_SET_ITEM(result, 1, Py_NewRef(new)); | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
Py_XSETREF(po->old, new); | ||||||||||||||||
Py_DECREF(old); | ||||||||||||||||
return result; | ||||||||||||||||
|
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.
In all tests above except for
zip_longest
there is anext(it)
beforegc.collect()
. All these tests were added in 226a012 (bpo-42536).@brandtbucher, why is such difference? Is there a bug in
test_zip_longest_result_gc
? Do we neednext(it)
there and here?