@@ -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