Skip to content

gh-159580: Improve performance of isinstance() for misses and improve documentation - #159581

Open
eendebakpt wants to merge 5 commits into
python:mainfrom
eendebakpt:isinstance-class-fastpath
Open

eendebakpt wants to merge 5 commits into
python:mainfrom
eendebakpt:isinstance-class-fastpath

Conversation

@eendebakpt

@eendebakpt eendebakpt commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Improve performance of instance in the case of misses by avoiding attribute lookup on __class__. Documentation of isinstance is updated to match the actual behavior.

Details and benchmarks in the corresponding issue.

@read-the-docs-community

read-the-docs-community Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #35348044 | 📁 Comparing 25aa834 against main (75ce53d)

  🔍 Preview build  

73 files changed · + 1 added · ± 72 modified

+ Added

± Modified

Comment thread Objects/abstract.c
if (PyStackRef_IsNull(cref)) {
return 0;
}
PyObject *descr = PyStackRef_AsPyObjectBorrow(cref);

@kumaraditya303 kumaraditya303 Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

you could cache the object.__class__ descriptor in interp state then this would be simple pointer equality check and even faster.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That would require a _PyInterpreterState_GET() call. I tried this locally, but I see no performance improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants