Skip to content

Reduce per-row overhead in session recv and putValue dispatch#195

Open
TheDistributor wants to merge 1 commit into
masterfrom
mgallwey/accelerate-network-io
Open

Reduce per-row overhead in session recv and putValue dispatch#195
TheDistributor wants to merge 1 commit into
masterfrom
mgallwey/accelerate-network-io

Conversation

@TheDistributor

@TheDistributor TheDistributor commented Jun 29, 2026

Copy link
Copy Markdown
  • session.recv/__readFully: pre-allocate the receive buffer and fill it with
    recv_into, and drop the redundant bytes->bytearray copy in _exchangeMessages
  • raise the socket receive buffer to 1 MiB so large result batches arrive in
    fewer reads
  • putValue: check the common parameter types (int, str, float, bool) with a
    direct type check before falling through to the slower isinstance chain
  • executebatch: read parameter_count once instead of on every row

Nominal performance change on its own, but lays the groundwork for future changes.

Streamline the data-ingestion pipeline and outbound parameter encoding loop by
eliminating high-overhead memory copies, optimizing primitive type comparisons,
and increasing TCP window throughput.

Detailed changes:
- session: Request an opportunistic 1 MiB socket receive buffer (SO_RCVBUF) to
  minimize fragmented kernel reads across heavy wire payload streams.
- session: Rewrite `__readFully` to pre-allocate a single message-sized
  bytearray. Populate it directly using zero-copy `sock.recv_into()` on a
  slicing memoryview, replacing an iterative chunk concatenation loop.
- encodedsession: Update `recv()` and input stream targets to return and use
  raw mutable bytearray buffers directly, cutting down an extra copy.
- encodedsession: Refactor `putValue` to use rapid pointer-level exact type
  comparisons (`type(v) is X`) for common database primitives (int, str, float,
  bool) before falling back to heavier hierarchy-aware `isinstance()` checks
  for the long tail.
@TheDistributor TheDistributor self-assigned this Jun 29, 2026
@TheDistributor TheDistributor changed the title Optimize network I/O buffering and parameter type dispatch Optimize network reads in the session to generate a contiguous bytearray Jun 29, 2026
@TheDistributor TheDistributor changed the title Optimize network reads in the session to generate a contiguous bytearray Reduce per-row overhead in session recv and putValue dispatch Jun 29, 2026

@madscientist madscientist left a comment

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.

This looks good but please remember that, until our test environment is ported to Python3, we must preserve python2 compatibility.

The handling of bytes/bytearray is different between P2 and P3 and this difference is the cause of some of the complexity in this area. The Circle CI/CD tests only test Python 3: to check Python 2 you need to run the tests on our local systems, using nuopython.

If this is passing then these changes look good to me.

Comment thread pynuodb/encodedsession.py
Comment on lines +883 to +885
# Preserve historic wire behaviour: bools encode as integers
# because the original isinstance(value, int) chain matched True
# and False before reaching the (dead) bool branch below.

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.

Ugh. How does this work? I mean I understand that we want to preserve compatibility but doesn't this mean we can't send bool types?

Does the server interpret the integer as a bool based on the type?

Comment thread pynuodb/session.py
Comment on lines +532 to +533
recv_into(), avoiding the repeated bytearray concatenations and the
final bytes() copy that the previous implementation performed.

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.

We shouldn't justify this implementation comparing it to some previous implementaiton, in the docstring of the method. This information is appropriate for the git commit message, but not here.

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.

3 participants