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

ruby-changes:74467

From: Kenta <ko1@a...>
Date: Sun, 13 Nov 2022 13:29:35 +0900 (JST)
Subject: [ruby-changes:74467] d1f55dea86 (master): [ruby/bigdecimal] Tweak VpAlloc

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

From d1f55dea864f1ab819bc8332476ae2824c5d0302 Mon Sep 17 00:00:00 2001
From: Kenta Murata <mrkn@m...>
Date: Sun, 13 Nov 2022 13:26:50 +0900
Subject: [ruby/bigdecimal] Tweak VpAlloc

* Stop reusing mx and mf
* Check szVal == NULL first
* Treat special values before checking the leading `#`

https://github.com/ruby/bigdecimal/commit/14f3d965f8
---
 ext/bigdecimal/bigdecimal.c | 64 ++++++++++++++++++++++-----------------------
 1 file changed, 32 insertions(+), 32 deletions(-)

diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c
index fcb4f460e9..4e4ac99162 100644
--- a/ext/bigdecimal/bigdecimal.c
+++ b/ext/bigdecimal/bigdecimal.c
@@ -5054,11 +5054,11 @@ bigdecimal_parse_special_string(const char *str) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L5054
 /*
  * Allocates variable.
  * [Input]
- *   mx ... allocation unit, if zero then mx is determined by szVal.
- *    The mx is the number of effective digits can to be stored.
- *   szVal ... value assigned(char). If szVal==NULL,then zero is assumed.
- *            If szVal[0]=='#' then Max. Prec. will not be considered(1.1.7),
- *            full precision specified by szVal is allocated.
+ *   mx ... The number of decimal digits to be allocated, if zero then mx is determined by szVal.
+ *          The mx will be the number of significant digits can to be stored.
+ *   szVal ... The value assigned(char). If szVal==NULL, then zero is assumed.
+ *             If szVal[0]=='#' then MaxPrec is not affected by the precision limit
+ *             so that the full precision specified by szVal is allocated.
  *
  * [Returns]
  *   Pointer to the newly allocated variable, or
@@ -5069,48 +5069,48 @@ VpAlloc(size_t mx, const char *szVal, int strict_p, int exc) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L5069
 {
     const char *orig_szVal = szVal;
     size_t i, j, ni, ipf, nf, ipe, ne, dot_seen, exp_seen, nalloc;
+    size_t len;
     char v, *psz;
     int  sign=1;
     Real *vp = NULL;
-    size_t mf = VpGetPrecLimit();
+    size_t prec_limit = VpGetPrecLimit();
     VALUE buf;
 
-    mx = (mx + BASE_FIG - 1) / BASE_FIG;    /* Determine allocation unit. */
-    if (mx == 0) ++mx;
+    len = (mx + BASE_FIG - 1) / BASE_FIG;    /* Determine allocation unit. */
+    if (len == 0) ++len;
 
-    if (szVal) {
-        /* Skipping leading spaces */
-        while (ISSPACE(*szVal)) szVal++;
-
-        /* Processing the leading one `#` */
-        if (*szVal != '#') {
-            if (mf) {
-                mf = (mf + BASE_FIG - 1) / BASE_FIG + 2; /* Needs 1 more for div */
-                if (mx > mf) {
-                    mx = mf;
-                }
-            }
-        }
-        else {
-            ++szVal;
-        }
-    }
-    else {
+    if (szVal == NULL) {
       return_zero:
         /* necessary to be able to store */
         /* at least mx digits. */
         /* szVal==NULL ==> allocate zero value. */
         vp = rbd_allocate_struct(mx);
-        vp->MaxPrec = mx;    /* set max precision */
+        vp->MaxPrec = len;   /* set max precision */
         VpSetZero(vp, 1);    /* initialize vp to zero. */
         return vp;
     }
 
+    /* Skipping leading spaces */
+    while (ISSPACE(*szVal)) szVal++;
+
     /* Check on Inf & NaN */
     if ((vp = bigdecimal_parse_special_string(szVal)) != NULL) {
         return vp;
     }
 
+    /* Processing the leading one `#` */
+    if (*szVal != '#') {
+        if (prec_limit) {
+            size_t const max_len = (prec_limit + BASE_FIG - 1) / BASE_FIG + 2; /* Needs 1 more for div */
+            if (len > max_len) {
+                len = max_len;
+            }
+        }
+    }
+    else {
+        ++szVal;
+    }
+
     /* Scanning digits */
 
     /* A buffer for keeping scanned digits */
@@ -5272,11 +5272,11 @@ VpAlloc(size_t mx, const char *szVal, int strict_p, int exc) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L5272
 
     nalloc = (ni + nf + BASE_FIG - 1) / BASE_FIG + 1;    /* set effective allocation  */
     /* units for szVal[]  */
-    if (mx == 0) mx = 1;
-    nalloc = Max(nalloc, mx);
-    mx = nalloc;
-    vp = rbd_allocate_struct(mx);
-    vp->MaxPrec = mx;        /* set max precision */
+    if (len == 0) len = 1;
+    nalloc = Max(nalloc, len);
+    len = nalloc;
+    vp = rbd_allocate_struct(len);
+    vp->MaxPrec = len;        /* set max precision */
     VpSetZero(vp, sign);
     VpCtoV(vp, psz, ni, psz + ipf, nf, psz + ipe, ne);
     rb_str_resize(buf, 0);
-- 
cgit v1.2.3


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

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