-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
bpo-28685: Optimize sorted() list.sort() with type-specialized comparisons #582
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 1 commit
89c278f
2ce5e5e
7d2f44a
d752fc7
e19728e
7e74c27
9c566b1
8876e26
8accd71
1567801
3820cdb
201a468
c2a9df2
37b15b8
e402948
ed9b21f
acf4c9d
395bc7d
e677586
6070c72
294aa1c
ba05b2a
40ba266
a175939
f0dc847
15f87a2
15f2f01
6afa847
af7c027
20716cb
5960fbe
804807b
5db7158
934d83f
c536ed3
0b85ac5
a12d784
a54a4e4
20172fb
862c761
dd302b5
ab3d520
dba3f27
c796422
fa19903
014fd8f
e4679e2
3b3ce52
afed812
ebb4c1f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1947,15 +1947,16 @@ unsafe_object_compare(PyObject* v, PyObject* w, MergeState* ms) | |
| assert(v->ob_type == w->ob_type && | ||
| v->ob_type->tp_richcompare != NULL); | ||
|
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. Is it possible that |
||
| #endif | ||
| if (v == w) return 0; | ||
| int ok; | ||
|
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. In a debug build, the declaration of
Contributor
Author
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. Right, sorry! Fixed (won't show up until I file a new PR, though)
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. Current style guide allows declarations not at the start of the block. |
||
|
|
||
| if (v == w) return 0; | ||
|
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. Split the line before
Contributor
Author
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. This was actually removed -- it turns out you only need to look at addresses when you're doing equality testing. |
||
| if (v->ob_type->tp_richcompare != ms->key_richcompare) | ||
| return PyObject_RichCompareBool(v, w, Py_LT); | ||
|
|
||
| PyObject* res = (*(ms->key_richcompare))(v, w, Py_LT); | ||
| if (res == NULL) | ||
| return -1; | ||
| int ok; | ||
|
|
||
| if (PyBool_Check(res)){ | ||
|
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. Right before this line, should add That should repair the latest example on the bug tracker.
Contributor
Author
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. thanks, fixed
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. Add a space before |
||
| ok = (res == Py_True); | ||
| } | ||
|
|
@@ -2040,14 +2041,17 @@ unsafe_tuple_compare(PyObject* v, PyObject* w, MergeState* ms) | |
| Py_SIZE(v) > 0 && | ||
| Py_SIZE(w) > 0); | ||
| #endif | ||
| if (v == w) return 0; | ||
|
|
||
| PyTupleObject *vt, *wt; | ||
| Py_ssize_t i; | ||
| Py_ssize_t vlen, wlen; | ||
|
|
||
| vt = (PyTupleObject *)v; | ||
| wt = (PyTupleObject *)w; | ||
|
|
||
| int ok; | ||
|
|
||
| if (v == w) return 0; | ||
|
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. Split the line before |
||
|
|
||
| /* Is v[0] < w[0]? */ | ||
| int k = (*(ms->tuple_elem_compare))(vt->ob_item[0], wt->ob_item[0], ms); | ||
|
|
||
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.
It would be more helpful to use two separate asserts. In case of the failure we could know what condition is false.
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.
Does
list.sort()sorts in-place? Can the content of the list be modified during sorting? If this is true, then the conditionv->ob_type == w->ob_typecan be false.Uh oh!
There was an error while loading. Please reload this page.
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.
Yes, you are correct.
ppperrygave an explicit counterexample to this. It's OK, though, because I added the following:However, I should get rid of the
ob_typeassertion: it could legitimately fail.