[librsvg: 3/6] PaintServer: Box the NodeId in the Iri variant




commit aab8f4e15b7464aa2c09b4ed9a1c22787cc984a7
Author: Federico Mena Quintero <federico gnome org>
Date:   Mon Mar 1 18:11:31 2021 -0600

    PaintServer: Box the NodeId in the Iri variant
    
    Although ComputedValues just has two fields (stroke, fill) of type
    PaintServer, they are quite big.  Moving most of the Iri to the heap
    reduces the size of this not-so-common case.
    
    cargo +nightly rustc -- -Z print-type-sizes
    
    Before:
    print-type-size type: `librsvg::paint_server::PaintServer`: 64 bytes, alignment: 8 bytes
    print-type-size type: `librsvg::properties::ComputedValues`: 528 bytes, alignment: 8 bytes
    
    After:
    print-type-size type: `librsvg::paint_server::PaintServer`: 16 bytes, alignment: 8 bytes
    print-type-size type: `librsvg::properties::ComputedValues`: 432 bytes, alignment: 8 bytes

 src/paint_server.rs | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
---
diff --git a/src/paint_server.rs b/src/paint_server.rs
index 83c17364..5639ccc1 100644
--- a/src/paint_server.rs
+++ b/src/paint_server.rs
@@ -19,7 +19,7 @@ use crate::properties::ComputedValues;
 pub enum PaintServer {
     None,
     Iri {
-        iri: NodeId,
+        iri: Box<NodeId>,
         alternate: Option<cssparser::Color>,
     },
     SolidColor(cssparser::Color),
@@ -63,9 +63,11 @@ impl Parse for PaintServer {
             };
 
             Ok(PaintServer::Iri {
-                iri: NodeId::parse(&url)
-                    .map_err(|e: NodeIdError| -> ValueErrorKind { e.into() })
-                    .map_err(|e| loc.new_custom_error(e))?,
+                iri: Box::new(
+                    NodeId::parse(&url)
+                        .map_err(|e: NodeIdError| -> ValueErrorKind { e.into() })
+                        .map_err(|e| loc.new_custom_error(e))?,
+                ),
                 alternate,
             })
         } else {
@@ -99,7 +101,7 @@ impl PaintServer {
                         Element::RadialGradient(ref g) => g
                             .resolve(&node, acquired_nodes)
                             .map(|g| PaintSource::Gradient(g, *alternate)),
-                        _ => Err(AcquireError::InvalidLinkType(iri.clone())),
+                        _ => Err(AcquireError::InvalidLinkType(iri.as_ref().clone())),
                     }
                 })
                 .or_else(|err| match (err, alternate) {
@@ -203,7 +205,7 @@ mod tests {
         assert_eq!(
             PaintServer::parse_str("url(#link)").unwrap(),
             PaintServer::Iri {
-                iri: NodeId::Internal("link".to_string()),
+                iri: Box::new(NodeId::Internal("link".to_string())),
                 alternate: None,
             }
         );
@@ -211,7 +213,7 @@ mod tests {
         assert_eq!(
             PaintServer::parse_str("url(foo#link) none").unwrap(),
             PaintServer::Iri {
-                iri: NodeId::External("foo".to_string(), "link".to_string()),
+                iri: Box::new(NodeId::External("foo".to_string(), "link".to_string())),
                 alternate: None,
             }
         );
@@ -219,7 +221,7 @@ mod tests {
         assert_eq!(
             PaintServer::parse_str("url(#link) #ff8040").unwrap(),
             PaintServer::Iri {
-                iri: NodeId::Internal("link".to_string()),
+                iri: Box::new(NodeId::Internal("link".to_string())),
                 alternate: Some(cssparser::Color::RGBA(cssparser::RGBA::new(
                     255, 128, 64, 255
                 ))),
@@ -229,7 +231,7 @@ mod tests {
         assert_eq!(
             PaintServer::parse_str("url(#link) rgb(255, 128, 64, 0.5)").unwrap(),
             PaintServer::Iri {
-                iri: NodeId::Internal("link".to_string()),
+                iri: Box::new(NodeId::Internal("link".to_string())),
                 alternate: Some(cssparser::Color::RGBA(cssparser::RGBA::new(
                     255, 128, 64, 128
                 ))),
@@ -239,7 +241,7 @@ mod tests {
         assert_eq!(
             PaintServer::parse_str("url(#link) currentColor").unwrap(),
             PaintServer::Iri {
-                iri: NodeId::Internal("link".to_string()),
+                iri: Box::new(NodeId::Internal("link".to_string())),
                 alternate: Some(cssparser::Color::CurrentColor),
             }
         );


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