Skip to content

perf: read a body of unknown length into one buffer of its total - #22

Closed
metalwarrior665 wants to merge 1 commit into
masterfrom
perf/body-buffer
Closed

metalwarrior665 wants to merge 1 commit into
masterfrom
perf/body-buffer

Conversation

@metalwarrior665

Copy link
Copy Markdown
Member

A decompressed body has no length in advance (Content-Length is the compressed size). ResponseBody::bytes used to start with an empty BytesMut, which grew as the chunks came, copying what it held at each doubling. On the Instagram profile scraper that regrowth (jemalloc rallocx plus the copies) was about 6% of the run's CPU, at a few hundred KB per response.

The chunks are now kept until the body ends and copied once into a buffer of their total. A body of known length still goes into its preallocated buffer, and a single-chunk body is still not copied.

A new test reads 200 chunks with and without a known length. The effect will be measured with instagram-rs paired runs.


Generated by Claude Code

A decompressed body has no length in advance, so its chunks went into
a buffer that grew as they came: copied at each doubling, and the
regrowth (reallocation and copying) took about 6% of the CPU of an
Instagram profile run, a few hundred KB per response. The chunks are now
kept and copied once into a buffer of their total. A body of known
length is still read into its preallocated buffer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0148RYR4BTGyCYHENSRZjC3S

Copy link
Copy Markdown
Member Author

Closing: no gain measured. Paired instagram-rs runs (apify-store/instagram-rs#93, 10k profiles at 1 GB, both builds at the same time) took 50.0 and 49.4 CPU-seconds with this change against 46.6 and 48.9 without. Copying the chunks once at the end costs about as much as letting the buffer grow.


Generated by Claude Code


Generated by Claude Code

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.

2 participants