🚧 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 {
array: Vec <Value>,
hash: HashMap <Value, Value>,
strings: Vec <(InternedString, Value)>,
map: BTreeMap <InternedString, Value>,
}
@ -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 {