🚧 wip: Vec seems to be faster than BTreeMap here, slightly

main
_ 2023-10-02 20:13:43 -05:00
parent 4e23f51634
commit 7878efc235
1 changed files with 9 additions and 9 deletions

View File

@ -247,6 +247,7 @@ impl Value {
pub struct Table { pub struct Table {
array: Vec <Value>, array: Vec <Value>,
hash: HashMap <Value, Value>, hash: HashMap <Value, Value>,
strings: Vec <(InternedString, Value)>,
map: BTreeMap <InternedString, Value>, map: BTreeMap <InternedString, Value>,
} }
@ -262,7 +263,7 @@ impl Table {
fn get_inner (&self, key: &Value) -> &Value { fn get_inner (&self, key: &Value) -> &Value {
match key { match key {
Value::Nil => &NIL, 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), Value::Integer (x) => self.array.get (usize::try_from (*x).unwrap ()).unwrap_or (&NIL),
x => self.hash.get (x).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 { pub fn get_str (&self, key: InternedString) -> &Value {
match self.map.get (&key) { self.strings.iter ().find (|(hay, _)| hay == &key).map (|(_, v)| v).unwrap_or (&NIL)
None => &NIL,
Some (x) => x,
}
} }
/// Insert value at arbitrary key /// Insert value at arbitrary key
@ -293,9 +291,7 @@ impl Table {
match a.into () { match a.into () {
Value::Integer (x) => self.insert_int (x, b), Value::Integer (x) => self.insert_int (x, b),
Value::Nil => (), Value::Nil => (),
Value::String (x) => { Value::String (x) => self.insert_str (x, b.into ()),
self.map.insert (x, b.into ());
},
x => { x => {
self.hash.insert (x, b.into ()); self.hash.insert (x, b.into ());
}, },
@ -312,7 +308,11 @@ impl Table {
} }
pub fn insert_str (&mut self, key: InternedString, v: Value) { 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 { pub fn length (&self) -> i64 {