From 04af58db8920566cd6de637c1f88288d01b34033 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Tue, 21 Oct 2025 17:54:49 +0900 Subject: [PATCH] array.c: hoist RARRAY_PTR calls in comparison operator Optimizes Array#<=> by hoisting RARRAY_PTR calls outside the loop to avoid repeated conditional checks. This is a frequently used operation for array comparisons and sorting. Before: 2 RARRAY_PTR calls per iteration (checks embed vs heap twice) After: 2 RARRAY_PTR calls total (pointers cached outside loop) Co-authored-by: Claude --- src/array.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/array.c b/src/array.c index fd043786a..a73e9a98a 100644 --- a/src/array.c +++ b/src/array.c @@ -1956,8 +1956,11 @@ mrb_ary_cmp(mrb_state *mrb, mrb_value ary1) if (mrb_obj_equal(mrb, ary1, ary2)) return mrb_fixnum_value(0); if (!mrb_array_p(ary2)) return mrb_nil_value(); + /* Hoist pointer retrieval outside loop to avoid repeated conditionals */ + mrb_value *ptr1 = RARRAY_PTR(ary1); + mrb_value *ptr2 = RARRAY_PTR(ary2); for (mrb_int i=0; i