Skip to content

Commit 6dc6d73

Browse files
committed
improve table access
1 parent aeb68c5 commit 6dc6d73

4 files changed

Lines changed: 70 additions & 2 deletions

File tree

crates/luars/src/lua_value/lua_table/native_table.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,34 @@ impl NativeTable {
319319
}
320320
}
321321

322+
#[inline]
323+
pub fn set_existing_shortstr_parts(&mut self, key: &LuaValue, value: Value, tt: u8) -> bool {
324+
if self.node.is_null() {
325+
return false;
326+
}
327+
328+
let mut node = self.mainposition_string(key);
329+
let key_ptr = unsafe { key.value.i };
330+
331+
unsafe {
332+
loop {
333+
if (*node).key_tt == LUA_VSHRSTR && (*node).key_data.i == key_ptr {
334+
if (*node).val_tt != LUA_VNIL {
335+
(*node).set_value_parts(value, tt);
336+
return true;
337+
}
338+
return false;
339+
}
340+
341+
let next = (*node).next;
342+
if next == 0 {
343+
return false;
344+
}
345+
node = node.offset(next as isize);
346+
}
347+
}
348+
}
349+
322350
/// Fast path for repeated writes to an existing integer key.
323351
/// Mirrors C Lua's fastset semantics: when the key already has a live slot,
324352
/// the update can ignore `__newindex` and complete in place.

crates/luars/src/lua_vm/execute/execute_loop.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,12 @@ pub fn lua_execute(lua_state: &mut LuaState, target_depth: usize) -> LuaResult<(
470470
if meta.is_null() || meta.as_mut_ref().data.no_tm(TmKind::NewIndex.into()) {
471471
let (new_key, delta, rc_tt) = if instr.get_k() {
472472
let rc = *k_val!(c);
473+
if table.impl_table.set_existing_shortstr(key, rc) {
474+
if rc.is_collectable() {
475+
lua_state.gc_barrier_back(gc_ptr);
476+
}
477+
continue;
478+
}
473479
let pset_result = table.impl_table.pset_shortstr(key, rc);
474480
let (new_key, delta) =
475481
table.impl_table.finish_shortstr_set(key, rc, pset_result);
@@ -479,6 +485,15 @@ pub fn lua_execute(lua_state: &mut LuaState, target_depth: usize) -> LuaResult<(
479485
unsafe { lua_state.stack().as_ptr().add(stack_id!(c)) };
480486
let rc_tt = unsafe { (*rc_ptr).tt };
481487
let rc_value = unsafe { (*rc_ptr).value };
488+
if table
489+
.impl_table
490+
.set_existing_shortstr_parts(key, rc_value, rc_tt)
491+
{
492+
if rc_tt & 0x40 != 0 {
493+
lua_state.gc_barrier_back(gc_ptr);
494+
}
495+
continue;
496+
}
482497
let pset_result =
483498
table.impl_table.pset_shortstr_parts(key, rc_value, rc_tt);
484499
let (new_key, delta) = table.impl_table.finish_shortstr_set_parts(
@@ -639,6 +654,14 @@ pub fn lua_execute(lua_state: &mut LuaState, target_depth: usize) -> LuaResult<(
639654
let meta = table.meta_ptr();
640655
if meta.is_null() || meta.as_mut_ref().data.no_tm(TmKind::NewIndex.into()) {
641656
if rb.is_short_string() {
657+
if table.impl_table.set_existing_shortstr(&rb, rc) {
658+
if rc.is_collectable() || rb.is_collectable() {
659+
lua_state.gc_barrier_back(unsafe {
660+
ra.as_gc_ptr_table_unchecked()
661+
});
662+
}
663+
continue;
664+
}
642665
let pset_result = table.impl_table.pset_shortstr(&rb, rc);
643666
let (new_key, delta) =
644667
table.impl_table.finish_shortstr_set(&rb, rc, pset_result);
@@ -792,6 +815,14 @@ pub fn lua_execute(lua_state: &mut LuaState, target_depth: usize) -> LuaResult<(
792815
if meta.is_null() || meta.as_mut_ref().data.no_tm(TmKind::NewIndex.into()) {
793816
let (new_key, delta, rc_tt) = if instr.get_k() {
794817
let rc = *k_val!(c);
818+
if table.impl_table.set_existing_shortstr(key, rc) {
819+
if rc.is_collectable() {
820+
lua_state.gc_barrier_back(unsafe {
821+
(*ra_ptr).as_gc_ptr_table_unchecked()
822+
});
823+
}
824+
continue;
825+
}
795826
let pset_result = table.impl_table.pset_shortstr(key, rc);
796827
let (new_key, delta) =
797828
table.impl_table.finish_shortstr_set(key, rc, pset_result);
@@ -801,6 +832,17 @@ pub fn lua_execute(lua_state: &mut LuaState, target_depth: usize) -> LuaResult<(
801832
unsafe { lua_state.stack().as_ptr().add(stack_id!(c)) };
802833
let rc_tt = unsafe { (*rc_ptr).tt };
803834
let rc_value = unsafe { (*rc_ptr).value };
835+
if table
836+
.impl_table
837+
.set_existing_shortstr_parts(key, rc_value, rc_tt)
838+
{
839+
if rc_tt & 0x40 != 0 {
840+
lua_state.gc_barrier_back(unsafe {
841+
(*ra_ptr).as_gc_ptr_table_unchecked()
842+
});
843+
}
844+
continue;
845+
}
804846
let pset_result =
805847
table.impl_table.pset_shortstr_parts(key, rc_value, rc_tt);
806848
let (new_key, delta) = table.impl_table.finish_shortstr_set_parts(

run_benchmarks.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ function Write-ColorHost {
4343
if ($NoColor) {
4444
Write-Output $Message
4545
} else {
46-
Write-Output $Message
4746
Write-Host $Message -ForegroundColor $Color
4847
}
4948
}

run_lua_benchmarks.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ function Write-ColorHost {
2121
if ($NoColor) {
2222
Write-Output $Message
2323
} else {
24-
Write-Output $Message
2524
Write-Host $Message -ForegroundColor $Color
2625
}
2726
}

0 commit comments

Comments
 (0)