-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-116738: Inline append for local PyList #135196
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
Conversation
Can you add a sentence or two about the motivation for this change (performance?) to the PR description? |
Updated the description, thank you! |
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.
LGTM.
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'm a little skeptical that the performance is ever going to matter (getgrall() is unlikely to be very big and if it is, it's always going to be horrendously slow) but the code is clear enough and the assert protects us against unintended sharing of the list, so why not.
@@ -290,18 +292,19 @@ grp_getgrall_impl(PyObject *module) | |||
setgrent(); | |||
|
|||
struct group *p; | |||
// `d` is a local list; append items without a lock using | |||
// _PyList_AppendTakeRef() within the loop. | |||
assert(_PyObject_IsUniquelyReferenced(d)); |
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.
d is not passed anywhere, isn't this obvious? we never added such assertions where it was obvious like this case
I doubt that getgrall would produce so many entries to that performance of this would matter, using private API even in non core extension modules and non performance critical code seems like a bad idea to me so -1 from me. |
@Yhg1s, @kumaraditya303: Thank you for the reviews. I completely agree that performance is very unlikely to matter here for @kumaraditya303 As far as I understand, you prefer to use the private API only when the performance wins can be clearly measured. @Yhg1s @kumaraditya303, I could close the PR and, in the future, make changes only when the performance benefits of using private APIs are clearly measurable. What do you think? |
Yes, that sounds better |
Yes, I think that's the right approach in this. (Your efforts are nonetheless appreciated, Alper :) |
Append items to the local list in a loop without a lock.
Appending to the list without a lock can improve performance, but testing
grp.getgrall()
produced noisy results, making it difficult to determine any improvement. Running a micro benchmark with a function that adds over 100K items to the list using_PyList_AppendTakeRef()
instead ofPyList_Append()
showed about a 35% performance improvement.cc: @mpage @colesbury