Reviewers: Erik Corry,

Message:
http://compute3.aar:9013/golem/r4286-v8-serya-FasterParsingInt-vs-4286-v8.html

Description:
Optimization for parsing integers of limited length (in digits).

Please review this at http://codereview.chromium.org/1367004

SVN Base: http://v8.googlecode.com/svn/branches/bleeding_edge/

Affected files:
  M     src/conversions.cc
  M     test/mjsunit/parse-int-float.js


Index: test/mjsunit/parse-int-float.js
===================================================================
--- test/mjsunit/parse-int-float.js     (revision 4286)
+++ test/mjsunit/parse-int-float.js     (working copy)
@@ -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'));

Index: src/conversions.cc
===================================================================
--- src/conversions.cc  (revision 4286)
+++ src/conversions.cc  (working copy)
@@ -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) {
@@ -476,6 +475,10 @@
       }
     }

+    ASSERT(buffer_pos < kBufferSize);
+    buffer[buffer_pos++] = '.';
+    fractional_part = true;
+
     // There is the fractional part.
     while (*current >= '0' && *current <= '9') {
       if (significant_digits < kMaxSignificantDigits) {
@@ -580,6 +583,11 @@
     buffer[buffer_pos++] = '1';
   }

+  // If the number has no more than kMaxDigitsInInt digits and doesn't have
+  // fractional part it could be parsed fatser (without check for
+  // spaces, overflow, etc).
+  const int kMaxDigitsInInt = 9 * sizeof(int) / 4;  // NOLINT
+
   if (exponent != 0) {
     ASSERT(buffer_pos < kBufferSize);
     buffer[buffer_pos++] = 'e';
@@ -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);


--
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