Commit 41d82b55

41d82b553a6aMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified

Description

Pointer-equality fast path in sp_str_eq (case-on-string dispatch)

A pointer-equality pre-check before strcmp short-circuits string comparison when both operands are the same pointer -- which, under clang's -fmerge-all-constants (default at -O2+), is the case whenever two string literals of identical content meet. This is the case-on-string dispatch lever from #282: case x when "foo" lowers to sp_str_eq(x, "foo"), so a literal-flow subject now compares in O(1).

Applied in the runtime function rather than per codegen site, so every sp_str_eq caller benefits; behaviour is unchanged (a pure fast path).

Refs #1396 #282 Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
41d82b553a6a
Parent
eedc17323ec3

lib/sp_runtime.hmodifiedView file

@@ -580,7 +580,7 @@
580580 dereference NULL on either side. nil-vs-string equality is false in
581581 Ruby; nil == nil is true, so falling back to pointer equality on the
582582 NULL path covers both. */
583-static inline int sp_str_eq(const char*a,const char*b){if(!a||!b)return a==b;return strcmp(a,b)==0;}
583+static inline int sp_str_eq(const char*a,const char*b){if(a==b)return 1;if(!a||!b)return 0;return strcmp(a,b)==0;}
584584 static size_t sp_utf8_byte_offset(const char*s,mrb_int char_idx){
585585 if (!s || char_idx <= 0) return 0;
586586 if (sp_str_cacheable(s)) {