[librsvg: 2/6] IRI: Box the Resource(NodeId) variant to shrink the IRI enum




commit 78419f70cadf6f8d53c2d606b29d1f295cf87c27
Author: Federico Mena Quintero <federico gnome org>
Date:   Mon Mar 1 17:50:04 2021 -0600

    IRI: Box the Resource(NodeId) variant to shrink the IRI enum
    
    `cargo +nightly rustc -- -Z print-type-sizes` gives these:
    
    Before:
    print-type-size type: `librsvg::iri::IRI`: 56 bytes, alignment: 8 bytes
    print-type-size     variant `Resource`: 56 bytes
    print-type-size         field `.0`: 56 bytes
    print-type-size     variant `None`: 0 bytes
    
    After (the NodeId is in the heap:
    print-type-size type: `librsvg::iri::IRI`: 8 bytes, alignment: 8 bytes
    print-type-size     variant `Resource`: 8 bytes
    print-type-size         field `.0`: 8 bytes
    print-type-size     variant `None`: 0 bytes
    
    In particular, since ComputedValues has the following fields which are
    newtypes around IRI:
    
      clip_path
      marker_end
      marker_mid
      marker_start
      mask
    
    and most of them are unset for most elements, this lets us reduce
    CmoputedValues quite a bit:
    
    Before:
    print-type-size type: `librsvg::properties::ComputedValues`: 768 bytes, alignment: 8 bytes
    
    After:
    print-type-size type: `librsvg::properties::ComputedValues`: 528 bytes, alignment: 8 bytes

 src/iri.rs | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
---
diff --git a/src/iri.rs b/src/iri.rs
index e73a3fd5..60ba3362 100644
--- a/src/iri.rs
+++ b/src/iri.rs
@@ -15,7 +15,7 @@ use crate::parsers::Parse;
 #[derive(Debug, Clone, PartialEq)]
 pub enum IRI {
     None,
-    Resource(NodeId),
+    Resource(Box<NodeId>),
 }
 
 impl Default for IRI {
@@ -29,7 +29,7 @@ impl IRI {
     pub fn get(&self) -> Option<&NodeId> {
         match *self {
             IRI::None => None,
-            IRI::Resource(ref f) => Some(f),
+            IRI::Resource(ref f) => Some(&*f),
         }
     }
 }
@@ -47,7 +47,7 @@ impl Parse for IRI {
             let node_id =
                 NodeId::parse(&url).map_err(|e| loc.new_custom_error(ValueErrorKind::from(e)))?;
 
-            Ok(IRI::Resource(node_id))
+            Ok(IRI::Resource(Box::new(node_id)))
         }
     }
 }
@@ -65,22 +65,22 @@ mod tests {
     fn parses_url() {
         assert_eq!(
             IRI::parse_str("url(#bar)").unwrap(),
-            IRI::Resource(NodeId::Internal("bar".to_string()))
+            IRI::Resource(Box::new(NodeId::Internal("bar".to_string())))
         );
 
         assert_eq!(
             IRI::parse_str("url(foo#bar)").unwrap(),
-            IRI::Resource(NodeId::External("foo".to_string(), "bar".to_string()))
+            IRI::Resource(Box::new(NodeId::External("foo".to_string(), "bar".to_string())))
         );
 
         // be permissive if the closing ) is missing
         assert_eq!(
             IRI::parse_str("url(#bar").unwrap(),
-            IRI::Resource(NodeId::Internal("bar".to_string()))
+            IRI::Resource(Box::new(NodeId::Internal("bar".to_string())))
         );
         assert_eq!(
             IRI::parse_str("url(foo#bar").unwrap(),
-            IRI::Resource(NodeId::External("foo".to_string(), "bar".to_string()))
+            IRI::Resource(Box::new(NodeId::External("foo".to_string(), "bar".to_string())))
         );
 
         assert!(IRI::parse_str("").is_err());


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