8000 Avoid quadratic buffer concatenation when receiving large replies by Avasam · Pull Request #298 · python-xlib/python-xlib · GitHub
[go: up one dir, main page]

Skip to content

Avoid quadratic buffer concatenation when receiving large replies - #298

Open
Avasam wants to merge 1 commit into
python-xlib:masterfrom
Avasam:fix-quadratic-recv
Open

Avasam wants to merge 1 commit into
python-xlib:masterfrom
Avasam:fix-quadratic-recv

Conversation

@Avasam
@Avasam Avasam commented Oct 23, 2026
Copy link
Copy Markdown
Contributor

send_and_recv rebuilt data_recv as a new bytes object on every socket read, copying everything received so far. A reply spanning N reads cost O(N^2) copying, which dominates large GetImage requests.

Once a packet's length is known, hold incoming chunks in a list and join them only when the packet is complete. Connection setup and packets whose header hasn't arrived yet are handled as before.

GetImage of a full 3840x2160 ZPixmap on Xvfb: ~245 ms -> ~70 ms.
1920x1080: ~13.5 ms -> ~11.5 ms. Small replies are unchanged.

AI Disclaimer: Claude Opus 5.5 found this whilst testing performance improvements for https://github.com/Toufool/AutoSplit . I don't know what's these maintainers view on LLM-generated code. And the project doesn't seem actively maintained either. But I figured I may as well share the fix.

`send_and_recv` rebuilt `data_recv` as a new `bytes` object on every
socket read, copying everything received so far. A reply spanning N
reads cost O(N^2) copying, which dominates large `GetImage` requests.

Once a packet's length is known, hold incoming chunks in a list and
join them only when the packet is complete. Connection setup and
packets whose header hasn't arrived yet are handled as before.

`GetImage` of a full 3840x2160 `ZPixmap` on Xvfb: ~245 ms -> ~70 ms.
1920x1080: ~13.5 ms -> ~11.5 ms. Small replies are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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

0