From eff8d7be34deed12e4c7bb472a2aae2580d21949 Mon Sep 17 00:00:00 2001 From: Thomas Klemm Date: Tue, 10 Nov 2026 23:07:55 +0000 Subject: [PATCH] Reuse the frozen zero when converting Integer 0 to BigDecimal Converting Integer 0 allocated a new zero every time, so x > 0, x + 0, x.coerce(0), BigDecimal(0) and 0.to_d each allocated a temporary object. BigDecimal objects are frozen and Float 0.0 already converts to the shared positive zero, so return it for Integer 0 too. --- ext/bigdecimal/bigdecimal.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index 2c28e13a..afc99e62 100644 --- a/ext/bigdecimal/bigdecimal.c +++ b/ext/bigdecimal/bigdecimal.c @@ -2551,12 +2551,7 @@ rb_uint64_convert_to_BigDecimal(uint64_t uval) VALUE bd; Real *vp; if (uval == 0) { - bd = BigDecimal_allocate(1); - vp = VpPtr(bd); - vp->Prec = 1; - vp->exponent = 1; - VpSetZero(vp, 1); - vp->frac[0] = 0; + return BigDecimal_positive_zero(); } else if (uval < BASE) { bd = BigDecimal_allocate(1);