Skip to content

tcp 2.0.1: 4.5x faster receives, bounded allocations, whole-connect deadline

Choose a tag to compare

@Mosaad-M Mosaad-M released this 08 Oct 11:49
· 9 commits to master since this release
98fc4f9

Performance and security fixes from profiling tcp 2.0.0. No API change.

Performance

Receives now land directly in the returned list, with no intermediate buffer or byte-by-byte copy. Measured on loopback (Apple M1 Pro):

2.0.0 2.0.1
recv_bytes(64 KiB) loop 1.6 GB/s 7.1-7.7 GB/s (Python sockets: 8.6)
recv_all 256 MiB 1.0 GB/s 2.2 GB/s
recv_bytes_exact, 5 + 59 bytes 1.5 µs 1.1 µs

Sending, round-trip latency and connect time are unchanged. pixi run bench reproduces these numbers.

Security

  • Bounded memory: recv_bytes_exact(n) no longer reserves n bytes before any data arrives. A server-supplied length used to reserve up to gigabytes; pg 1.7.1 also caps message lengths at 1 GiB. A single recv() now reads at most 1 MiB; lengths above 2 GiB used to fail with EINVAL.
  • timeout_secs now bounds the whole connect. In 2.0.0 each resolved address got the full timeout, so 8 unreachable addresses took 8 s with timeout_secs=1 (now 1 s), and a DNS name returning many dead addresses could hang a client for minutes. The first address still gets nearly the full timeout: the timeout minus 1 s for each address after it.
  • Huge timeouts: very large timeout_secs values no longer overflow the poll timeout.

Testing

  • 6 new tests, 26 in total, passing in CI on Linux and macOS.
  • requests, websocket and pg pass their suites with this version.
  • The tls live sweep passes (24 sites plus 4 bad endpoints). The 55-site sweep gets 54 of 55, the same as 2.0.0.