Skip to content

Reuse the buffer key of the parent header in OutStream.write - #1599

Open
g4lb wants to merge 1 commit into
ipython:mainfrom
g4lb:perf-outstream-buffer-key
Open

g4lb wants to merge 1 commit into
ipython:mainfrom
g4lb:perf-outstream-buffer-key

Conversation

@g4lb

@g4lb g4lb commented Nov 19, 2026

Copy link
Copy Markdown

OutStream.write makes frozenset(parent.items()) for each call. This change keeps the key for the last few parent headers and uses it again when the parent header is the same object. The output does not change.

A real kernel (ipc transport) runs these cells. The time is from execute to the idle status:

Cell Before After Change
for i in range(20000): print(i) 36.8 ms 29.9 ms -19%
for i in range(5000): print('x' * 80) 11.2 ms 9.5 ms -15%
a = 1 1.71 ms 1.70 ms -1%

Direct calls to OutStream.write:

Benchmark Before After Change
100 short write() calls 48.6 µs 32.3 µs -34%
100 print(i) calls 112.4 µs 79.0 µs -30%
50 writes, 2 parents in turn 38.6 µs 31.0 µs -20%
50 writes, a new parent for each write 41.5 µs 47.7 µs +15%

The last row is the slow case: each new parent header adds one dict entry. The kernel sets one parent header for each request, so this cost occurs one time for each request.

I used 20 interleaved rounds and a Mann-Whitney test (Apple M5, CPython 3.11). CPython 3.13 and 3.15 gave almost the same numbers. On PyPy 3.11, the write benchmarks are 85-94% faster, and the new-parent case did not change significantly. I did not measure other architectures.

Checks:

  • pytest gives the same result as on main on CPython 3.11, 3.14, 3.15 and PyPy 3.11.
  • Old and new outputs matched on 1,000,000 random OutStream call sequences and 50,000 random cells in the in-process kernel.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant