From 7878efc2355f163ca20f684ebe9a3e5bfba44d22 Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Mon, 2 Oct 2023 20:13:43 -0500 Subject: [PATCH] :construction: wip: Vec seems to be faster than BTreeMap here, slightly --- lunar_wave_vm/src/value.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lunar_wave_vm/src/value.rs b/lunar_wave_vm/src/value.rs index e4b2eb0..fb289b6 100644 --- a/lunar_wave_vm/src/value.rs +++ b/lunar_wave_vm/src/value.rs @@ -247,6 +247,7 @@ impl Value { pub struct Table { array: Vec , hash: HashMap , + strings: Vec <(InternedString, Value)>, map: BTreeMap , } @@ -262,7 +263,7 @@ impl Table { fn get_inner (&self, key: &Value) -> &Value { match key { Value::Nil => &NIL, - Value::String (x) => self.map.get (x).unwrap_or (&NIL), + Value::String (x) => self.get_str (*x), Value::Integer (x) => self.array.get (usize::try_from (*x).unwrap ()).unwrap_or (&NIL), x => self.hash.get (x).unwrap_or (&NIL), } @@ -277,10 +278,7 @@ impl Table { } pub fn get_str (&self, key: InternedString) -> &Value { - match self.map.get (&key) { - None => &NIL, - Some (x) => x, - } + self.strings.iter ().find (|(hay, _)| hay == &key).map (|(_, v)| v).unwrap_or (&NIL) } /// Insert value at arbitrary key @@ -293,9 +291,7 @@ impl Table { match a.into () { Value::Integer (x) => self.insert_int (x, b), Value::Nil => (), - Value::String (x) => { - self.map.insert (x, b.into ()); - }, + Value::String (x) => self.insert_str (x, b.into ()), x => { self.hash.insert (x, b.into ()); }, @@ -312,7 +308,11 @@ impl Table { } pub fn insert_str (&mut self, key: InternedString, v: Value) { - self.map.insert (key, v); + match self.strings.iter_mut ().find (|(hay, _)| hay == &key).map (|(_, v)| v) + { + None => self.strings.push ((key, v)), + Some (x) => *x = v, + } } pub fn length (&self) -> i64 {