FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-157840: Special-case member descriptors when using `hasattr` by ZeroIntensity · Pull Request #157866 · python/cpython · GitHub

Repository navigation

gh-157840: Special-case member descriptors when using hasattr - #157866

Merged
ZeroIntensity merged 5 commits into
python:mainfrom
ZeroIntensity:performance/hasattr-unassigned-slots
Sep 20, 2026
Merged

ZeroIntensity merged 5 commits into
python:mainfrom
ZeroIntensity:performance/hasattr-unassigned-slots

Conversation

ZeroIntensity commented Sep 20, 2026 •
edited
Loading

Copy link
Copy Markdown
Member

Before this PR:

$ ./python -m timeit -s 'class Test: __slots__ = ("x",)' -s 't = Test()' 'hasattr(t,"x")' --number 100000000
100000000 loops, best of 5: 192 nsec per loop

After:

$ ./python -m timeit -s 'class Test: __slots__ = ("x",)' -s 't = Test()' 'hasattr(t,"x")' --number 100000000
100000000 loops, best of 5: 30.8 nsec per loop

serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Does it affect three-argument getattr()?

Comment thread Objects/object.c Outdated
Comment on lines +1927 to +1931
// gh-157840: When using hasattr(), slot members without assigned values
// need to go through __get__(), which involves creating an AttributeError
// object (which is really slow). There's no way to supply "suppress"
// to the tp_descr_get() function, so we just special-case member
// descriptors here to improve performance.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Could you please simplify the comment, leaving the substantial part?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Done

Copy link
Copy Markdown
Member Author

Does it affect three-argument getattr()?

Yeah -- performance there is also improved by this PR.

serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM. 👍

Two nits.

Comment thread Objects/object.c Outdated
if (suppress && Py_IS_TYPE(descr, &PyMemberDescr_Type)) {
PyMemberDef *member = ((PyMemberDescrObject *)descr)->d_member;
if (member->type == Py_T_OBJECT_EX
&& !(member->flags & Py_AUDIT_READ) &&

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Inconsistent formatting. && after and before line split.

@@ -0,0 +1,2 @@
Speed up calls to :func:`hasattr` for unassigned attributes in an object's

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Mention also getattr.

ZeroIntensity enabled auto-merge (squash) September 20, 2026 21:36
ZeroIntensity merged commit 41d0922 into python:main Sep 20, 2026
55 checks passed
ZeroIntensity deleted the performance/hasattr-unassigned-slots branch September 20, 2026 22:08

Copy link
Copy Markdown
Member Author

Thanks for the review!

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Performance or resource usage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL