Skip to content

perf: use memoryview in write() to avoid copies#954

Open
mbeijen wants to merge 2 commits into
pydantic:mainfrom
mbeijen:perf/memoryview-write
Open

perf: use memoryview in write() to avoid copies#954
mbeijen wants to merge 2 commits into
pydantic:mainfrom
mbeijen:perf/memoryview-write

Conversation

@mbeijen
Copy link
Copy Markdown
Contributor

@mbeijen mbeijen commented May 17, 2026

Summary

Use memoryview slicing in TLSinTLSStream.write() and SyncStream.write() so that each iteration of the send loop does a zero-copy view advance instead of copying the remaining buffer bytes. Most visible on large POST uploads where partial sends cause repeated re-copies of the tail.

Ported from https://codeberg.org/httpxyz/httpcorexyz/pulls/4

Closes #912
Closes encode/httpcore#1029

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 17, 2026

Merging this PR will degrade performance by 10.05%

❌ 1 regressed benchmark
✅ 6 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
WallTime test_bench_client_stream_download 28.2 ms 31.3 ms -10.05%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing mbeijen:perf/memoryview-write (519a8d6) with main (8d5f182)

Open in CodSpeed

Use memoryview slicing in TLSinTLSStream.write() and SyncStream.write()
so that each iteration of the send loop does a zero-copy view advance
instead of copying the remaining buffer bytes. Most visible on large
POST uploads where partial sends cause repeated re-copies of the tail.

Ported from https://codeberg.org/httpxyz/httpcorexyz/pulls/4

Closes pydantic#912
Closes encode/httpcore#1029

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: naarob <laforge@forge-sync.local>
@mbeijen mbeijen force-pushed the perf/memoryview-write branch from c0bff4b to f42a743 Compare May 17, 2026 14:12
@Kludex
Copy link
Copy Markdown
Member

Kludex commented May 17, 2026

I'm experimenting some benchmark strategies. Sorry the noise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants