[librsvg: 1/5] gradient, pattern: use OnceCell to store the resolved field




commit cb09341ee092ee7c828bf94023814bbd9c2e8913
Author: Paolo Borelli <pborelli gnome org>
Date:   Tue Dec 29 19:22:12 2020 +0000

    gradient,pattern: use OnceCell to store the resolved field

 src/gradient.rs | 26 ++++++++++++++------------
 src/pattern.rs  | 25 +++++++++++++------------
 2 files changed, 27 insertions(+), 24 deletions(-)
---
diff --git a/src/gradient.rs b/src/gradient.rs
index b005cf30..68bf8388 100644
--- a/src/gradient.rs
+++ b/src/gradient.rs
@@ -4,7 +4,7 @@ use cssparser::Parser;
 use markup5ever::{
     expanded_name, local_name, namespace_url, ns, ExpandedName, LocalName, Namespace,
 };
-use std::cell::RefCell;
+use once_cell::sync::OnceCell;
 
 use crate::bbox::BoundingBox;
 use crate::coord_units::CoordUnits;
@@ -312,7 +312,7 @@ struct Common {
 
     fallback: Option<NodeId>,
 
-    resolved: RefCell<Option<ResolvedGradient>>,
+    resolved: OnceCell<ResolvedGradient>,
 }
 
 /// Node for the <linearGradient> element
@@ -604,16 +604,11 @@ macro_rules! impl_gradient {
                 }
             }
 
-            pub fn resolve(
+            fn init_resolved(
                 &self,
                 node: &Node,
                 acquired_nodes: &mut AcquiredNodes<'_>,
             ) -> Result<ResolvedGradient, AcquireError> {
-                let mut resolved = self.common.resolved.borrow_mut();
-                if let Some(ref gradient) = *resolved {
-                    return Ok(gradient.clone());
-                }
-
                 let Unresolved {
                     mut gradient,
                     mut fallback,
@@ -646,11 +641,18 @@ macro_rules! impl_gradient {
                     }
                 }
 
-                let gradient = gradient.into_resolved();
-
-                *resolved = Some(gradient.clone());
+                Ok(gradient.into_resolved())
+            }
 
-                Ok(gradient)
+            pub fn resolve(
+                &self,
+                node: &Node,
+                acquired_nodes: &mut AcquiredNodes<'_>,
+            ) -> Result<ResolvedGradient, AcquireError> {
+                self.common
+                    .resolved
+                    .get_or_try_init(|| self.init_resolved(node, acquired_nodes))
+                    .map(|r| r.clone())
             }
         }
     };
diff --git a/src/pattern.rs b/src/pattern.rs
index 83a0836e..6bcc7a9e 100644
--- a/src/pattern.rs
+++ b/src/pattern.rs
@@ -1,7 +1,7 @@
 //! The `pattern` element.
 
 use markup5ever::{expanded_name, local_name, namespace_url, ns};
-use std::cell::RefCell;
+use once_cell::sync::OnceCell;
 
 use crate::aspect_ratio::*;
 use crate::bbox::BoundingBox;
@@ -117,7 +117,7 @@ pub struct UserSpacePattern {
 pub struct Pattern {
     common: Common,
     fallback: Option<NodeId>,
-    resolved: RefCell<Option<ResolvedPattern>>,
+    resolved: OnceCell<ResolvedPattern>,
 }
 
 impl SetAttributes for Pattern {
@@ -406,16 +406,11 @@ impl Pattern {
         }
     }
 
-    pub fn resolve(
+    fn init_resolved(
         &self,
         node: &Node,
         acquired_nodes: &mut AcquiredNodes<'_>,
     ) -> Result<ResolvedPattern, AcquireError> {
-        let mut resolved = self.resolved.borrow_mut();
-        if let Some(ref pattern) = *resolved {
-            return Ok(pattern.clone());
-        }
-
         let Unresolved {
             mut pattern,
             mut fallback,
@@ -461,11 +456,17 @@ impl Pattern {
             }
         }
 
-        let pattern = pattern.into_resolved();
-
-        *resolved = Some(pattern.clone());
+        Ok(pattern.into_resolved())
+    }
 
-        Ok(pattern)
+    pub fn resolve(
+        &self,
+        node: &Node,
+        acquired_nodes: &mut AcquiredNodes<'_>,
+    ) -> Result<ResolvedPattern, AcquireError> {
+        self.resolved
+            .get_or_try_init(|| self.init_resolved(node, acquired_nodes))
+            .map(|r| r.clone())
     }
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]