Revision: 4304
Author: [email protected]
Date: Mon Mar 29 04:17:34 2010
Log: Optimization for parsing integers of limited length (in digits).
Review URL: http://codereview.chromium.org/1367004
http://code.google.com/p/v8/source/detail?r=4304

Modified:
 /branches/bleeding_edge/src/conversions.cc
 /branches/bleeding_edge/test/mjsunit/parse-int-float.js

=======================================
--- /branches/bleeding_edge/src/conversions.cc  Thu Mar 25 07:39:39 2010
+++ /branches/bleeding_edge/src/conversions.cc  Mon Mar 29 04:17:34 2010
@@ -377,6 +377,7 @@
   int significant_digits = 0;
   int insignificant_digits = 0;
   bool nonzero_digit_dropped = false;
+  bool fractional_part = false;

   double signed_zero = 0.0;

@@ -454,8 +455,6 @@
   }

   if (*current == '.') {
-    ASSERT(buffer_pos < kBufferSize);
-    buffer[buffer_pos++] = '.';
     ++current;
     if (current == end) {
       if (significant_digits == 0 && !leading_zero) {
@@ -475,6 +474,10 @@
         exponent--;  // Move this 0 into the exponent.
       }
     }
+
+    ASSERT(buffer_pos < kBufferSize);
+    buffer[buffer_pos++] = '.';
+    fractional_part = true;

     // There is the fractional part.
     while (*current >= '0' && *current <= '9') {
@@ -579,6 +582,11 @@
     if (insignificant_digits) buffer[buffer_pos++] = '.';
     buffer[buffer_pos++] = '1';
   }
+
+  // If the number has no more than kMaxDigitsInInt digits and doesn't have
+  // fractional part it could be parsed faster (without checks for
+  // spaces, overflow, etc.).
+  const int kMaxDigitsInInt = 9 * sizeof(int) / 4;  // NOLINT

   if (exponent != 0) {
     ASSERT(buffer_pos < kBufferSize);
@@ -597,6 +605,16 @@
     }
     ASSERT(exponent == 0);
     buffer_pos += exp_digits;
+  } else if (!fractional_part && significant_digits <= kMaxDigitsInInt) {
+    if (significant_digits == 0) return signed_zero;
+    ASSERT(buffer_pos > 0);
+    int num = 0;
+    int start_pos = (buffer[0] == '-' ? 1 : 0);
+    for (int i = start_pos; i < buffer_pos; i++) {
+      ASSERT(buffer[i] >= '0' && buffer[i] <= '9');
+      num = 10 * num + (buffer[i] - '0');
+    }
+    return static_cast<double>(start_pos == 0 ? num : -num);
   }

   ASSERT(buffer_pos < kBufferSize);
=======================================
--- /branches/bleeding_edge/test/mjsunit/parse-int-float.js Tue Nov 17 05:54:05 2009 +++ /branches/bleeding_edge/test/mjsunit/parse-int-float.js Mon Mar 29 04:17:34 2010
@@ -47,6 +47,7 @@

 assertEquals(0.1, parseFloat('0.1'));
 assertEquals(0.1, parseFloat('0.1aaa'));
+assertEquals(0, parseFloat('0aaa'));
 assertEquals(0, parseFloat('0x12'));
 assertEquals(77, parseFloat('077'));

--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

To unsubscribe from this group, send email to v8-dev+unsubscribegooglegroups.com or reply 
to this email with the words "REMOVE ME" as the subject.

Reply via email to