Skip to content

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

Closed
wants to merge 4 commits into from
Closed

Conversation

yoney
Copy link
Contributor

@yoney yoney commented Jun 6, 2025

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 of PyList_Append() showed about a 35% performance improvement.

cc: @mpage @colesbury

@yoney yoney marked this pull request as ready for review June 6, 2025 00:22
@mpage
Copy link
Contributor

mpage commented Jun 6, 2025

Can you add a sentence or two about the motivation for this change (performance?) to the PR description?

@yoney
Copy link
Contributor Author

yoney commented Jun 10, 2025

Can you add a sentence or two about the motivation for this change (performance?) to the PR description?

Updated the description, thank you!

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Copy link
Member

@Yhg1s Yhg1s left a 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));
Copy link
Contributor

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

@kumaraditya303
Copy link
Contributor

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 of PyList_Append() showed about a 35% performance improvement.

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.

@yoney
Copy link
Contributor Author

yoney commented Jun 11, 2025

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)

I doubt that getgrall would produce so many entries to that performance of this would matter

@Yhg1s, @kumaraditya303: Thank you for the reviews. I completely agree that performance is very unlikely to matter here for getgrall(). I created this PR while working on grp module FT changes, and I want to gather feedback to determine exactly when we should use the private API.

@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?

@kumaraditya303
Copy link
Contributor

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

@Yhg1s
Copy link
Member

Yhg1s commented Jun 12, 2025

Yes, I think that's the right approach in this. (Your efforts are nonetheless appreciated, Alper :)

@Yhg1s Yhg1s closed this Jun 12, 2025
@yoney yoney deleted the ft_grpmodule branch June 12, 2025 12:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants