Skip to content

http_loader: Improve preallocation heuristic - #47455

Merged
Narfinger merged 3 commits into
servo:mainfrom
jschwe:net_allocations
Sep 7, 2026
Merged

Narfinger merged 3 commits into
servo:mainfrom
jschwe:net_allocations

Conversation

@jschwe

@jschwe jschwe commented Aug 23, 2026

Copy link
Copy Markdown
Member

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 #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

@jschwe
jschwe requested a review from gterzian as a code owner August 23, 2026 12:19
@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label Aug 23, 2026

@Narfinger Narfinger 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.

Just small nits.

Comment thread components/script/fetch/fetch.rs
Comment thread components/net/http_loader.rs Outdated
@servo-highfive servo-highfive added S-needs-code-changes Changes have not yet been made that were requested by a reviewer. and removed S-awaiting-review There is new code that needs to be reviewed. labels Aug 24, 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, we pre-allocate 5x, which should cover
typical compressions ratios.
For the http-cache, the backing allocation will be shrunk once
everything is received.
Also change the calculation in fetch.rs to prevent underflow, if we
do call the trait wrongly.

Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
Use saturating_mul to prevent potential overflows.

Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
@jschwe
jschwe requested a review from nicoburns as a code owner September 7, 2026 03:31
@servo-highfive servo-highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-needs-code-changes Changes have not yet been made that were requested by a reviewer. labels Sep 7, 2026
@jschwe
jschwe requested a review from Narfinger September 7, 2026 03:32
@servo-highfive servo-highfive removed the S-awaiting-review There is new code that needs to be reviewed. label Sep 7, 2026
@Narfinger
Narfinger added this pull request to the merge queue Sep 7, 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 Sep 7, 2026
Merged via the queue into servo:main with commit bbf8694 Sep 7, 2026
36 checks passed
@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 Sep 7, 2026
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.

3 participants