From c1bea626b1516f383035fcd098f22725edf5282d Mon Sep 17 00:00:00 2001 From: Joscha Date: Sun, 26 Sep 2021 14:48:02 +0200 Subject: [PATCH] Optimize size of Key and Value types Down from 32 or 40 bytes to 16 bytes each, although with an added indirection for strings. --- src/main.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index a7f4af2..8dce083 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,7 +35,7 @@ impl Hash for Table { #[derive(PartialEq, Eq, Hash)] enum Key { - String(String), + String(Box), Bool(bool), Int(i64), Table(Table), @@ -54,14 +54,20 @@ impl Debug for Key { #[derive(PartialEq)] enum Value { - Key(Key), + String(Box), + Bool(bool), + Int(i64), + Table(Table), Float(f64), } impl Debug for Value { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Self::Key(k) => k.fmt(f), + Self::String(s) => s.fmt(f), + Self::Bool(b) => b.fmt(f), + Self::Int(i) => i.fmt(f), + Self::Table(t) => t.fmt(f), Self::Float(d) => d.fmt(f), } } @@ -72,11 +78,11 @@ fn main() { let mut table = HashMap::new(); table.insert( - Key::String("Hello".into()), - Value::Key(Key::String("World".into())), + Key::String(Box::new("Hello".into())), + Value::String(Box::new("World".into())), ); let table = Rc::new(RefCell::new(table)); - let table_value = Value::Key(Key::Table(Table(Rc::downgrade(&table)))); + let table_value = Value::Table(Table(Rc::downgrade(&table))); dbg!(table_value); }