[前][次][番号順一覧][スレッド一覧]

ruby-changes:64765

From: Kenta <ko1@a...>
Date: Wed, 6 Jan 2021 10:57:22 +0900 (JST)
Subject: [ruby-changes:64765] f289f8ae3a (master): [ruby/bigdecimal] Optimize the conversion from small Bignum

https://git.ruby-lang.org/ruby.git/commit/?id=f289f8ae3a

From f289f8ae3ab3e76287597722c52cd8cfcfc694ad Mon Sep 17 00:00:00 2001
From: Kenta Murata <mrkn@m...>
Date: Wed, 6 Jan 2021 10:25:45 +0900
Subject: [ruby/bigdecimal] Optimize the conversion from small Bignum

https://github.com/ruby/bigdecimal/commit/4792a917d8

diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c
index b4f7114..a912908 100644
--- a/ext/bigdecimal/bigdecimal.c
+++ b/ext/bigdecimal/bigdecimal.c
@@ -2763,8 +2763,32 @@ rb_int64_convert_to_BigDecimal(int64_t ival, size_t digs, int raise_exception) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L2763
 static VALUE
 rb_big_convert_to_BigDecimal(VALUE val, RB_UNUSED_VAR(size_t digs), int raise_exception)
 {
-    Real *vp = GetVpValue(val, 1);
-    return check_exception(vp->obj);
+    assert(RB_TYPE_P(val, T_BIGNUM));
+
+    size_t size = rb_absint_size(val, NULL);
+    int sign = rb_big_cmp(val, INT2FIX(0));
+    if (size <= sizeof(long)) {
+        if (sign < 0) {
+            return rb_int64_convert_to_BigDecimal(NUM2LONG(val), digs, raise_exception);
+        }
+        else {
+            return rb_uint64_convert_to_BigDecimal(NUM2ULONG(val), digs, raise_exception);
+        }
+    }
+#if defined(SIZEOF_LONG_LONG) && SIZEOF_LONG < SIZEOF_LONG_LONG
+    else if (size <= sizeof(LONG_LONG)) {
+        if (sign < 0) {
+            return rb_int64_convert_to_BigDecimal(NUM2LL(val), digs, raise_exception);
+        }
+        else {
+            return rb_uint64_convert_to_BigDecimal(NUM2ULL(val), digs, raise_exception);
+        }
+    }
+#endif
+    else {
+        Real *vp = GetVpValue(val, 1);
+        return check_exception(vp->obj);
+    }
 }
 
 static VALUE
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]