From ac1e4a2d58942868801a58f805f7eedcb64ffef0 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Thu, 27 Jun 2024 08:55:28 +0900 Subject: [PATCH] numeric.c (cmpnum): handle the case where both arguments are fixnum first To avoid the cost of `mrb_type(v2)`. --- src/numeric.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/numeric.c b/src/numeric.c index e0cd255ef..783f50789 100644 --- a/src/numeric.c +++ b/src/numeric.c @@ -1898,8 +1898,22 @@ cmpnum(mrb_state *mrb, mrb_value v1, mrb_value v2) #else /* float version */ - mrb_float x = mrb_as_float(mrb, v1); - mrb_float y; + mrb_float x, y; + + if (mrb_fixnum_p(v1)) { + if (mrb_fixnum_p(v2)) { + mrb_int x = mrb_integer(v1); + mrb_int y = mrb_integer(v2); + + if (x > y) return 1; + else if (x < y) return -1; + return 0; + } + x = (mrb_float)mrb_integer(v1); + } + else { + x = mrb_as_float(mrb, v1); + } switch (mrb_type(v2)) { #ifdef MRB_USE_RATIONAL