♻️ refactor: mark where we can optimize tables for int keys

n-body actually doesn't need this, it's bottlenecked on string key
reads and writes.
main
_ 2023-10-01 02:12:17 -05:00
parent 4e9f9a74c5
commit 5331b5a4d2
2 changed files with 16 additions and 3 deletions

View File

@ -508,7 +508,7 @@ impl <'a> State <'a> {
let value = {
let table = self.reg (*b).as_table ().expect ("GetI only works on tables").borrow ();
table.get (key)
table.get_int (key)
};
*self.reg_mut (*a) = value;
@ -752,7 +752,7 @@ impl <'a> State <'a> {
let mut dst = self.reg_mut (*a).as_table ().expect ("SetI only works on tables").borrow_mut ();
dst.insert (i64::from (*b), value);
dst.insert_int (i64::from (*b), value);
},
Instruction::SetList (a, b, c, k) => {
if *b == 0 {
@ -766,7 +766,7 @@ impl <'a> State <'a> {
for i in 1..=*b {
let src = self.reg (*a + i);
dst.insert (Value::from (i64::from (*c + i)), src.clone ());
dst.insert_int (i64::from (*c + i), src.clone ());
}
},
Instruction::SetTabUp (_a, _b, _c) => unimplemented! (),

View File

@ -261,10 +261,16 @@ impl Table {
self.get_inner (&(key.into ()))
}
pub fn get_int (&self, key: i64) -> Value {
self.get_inner (&(key.into ()))
}
fn insert_inner (&mut self, a: Value, b: Value) {
self.hash.insert (a, b);
}
/// Insert value at arbitrary key
pub fn insert <A: Into <Value>, B: Into <Value>> (
&mut self,
a: A,
@ -273,6 +279,13 @@ impl Table {
self.insert_inner (a.into (), b.into ())
}
/// Insert value at integer key
pub fn insert_int <A: Into <Value>> (&mut self, k: i64, v: A)
{
self.insert_inner (k.into (), v.into ())
}
pub fn length (&self) -> i64 {
for i in 1..i64::MAX {
if self.get (i) == Value::Nil {