From 5331b5a4d2e49c7f8bed8ac98542d475e3523384 Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Sun, 1 Oct 2023 02:12:17 -0500 Subject: [PATCH] :recycle: 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. --- src/state.rs | 6 +++--- src/value.rs | 13 +++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/state.rs b/src/state.rs index 9951f83..30b957d 100644 --- a/src/state.rs +++ b/src/state.rs @@ -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! (), diff --git a/src/value.rs b/src/value.rs index 9f87ceb..b4495d2 100644 --- a/src/value.rs +++ b/src/value.rs @@ -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 , B: Into > ( &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 > (&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 {