changeset 6575:cc055e12248d

8024356: Double.parseDouble() is slow for long Strings Summary: Clamp nDigits to MAX_NDIGITS. Reviewed-by: bpb, drchase, shade Contributed-by: Dmitry Nadezhin <dmitry.nadezhin@oracle.com>
author bpb
date Tue, 17 Sep 2013 13:06:05 +0100
parents a0ae97463c58
children 6af8b9ce93e1
files src/share/classes/sun/misc/FloatingDecimal.java
diffstat 1 files changed, 13 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/sun/misc/FloatingDecimal.java	Mon Sep 16 17:17:42 2013 +0200
+++ b/src/share/classes/sun/misc/FloatingDecimal.java	Tue Sep 17 13:06:05 2013 +0100
@@ -70,6 +70,15 @@
     static final int    minDecimalExponent = -324;
     static final int    bigDecimalExponent = 324; // i.e. abs(minDecimalExponent)
 
+    //
+    // The value below is chosen as a conservative threshold. It
+    // can be demonstrated that a decimal ulp less than 10^(-1075)
+    // is enough to guarantee correctness. Compensation is also made
+    // for the binary mantissa which takes 53 binary digits, or
+    // 17 decimal ones. Hence 1075 + 17 =~ 1100.
+    //
+    static final int    MAX_NDIGITS = 1100;
+
     static final long   highbyte = 0xff00000000000000L;
     static final long   highbit  = 0x8000000000000000L;
     static final long   lowbytes = ~highbyte;
@@ -1468,6 +1477,10 @@
              * Formulate the EXACT big-number result as
              * bigD0 * 10^exp
              */
+            if (nDigits > MAX_NDIGITS) {
+                nDigits = MAX_NDIGITS + 1;
+                digits[MAX_NDIGITS] = '1';
+            }
             FDBigInt bigD0 = new FDBigInt( lValue, digits, kDigits, nDigits );
             exp   = decExponent - nDigits;