Skip to content

gh-143421: Lazily allocate tracer code and opt buffers - #143597

Merged
Fidget-Spinner merged 2 commits into
python:mainfrom
Fidget-Spinner:lazily-allocate
Jan 9, 2026
Merged

Fidget-Spinner merged 2 commits into
python:mainfrom
Fidget-Spinner:lazily-allocate

Conversation

@Fidget-Spinner

@Fidget-Spinner Fidget-Spinner commented Jan 9, 2026 •

Copy link
Copy Markdown
Member

I verified on my system this restores the old performance.

@cocolato

cocolato commented Jan 9, 2026

Copy link
Copy Markdown
Member

Should we remove the newly added pycore_optimizer_types.h?

@Fidget-Spinner

Copy link
Copy Markdown
Member Author

Should we remove the newly added pycore_optimizer_types.h?

Let's keep it. It's a good refactor.

@Fidget-Spinner
Fidget-Spinner merged commit b852236 into python:main Jan 9, 2026
@Fidget-Spinner
Fidget-Spinner deleted the lazily-allocate branch January 9, 2026 16:56
// Holds locals, stack, locals, stack ... co_consts (in that order)
#define MAX_ABSTRACT_INTERP_SIZE 4096
// Holds locals, stack, locals, stack ... (in that order)
#define MAX_ABSTRACT_INTERP_SIZE 512

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.

Why?

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.

This number was picked arbitrarily by me back then when I first wrote this. I now realise it's overkill. We don't need 3 pages of memory just to store locals and stack. It's slow and wastes memory.

@markshannon

Copy link
Copy Markdown
Member

This makes no sense to me. How is allocating the necessary memory piecemeal faster than allocating it in a single chunk?

@Fidget-Spinner

Copy link
Copy Markdown
Member Author

@markshannon no the problem was embedding it as a part of _PyThreadStateImpl, that caused a 100% slowdown of thread spawning time on the benchmark, which reflected in bench_thread_pool.

@Fidget-Spinner

Copy link
Copy Markdown
Member Author

The benchmark doesn't run any work to use the JIT, instead it just measures thread startup overhead https://github.com/python/pyperformance/blob/main/pyperformance/data-files/benchmarks/bm_concurrent_imap/run_benchmark.py#L19

Embedding the two structs together in the _PyThreadStateImpl causes a massive slowdown.

@markshannon

Copy link
Copy Markdown
Member

It shouldn't be embedded, it should be heap allocated as one
#143536 (comment)

@Fidget-Spinner

Copy link
Copy Markdown
Member Author

It shouldn't be embedded, it should be heap allocated as one #143536 (comment)

Ok let me just put a up a PR to do that.

reidenong pushed a commit to reidenong/cpython that referenced this pull request Jan 12, 2026
thunder-coding pushed a commit to thunder-coding/cpython that referenced this pull request Feb 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

4 participants