From 0ec5c3e4931e96e92d1b6be74f005354d8cbc50d Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Mon, 11 Apr 2022 11:12:47 +0900 Subject: [PATCH] bigint.c (mrb_bint_cmp): avoid object allocation for comparison. --- src/bigint.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/bigint.c b/src/bigint.c index f204f0346..9179ffbd4 100644 --- a/src/bigint.c +++ b/src/bigint.c @@ -1243,11 +1243,19 @@ mrb_bint_cmp(mrb_state *mrb, mrb_value x, mrb_value y) return -1; } #endif + struct RBigint *b = RBIGINT(x); if (!mrb_bigint_p(y)) { if (!mrb_integer_p(y)) return -2; /* type mismatch */ - y = mrb_bint_new_int(mrb, mrb_integer(y)); + + mrb_int i1, i2 = mrb_integer(y); + if (mpz_get_int(&b->mp, &i1)) { + if (i1 == i2) return 0; + if (i1 > i2) return 1; + return -1; + } + if (b->mp.sn > 0) return 1; + return -1; } - struct RBigint *b = RBIGINT(x); struct RBigint *b2 = RBIGINT(y); return mpz_cmp(mrb, &b->mp, &b2->mp); }