diff options
author | Oriol Brufau <obrufau@igalia.com> | 2024-11-26 14:35:41 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-26 13:35:41 +0000 |
commit | d034385f7643433b6bc738d32f84d45b70948534 (patch) | |
tree | b719a206ae2d034f5ecbc42e3d13875a1860e6ae /components/layout_2020/formatting_contexts.rs | |
parent | 63793ccbb7c0768af3f31c274df70625abacb508 (diff) | |
download | servo-d034385f7643433b6bc738d32f84d45b70948534.tar.gz servo-d034385f7643433b6bc738d32f84d45b70948534.zip |
Use an AtomicRefCell instead of a RwLock for caching intrinsic sizes (#34384)
It panics if the value is mutably borrowed concurrently, but this allows
it to perform better.
Signed-off-by: Oriol Brufau <obrufau@igalia.com>
Diffstat (limited to 'components/layout_2020/formatting_contexts.rs')
-rw-r--r-- | components/layout_2020/formatting_contexts.rs | 22 |
1 files changed, 9 insertions, 13 deletions
diff --git a/components/layout_2020/formatting_contexts.rs b/components/layout_2020/formatting_contexts.rs index 4f4eb097d3c..35aa74ac189 100644 --- a/components/layout_2020/formatting_contexts.rs +++ b/components/layout_2020/formatting_contexts.rs @@ -2,9 +2,8 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -use std::sync::RwLock; - use app_units::Au; +use atomic_refcell::AtomicRefCell; use serde::Serialize; use servo_arc::Arc; use style::properties::ComputedValues; @@ -41,7 +40,8 @@ pub(crate) struct NonReplacedFormattingContext { #[serde(skip_serializing)] pub style: Arc<ComputedValues>, /// If it was requested during construction - pub content_sizes_result: RwLock<Option<(SizeConstraint, InlineContentSizesResult)>>, + #[serde(skip_serializing)] + pub content_sizes_result: AtomicRefCell<Option<(SizeConstraint, InlineContentSizesResult)>>, pub contents: NonReplacedFormattingContextContents, } @@ -169,7 +169,7 @@ impl IndependentFormattingContext { Self::NonReplaced(NonReplacedFormattingContext { style: Arc::clone(&node_and_style_info.style), base_fragment_info, - content_sizes_result: RwLock::default(), + content_sizes_result: AtomicRefCell::default(), contents, }) }, @@ -298,24 +298,20 @@ impl NonReplacedFormattingContext { layout_context: &LayoutContext, constraint_space: &ConstraintSpace, ) -> InlineContentSizesResult { - if let Ok(Some((previous_cb_block_size, result))) = - self.content_sizes_result.read().as_deref() - { + let mut cache = self.content_sizes_result.borrow_mut(); + if let Some((previous_cb_block_size, result)) = *cache { if !result.depends_on_block_constraints || - *previous_cb_block_size == constraint_space.block_size + previous_cb_block_size == constraint_space.block_size { - return *result; + return result; } // TODO: Should we keep multiple caches for various block sizes? } - let writer = self.content_sizes_result.write(); let result = self .contents .inline_content_sizes(layout_context, constraint_space); - if let Ok(mut cache) = writer { - *cache = Some((constraint_space.block_size, result)); - } + *cache = Some((constraint_space.block_size, result)); result } |