Skip to content
Permalink
Browse files
Speed up parseInt and parseFloat.
Repeatedly indexing into a UString is slow, so retrieve a pointer into the underlying buffer once up front
and use that instead.  This is a 7% win on a parseInt/parseFloat micro-benchmark.

Reviewed by Cameron Zwarich.

Canonical link: https://commits.webkit.org/29316@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@37048 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
bdash committed Sep 29, 2008
1 parent f512028 commit f1759f1e405313851a73a9b6294844f97701f26a
Showing 2 changed files with 24 additions and 9 deletions.
@@ -1,3 +1,16 @@
2008-09-28 Mark Rowe <mrowe@apple.com>

Reviewed by Cameron Zwarich.

Speed up parseInt and parseFloat.

Repeatedly indexing into a UString is slow, so retrieve a pointer into the underlying buffer once up front
and use that instead. This is a 7% win on a parseInt/parseFloat micro-benchmark.

* kjs/JSGlobalObjectFunctions.cpp:
(JSC::parseInt):
(JSC::parseFloat):

2008-09-28 Simon Hausmann <hausmann@webkit.org>

Reviewed by David Hyatt.
@@ -195,26 +195,27 @@ double parseIntOverflow(const char* s, int length, int radix)
static double parseInt(const UString& s, int radix)
{
int length = s.size();
const UChar* data = s.data();
int p = 0;

while (p < length && isStrWhiteSpace(s[p]))
while (p < length && isStrWhiteSpace(data[p]))
++p;

double sign = 1;
if (p < length) {
if (s[p] == '+')
if (data[p] == '+')
++p;
else if (s[p] == '-') {
else if (data[p] == '-') {
sign = -1;
++p;
}
}

if ((radix == 0 || radix == 16) && length - p >= 2 && s[p] == '0' && (s[p + 1] == 'x' || s[p + 1] == 'X')) {
if ((radix == 0 || radix == 16) && length - p >= 2 && data[p] == '0' && (data[p + 1] == 'x' || data[p + 1] == 'X')) {
radix = 16;
p += 2;
} else if (radix == 0) {
if (p < length && s[p] == '0')
if (p < length && data[p] == '0')
radix = 8;
else
radix = 10;
@@ -227,7 +228,7 @@ static double parseInt(const UString& s, int radix)
bool sawDigit = false;
double number = 0;
while (p < length) {
int digit = parseDigit(s[p], radix);
int digit = parseDigit(data[p], radix);
if (digit == -1)
break;
sawDigit = true;
@@ -254,14 +255,15 @@ static double parseFloat(const UString& s)
// Check for 0x prefix here, because toDouble allows it, but we must treat it as 0.
// Need to skip any whitespace and then one + or - sign.
int length = s.size();
const UChar* data = s.data();
int p = 0;
while (p < length && isStrWhiteSpace(s[p]))
while (p < length && isStrWhiteSpace(data[p]))
++p;

if (p < length && (s[p] == '+' || s[p] == '-'))
if (p < length && (data[p] == '+' || data[p] == '-'))
++p;

if (length - p >= 2 && s[p] == '0' && (s[p + 1] == 'x' || s[p + 1] == 'X'))
if (length - p >= 2 && data[p] == '0' && (data[p + 1] == 'x' || data[p + 1] == 'X'))
return 0;

return s.toDouble(true /*tolerant*/, false /* NaN for empty string */);

0 comments on commit f1759f1

Please sign in to comment.