Reviewers: Erik Corry,
Message:
http://compute3.aar:9013/golem/r4309-v8-serya-SmiParsing-vs-4309-v8.html
Description:
Parsing some kinds of integers and junk values in Runtime_NumberToString.
For
that values the string is converted into int and then into SMI. It lets to
avoid
floating point operations.
Please review this at http://codereview.chromium.org/1572004
SVN Base: http://v8.googlecode.com/svn/branches/bleeding_edge/
Affected files:
M src/runtime.cc
Index: src/runtime.cc
===================================================================
--- src/runtime.cc (revision 4309)
+++ src/runtime.cc (working copy)
@@ -4438,11 +4438,62 @@
}
+static bool AreDigits(const char*s, int from, int to) {
+ for (int i = from; i < to; i++) {
+ if (s[i] < '0' || s[i] > '9') return false;
+ }
+
+ return true;
+}
+
+
+static int ParseDecimalInteger(const char*s, int from, int to) {
+ ASSERT(to - from < 10); // Overflow is not possible.
+ ASSERT(from < to);
+ int d = s[from] - '0';
+
+ for (int i = from + 1; i < to; i++) {
+ d = 10 * d + (s[i] - '0');
+ }
+
+ return d;
+}
+
+
static Object* Runtime_StringToNumber(Arguments args) {
NoHandleAllocation ha;
ASSERT(args.length() == 1);
CONVERT_CHECKED(String, subject, args[0]);
subject->TryFlatten();
+
+ // Fast case: short integer or some sorts of junk values.
+ int len = subject->length();
+ if (subject->IsSeqAsciiString()) {
+ if (len == 0) return Smi::FromInt(0);
+
+ char const* data = SeqAsciiString::cast(subject)->GetChars();
+ bool minus = (data[0] == '-');
+ int start_pos = (minus ? 1 : 0);
+
+ if (start_pos == len || data[start_pos] > '9') {
+ // Fast check for a junk value. A valid string may start from a
+ // whitespace, a sign ('+' or '-'), the decimal point, a decimal
digit or
+ // the 'I' character ('Infinity'). All of that have codes greater
than '9'
+ // except 'I'.
+ if (data[start_pos] != 'I') {
+ return Heap::nan_value();
+ }
+ } else if (len - start_pos < 10 && AreDigits(data, start_pos, len)) {
+ int d = ParseDecimalInteger(data, start_pos, len);
+ if (minus) {
+ if (d == 0) return Heap::minus_zero_value();
+ d = -d;
+ }
+ return Smi::FromInt(d);
+ }
+ }
+
+ // Slower case.
return Heap::NumberFromDouble(StringToDouble(subject, ALLOW_HEX));
}
--
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.