diff options
author | Jonathan Schwender <55576758+jschwe@users.noreply.github.com> | 2025-03-04 08:48:12 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-04 07:48:12 +0000 |
commit | 5533092ab325626837d895e63380dad8519daef4 (patch) | |
tree | c75060f0f527b998a27d1c497c2881946ab85f88 | |
parent | f594691af99175571db61c4a41203f7fd6723985 (diff) | |
download | servo-5533092ab325626837d895e63380dad8519daef4.tar.gz servo-5533092ab325626837d895e63380dad8519daef4.zip |
Reduce allocations in layout_block_level_children_in_parallel (#35781)
This function showed up as a top producer of allocations
(around 10% of all allocations).
Allocating the vector once upfront and using
`collect_into_vec` removes any intermediate allocations.
This approach is also recommended by the rayon documentation:
https://docs.rs/rayon/1.10.0/rayon/iter/trait.ParallelIterator.html#method.collect
Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
-rw-r--r-- | components/layout_2020/flow/mod.rs | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/components/layout_2020/flow/mod.rs b/components/layout_2020/flow/mod.rs index d57d60da8d1..cccce1ee3aa 100644 --- a/components/layout_2020/flow/mod.rs +++ b/components/layout_2020/flow/mod.rs @@ -7,7 +7,7 @@ use app_units::{Au, MAX_AU}; use inline::InlineFormattingContext; -use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; +use rayon::iter::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator}; use servo_arc::Arc; use style::Zero; use style::computed_values::clear::T as StyleClear; @@ -662,7 +662,10 @@ fn layout_block_level_children_in_parallel( ) -> Vec<Fragment> { let collects_for_nearest_positioned_ancestor = positioning_context.collects_for_nearest_positioned_ancestor(); - let layout_results: Vec<(Fragment, PositioningContext)> = child_boxes + let mut layout_results: Vec<(Fragment, PositioningContext)> = + Vec::with_capacity(child_boxes.len()); + + child_boxes .par_iter() .map(|child_box| { let mut child_positioning_context = @@ -676,7 +679,7 @@ fn layout_block_level_children_in_parallel( ); (fragment, child_positioning_context) }) - .collect(); + .collect_into_vec(&mut layout_results); layout_results .into_iter() |