Skip to content

gh-132312: Significantly reduce thread state lookups in the interpreter#151453

Draft
ZeroIntensity wants to merge 13 commits into
python:mainfrom
ZeroIntensity:tstate-bonanza
Draft

gh-132312: Significantly reduce thread state lookups in the interpreter#151453
ZeroIntensity wants to merge 13 commits into
python:mainfrom
ZeroIntensity:tstate-bonanza

Conversation

@ZeroIntensity

@ZeroIntensity ZeroIntensity commented Jun 13, 2026

Copy link
Copy Markdown
Member

This does a lot of refactoring to avoid repeated _PyThreadState_GET calls in hot code paths (primarily the eval loop + common object operations).

Most of the changes here are mechanical, but I did have to make some more convoluted changes to make this robust (such as the addition of _Py_DECREF and such).

@picnixz

picnixz commented Jun 14, 2026

Copy link
Copy Markdown
Member

Do you have some numbers on how much we gain with this dance?

@ZeroIntensity

Copy link
Copy Markdown
Member Author

Nothing conclusive yet, no.

@ZeroIntensity

Copy link
Copy Markdown
Member Author

I'm having a lot of trouble getting consistent benchmarks, unfortunately. My system seems to be too jittery to get a reliable result. I also don't know why the JIT CI is failing on Windows.

@Fidget-Spinner -- do you have advice for either of these?

@Fidget-Spinner

Copy link
Copy Markdown
Member

@ZeroIntensity JIT on Windows requires whatever is used in bytecodes.c to be an exported symbol. In this case it's complaining memcpy is not available. Are you using that? If so, you need to add the relevant header to template.c

@ZeroIntensity

Copy link
Copy Markdown
Member Author

I didn't add anything that uses memcpy in this PR, but shouldn't functions from the C stdlib be considered exported anyway?

@Fidget-Spinner

Copy link
Copy Markdown
Member

It could be indirectly used as well through an inlined function. In any case, unless string.h is included by Python.h or the other pycores by default it wont be included https://github.com/python/cpython/blob/main/Tools/jit/template.c

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.

3 participants