Skip to content

gh-141968: Use take_bytes to simplify and remove copy from pyrepl BaseEventQueue#149852

Open
lgeiger wants to merge 4 commits into
python:mainfrom
lgeiger:pyrepl-eventq-take-bytes
Open

gh-141968: Use take_bytes to simplify and remove copy from pyrepl BaseEventQueue#149852
lgeiger wants to merge 4 commits into
python:mainfrom
lgeiger:pyrepl-eventq-take-bytes

Conversation

@lgeiger
Copy link
Copy Markdown
Contributor

@lgeiger lgeiger commented May 14, 2026

This PR uses bytearray.take_bytes to simplify pyrepl's BaseEventQueue.flush_buf and remove copies from BaseEventQueue.push

import timeit
from statistics import mean, stdev

times = timeit.repeat(
    "[q.push(b) for b in data]",
    setup=(
        "from _pyrepl.base_eventqueue import BaseEventQueue\n"
        "q = BaseEventQueue('utf-8', {})\n"
        "data = b'hello world' * 8\n"
    ),
    number=50_000,
    repeat=5,
)
print(f"{mean(times) * 1e3:.2f} ± {stdev(times) * 1e3:.2f} ms")
main:    1845.39 ± 10.02 ms
this PR: 1371.56 ± 3.85 ms

@maurycy
Copy link
Copy Markdown
Contributor

maurycy commented May 15, 2026

cc @cmaloney

return not self.events

def flush_buf(self) -> bytearray:
def flush_buf(self) -> bytes:
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.

Maybe this method is redundant now?

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.

I'm happy to remove the function. For now I kept it to keep the diff small. But happy to remove it and just use self.buf.take_bytes() instead. Let me know what you prefer.

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.

2 participants