Conversation
Client: py Connection.read() appended each received piece to a bytes object. A bytes object cannot grow, so every append copied everything received so far, and a frame arriving in n pieces cost O(n^2) bytes of copying. A 1 MiB frame in 4 KiB pieces copied about 130 MiB. The buffer is now a bytearray, which grows in place. Each message now gets a copy of exactly its own frame. It used to be handed the whole buffer as it stood, so the transport the processor read from also held whatever had arrived after the frame. What follows a frame stays in the buffer for the next one, as before. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
JIRA: THRIFT-6267
Client: py
Connection.read()inlib/py/src/server/TNonblockingServer.pydidself._rbuf += readon abytesobject. Abytesobject cannot grow, so each append copied everything received so far, and a frame that arrives in n pieces costs O(n²) bytes of copying. Measured with the new test: a 1 MiB frame in 4 KiB pieces copied 135,790,596 bytes.Change
_rbufis abytearray, so appending grows it in place. The consumed frame is removed withdel self._rbuf[:end].bytes(self._rbuf[:end]), a copy of exactly its own frame. The processing thread still buildsTMemoryBuffer(msg.buffer, msg.offset)as before.One difference in behaviour: a message used to receive the whole buffer as it stood. When further bytes arrived in the same read, the transport the processor read from therefore continued past the end of its frame into the next one. Now it ends at the frame. What follows a frame stays in the buffer for the next message, as before.
Tests
New
lib/py/test/test_nonblocking_server_read_buffer.py, registered in both lists inlib/py/Makefile.am. It uses a socket stand-in that hands out one piece perread(), the way oneselect()wakeup would.bytessubclass whose__radd__counts whatbuffer + piececopies. Growing abytearrayis not counted.Against the unmodified server:
\x00\x00\x00\x0ethe second one;Mutations, each on its own:
bytes(...) + read: the counting test fails.delempties it.Also run, with the fix:
test_nonblocking_server_read_buffer.pyandtest_nonblocking_server_frame_size.pypass on Python 3.10, 3.12 and 3.14;thrift_TNonblockingServer.py, a server with a real client, passes on 3.10;🤖 Generated with Claude Code