diff --git a/src/table.rs b/src/table.rs index 53a731a..49e7b76 100644 --- a/src/table.rs +++ b/src/table.rs @@ -1,6 +1,6 @@ -use deimos::gc::{Allocates, Id, MaybeId, Traverse}; use crate::vm::{Callable, MarsAllocator, RunError, Value}; -use deimos::map::{Equivalent, Hashed, Hashes, TableEntry, Map, Entry}; +use deimos::gc::{Allocates, Id, MaybeId, Traverse}; +use deimos::map::{DisplacementSet, Entry, Equivalent, Hashed, Hashes, Map, TableEntry}; #[derive(Debug, Clone)] struct KeyValue { @@ -21,12 +21,10 @@ impl Hashes for KeyValue { } impl TableEntry for KeyValue { - fn new_vacant() -> Self { - KeyValue { - index: Value::Nil, - value: Value::Nil, - } - } + const VACANT: Self = KeyValue { + index: Value::Nil, + value: Value::Nil, + }; fn is_vacant(&self) -> bool { matches!( self, @@ -64,7 +62,7 @@ impl Hashes for Value { } } Value::Table(table) => table.hashed(), - Value::Object(object,..) => object.hashed(), + Value::Object(object, ..) => object.hashed(), #[cfg(feature = "vector3")] Value::Vector(vec) => { let mut hasher = DefaultHasher::new(); @@ -78,12 +76,18 @@ impl Hashes for Value { // todo: displaced items don't ever get shuffled closer to their homes unless the table is resized #[derive(Debug)] pub struct Table { - table: Map, + table: DisplacementSet, array: Vec, pub meta: MaybeId, } -impl Traverse for Table where C: MarsAllocator { +impl Traverse for Table +where + C: MarsAllocator, +{ + fn collect(self) { + todo!() + } fn traverse(id: &Id
, context: &mut C) { for i in 0..context.freelist().get(id).table.map.len() { let entry = context.freelist().get(id).table.map[i].entry.clone(); @@ -122,7 +126,7 @@ impl Table { self.array[index as usize - 1] = Value::Nil } } - self.table.rem_table(Value::Integer(index)); + self.table.remove(Value::Integer(index)); } item => { if (0..self.array.len() + 1).contains(&(index as usize - 1)) { @@ -132,7 +136,7 @@ impl Table { self.array[index as usize - 1] = item; } } else { - self.table.set_table(KeyValue { + self.table.set(KeyValue { index: Value::Integer(index), value: item, }) @@ -144,7 +148,7 @@ impl Table { Value::Nil => Err(RunError( "Attempt to set new index of table with key: nil".to_string(), )), - index => Ok(self.table.set_table(KeyValue { index, value })), + index => Ok(self.table.set(KeyValue { index, value })), } } pub fn get(&mut self, index: Value) -> Result { @@ -159,11 +163,8 @@ impl Table { // todo: move this into the Map Ok(self .table - .get_table(index) - .unwrap_or(&KeyValue { - index: Value::Nil, - value: Value::Nil, - }) + .get(index) + .unwrap_or(&KeyValue::VACANT) .value .clone()) } @@ -174,7 +175,7 @@ impl Table { } pub fn new() -> Self { Table { - table: Map::new(), + table: DisplacementSet::new(), array: Vec::new(), meta: MaybeId::NULL, } diff --git a/src/vm.rs b/src/vm.rs index f52ffc2..7403b72 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -182,6 +182,12 @@ const SHORT_STRING_LEN: usize = 40; #[derive(Clone, Debug)] pub(crate) struct CachedString(Rc, pub(crate) Hashed); +impl Hashes for CachedString { + fn hashed(&self) -> Hashed { + self.1 + } +} + impl PartialEq for CachedString { fn eq(&self, other: &Self) -> bool { self.1 == other.1 && self.0 == other.0 @@ -606,13 +612,11 @@ impl Machine { pub fn new_string(&mut self, string: &str) -> Result { // todo: why Result of RunError? if string.len() < SHORT_STRING_LEN { - let existing = self.cache.get_table(string); - match existing { - Some(ref entry) => Ok(Value::CachedString(entry.0.clone().unwrap())), + match self.cache.get(string) { + Some(ref entry) => Ok(Value::CachedString(entry.clone())), None => { let cached = CachedString(Rc::from(string), string.hashed()); - self.cache - .set_table(CachedStringEntry(Some(cached.clone()))); + self.cache.set(cached.clone()); Ok(Value::CachedString(cached)) } }