Skip to content

net: Shrink completed_body allocation when finishing HTTP load - #47454

Merged
jschwe merged 1 commit into
servo:mainfrom
jschwe:shrink_cache
Aug 23, 2026
Merged

jschwe merged 1 commit into
servo:mainfrom
jschwe:shrink_cache

Conversation

@jschwe

@jschwe jschwe commented Aug 23, 2026 •

Copy link
Copy Markdown
Member

The responses are cached by our HTTP cache, so shrinking the allocation can save memory by discarding excess capacity. Not all responses are cached, but currently we don't have a way to determine if it is retained by the HTTP cache or not, and it's unclear if adding such a mechanic would save work compared to just always shrinking. The network loading process already contains a series of potential re-allocations (by growing the Vec), so adding another potential reallocation to shrink should be fine.

Testing: No behavior change.

The responses are cached by our http cache, so shrinking the
allocation can save memory by discarding excess capacity.

Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label Aug 23, 2026
@mrobinson mrobinson changed the title http_loader: Shrink allocation when done net: Shrink completed_body allocation when finishing HTTP load Aug 23, 2026
@servo-highfive servo-highfive removed the S-awaiting-review There is new code that needs to be reviewed. label Aug 23, 2026
@jschwe
jschwe added this pull request to the merge queue Aug 23, 2026
@servo-highfive servo-highfive added the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Aug 23, 2026
Merged via the queue into servo:main with commit 5b14258 Aug 23, 2026
38 checks passed
@jschwe
jschwe deleted the shrink_cache branch August 23, 2026 11:14
@servo-highfive servo-highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Aug 23, 2026
pull Bot pushed a commit to AmirulAndalib/servo that referenced this pull request Sep 7, 2026
Reduce the amount of intermediate reallocations by preallocating more
generously.
if we don't have any information, we at least preallocate something. If
the content is compressed and we know the compressed content-length, we
pre-allocate 5x, which should cover typical compressions ratios.
For the http-cache, the backing allocation will be shrunk once
everything is received (see servo#47454).
Also change the calculation in fetch.rs to prevent underflow, if we do
call the trait wrongly.
On typical pages this seems to avoid ~90% of the reallocs during
fetching and does not seem to negatively affect memory usage.


Testing: No observable change, covered by existing tests

---------

Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
RichardTjokroutomo pushed a commit to RichardTjokroutomo/servo that referenced this pull request Oct 6, 2026
…vo#47454)

The responses are cached by our HTTP cache, so shrinking the allocation
can save memory by discarding excess capacity. Not all responses are
cached, but currently we don't have a way to determine if it is retained
by the HTTP cache or not, and it's unclear if adding such a mechanic
would save work compared to just always shrinking. The network loading
process already contains a series of potential re-allocations (by
growing the `Vec`), so adding another potential reallocation to shrink
should be fine.

Testing: No behavior change.

Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
RichardTjokroutomo pushed a commit to RichardTjokroutomo/servo that referenced this pull request Oct 6, 2026
Reduce the amount of intermediate reallocations by preallocating more
generously.
if we don't have any information, we at least preallocate something. If
the content is compressed and we know the compressed content-length, we
pre-allocate 5x, which should cover typical compressions ratios.
For the http-cache, the backing allocation will be shrunk once
everything is received (see servo#47454).
Also change the calculation in fetch.rs to prevent underflow, if we do
call the trait wrongly.
On typical pages this seems to avoid ~90% of the reallocs during
fetching and does not seem to negatively affect memory usage.


Testing: No observable change, covered by existing tests

---------

Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
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.

4 participants