Changeset 58964 in webkit for trunk/JavaScriptCore/runtime


Ignore:
Timestamp:
May 7, 2010, 12:30:31 PM (15 years ago)
Author:
[email protected]
Message:

Added a fast path for number-to-character conversion via
Number.prototype.toString base 36.

Reviewed by Oliver Hunt.

0.7% speedup on SunSpider.

  • runtime/NumberPrototype.cpp:

(JSC::numberProtoFuncToString): Made radix interpretation a little more
efficient by keeping it in int space. Turned "const char" into
"static const char" just in case. Added a fast path for base 36
conversion of something that will turn into a character.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/runtime/NumberPrototype.cpp

    r54789 r58964  
    144144        return throwError(exec, TypeError);
    145145
    146     double radixAsDouble = args.at(0).toInteger(exec); // nan -> 0
    147     if (radixAsDouble == 10 || args.at(0).isUndefined())
     146    JSValue radixValue = args.at(0);
     147    int radix;
     148    if (radixValue.isInt32())
     149        radix = radixValue.asInt32();
     150    else if (radixValue.isUndefined())
     151        radix = 10;
     152    else
     153        radix = static_cast<int>(radixValue.toInteger(exec)); // nan -> 0
     154
     155    if (radix == 10)
    148156        return jsString(exec, v.toString(exec));
    149157
    150     if (radixAsDouble < 2 || radixAsDouble > 36)
     158    static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
     159
     160    // Fast path for number to character conversion.
     161    if (radix == 36) {
     162        if (v.isInt32()) {
     163            int x = v.asInt32();
     164            if (x < 36) {
     165                JSGlobalData* globalData = &exec->globalData();
     166                return globalData->smallStrings.singleCharacterString(globalData, digits[x]);
     167            }
     168        }
     169    }
     170
     171    if (radix < 2 || radix > 36)
    151172        return throwError(exec, RangeError, "toString() radix argument must be between 2 and 36");
    152173
    153     int radix = static_cast<int>(radixAsDouble);
    154     const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
    155174    // INT_MAX results in 1024 characters left of the dot with radix 2
    156175    // give the same space on the right side. safety checks are in place
Note: See TracChangeset for help on using the changeset viewer.