Skip to content

Commit 1e4bb8d

Browse files
committed
use macro
1 parent 2f2f8aa commit 1e4bb8d

File tree

15 files changed

+902
-767
lines changed

15 files changed

+902
-767
lines changed

crates/luars/src/gc/mod.rs

Lines changed: 85 additions & 75 deletions
Large diffs are not rendered by default.

crates/luars/src/lua_value/lua_table.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ impl Node {
3232
/// A bit set to 1 means the metamethod is NOT present (absence cache)
3333
/// Only the first 6 metamethods use this optimization (TM_INDEX..TM_EQ)
3434
pub mod tm_flags {
35-
pub const TM_INDEX: u8 = 1 << 0; // __index
36-
pub const TM_NEWINDEX: u8 = 1 << 1; // __newindex
37-
pub const TM_GC: u8 = 1 << 2; // __gc
38-
pub const TM_MODE: u8 = 1 << 3; // __mode
39-
pub const TM_LEN: u8 = 1 << 4; // __len
40-
pub const TM_EQ: u8 = 1 << 5; // __eq
41-
pub const TM_CALL: u8 = 1 << 6; // __call (bonus: very common)
42-
pub const MASK_ALL: u8 = 0x7F; // All 7 bits
35+
pub const TM_INDEX: u8 = 1 << 0; // __index
36+
pub const TM_NEWINDEX: u8 = 1 << 1; // __newindex
37+
pub const TM_GC: u8 = 1 << 2; // __gc
38+
pub const TM_MODE: u8 = 1 << 3; // __mode
39+
pub const TM_LEN: u8 = 1 << 4; // __len
40+
pub const TM_EQ: u8 = 1 << 5; // __eq
41+
pub const TM_CALL: u8 = 1 << 6; // __call (bonus: very common)
42+
pub const MASK_ALL: u8 = 0x7F; // All 7 bits
4343
}
4444

4545
/// Lua table implementation
@@ -61,7 +61,7 @@ pub struct LuaTable {
6161
/// Metatable - optional table that defines special behaviors
6262
/// Store as LuaValue (table ID) instead of Rc for ID-based architecture
6363
metatable: Option<LuaValue>,
64-
64+
6565
/// Metamethod absence flags (like Lua 5.4)
6666
/// A bit set to 1 means the metamethod is NOT present (cached absence)
6767
/// This allows O(1) check for common metamethods instead of hash lookup

crates/luars/src/lua_value/lua_value.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -401,30 +401,34 @@ impl LuaValue {
401401
// ============ Equality ============
402402

403403
/// Raw equality (no metamethods)
404-
/// OPTIMIZED: Branchless comparison for common case, only branch for float NaN handling
404+
/// OPTIMIZED: Branchless comparison for common case
405405
#[inline(always)]
406406
pub fn raw_equal(&self, other: &Self) -> bool {
407-
// Compare both primary and secondary
408-
// This is branchless and correct for:
409-
// - Strings/Tables/Functions: primary has tag|id, secondary is 0
410-
// - Integers: primary is TAG_INTEGER, secondary has value
411-
// - Booleans: primary is TAG_TRUE or TAG_FALSE, secondary is 0
412-
// - Nil: primary is TAG_NIL, secondary is 0
413-
//
414-
// Special case: Float - IEEE 754 says NaN != NaN, but bit comparison would say equal
415-
// We handle this with a single branch check
416-
if self.primary == other.primary && self.secondary == other.secondary {
417-
// Fast path: bits match
418-
// Need to verify this isn't two NaNs comparing equal
419-
if (self.primary & TAG_MASK) == TAG_FLOAT {
420-
// For floats, use proper IEEE comparison
421-
let a = f64::from_bits(self.secondary);
422-
a == a // Returns false if NaN
423-
} else {
424-
true
407+
// Fast path: compare primary (type tag + ID for GC objects)
408+
if self.primary != other.primary {
409+
return false;
410+
}
411+
412+
// Primary matches - now check secondary based on type
413+
let tag = self.primary & TAG_MASK;
414+
415+
// For nil, boolean: only primary matters (secondary is unused)
416+
// For integers, floats: need to compare secondary
417+
// For GC objects: secondary is unused, ID is in primary
418+
match tag {
419+
TAG_NIL | TAG_FALSE | TAG_TRUE => true,
420+
TAG_FLOAT => {
421+
// IEEE 754: NaN != NaN, but our bits might match
422+
// Need proper float comparison
423+
if self.secondary == other.secondary {
424+
let a = f64::from_bits(self.secondary);
425+
a == a // Returns false if NaN
426+
} else {
427+
false
428+
}
425429
}
426-
} else {
427-
false
430+
TAG_INTEGER => self.secondary == other.secondary,
431+
_ => true, // GC objects: ID is in primary, secondary unused
428432
}
429433
}
430434

0 commit comments

Comments
 (0)