Changeset 169316 in webkit


Ignore:
Timestamp:
May 24, 2014 8:50:43 PM (10 years ago)
Author:
akling@apple.com
Message:

Object.prototype.toString() should use cached strings for null/undefined.
<https://webkit.org/b/133261>

Normally, when calling Object.prototype.toString() on a regular object,
we'd cache the result of the stringification on the object's structure,
making repeated calls fast.

For null and undefined, we were not as smart. We'd instead construct a
new string with either "[object Null]" or "[object Undefined]" each time.

This was exposed by Dromaeo's JS library tests, where some prototype.js
subtests generate millions of strings this way.

This patch adds two VM-permanent cached strings to the SmallStrings.
Looks like ~10% speed-up on Dromaeo/jslib-traverse-prototype.html

Reviewed by Darin Adler.

  • runtime/ObjectPrototype.cpp:

(JSC::objectProtoFuncToString):

  • runtime/SmallStrings.cpp:

(JSC::SmallStrings::SmallStrings):
(JSC::SmallStrings::initializeCommonStrings):
(JSC::SmallStrings::visitStrongReferences):

  • runtime/SmallStrings.h:

(JSC::SmallStrings::nullObjectString):
(JSC::SmallStrings::undefinedObjectString):

Location:
trunk/Source/JavaScriptCore
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/ChangeLog

    r169285 r169316  
     12014-05-24  Andreas Kling  <akling@apple.com>
     2
     3        Object.prototype.toString() should use cached strings for null/undefined.
     4        <https://webkit.org/b/133261>
     5
     6        Normally, when calling Object.prototype.toString() on a regular object,
     7        we'd cache the result of the stringification on the object's structure,
     8        making repeated calls fast.
     9
     10        For null and undefined, we were not as smart. We'd instead construct a
     11        new string with either "[object Null]" or "[object Undefined]" each time.
     12
     13        This was exposed by Dromaeo's JS library tests, where some prototype.js
     14        subtests generate millions of strings this way.
     15
     16        This patch adds two VM-permanent cached strings to the SmallStrings.
     17        Looks like ~10% speed-up on Dromaeo/jslib-traverse-prototype.html
     18
     19        Reviewed by Darin Adler.
     20
     21        * runtime/ObjectPrototype.cpp:
     22        (JSC::objectProtoFuncToString):
     23        * runtime/SmallStrings.cpp:
     24        (JSC::SmallStrings::SmallStrings):
     25        (JSC::SmallStrings::initializeCommonStrings):
     26        (JSC::SmallStrings::visitStrongReferences):
     27        * runtime/SmallStrings.h:
     28        (JSC::SmallStrings::nullObjectString):
     29        (JSC::SmallStrings::undefinedObjectString):
     30
    1312014-05-23  Mark Hahnenberg  <mhahnenberg@apple.com>
    232
  • trunk/Source/JavaScriptCore/runtime/ObjectPrototype.cpp

    r168549 r169316  
    216216    JSValue thisValue = exec->thisValue().toThis(exec, StrictMode);
    217217    if (thisValue.isUndefinedOrNull())
    218         return JSValue::encode(jsNontrivialString(&vm, String(thisValue.isUndefined() ? ASCIILiteral("[object Undefined]") : ASCIILiteral("[object Null]"))));
     218        return JSValue::encode(thisValue.isUndefined() ? vm.smallStrings.undefinedObjectString() : vm.smallStrings.nullObjectString());
    219219    JSObject* thisObject = thisValue.toObject(exec);
    220220
  • trunk/Source/JavaScriptCore/runtime/SmallStrings.cpp

    r165982 r169316  
    6868    JSC_COMMON_STRINGS_EACH_NAME(JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE)
    6969#undef JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE
     70    , m_nullObjectString(nullptr)
     71    , m_undefinedObjectString(nullptr)
    7072{
    7173    COMPILE_ASSERT(singleCharacterStringCount == sizeof(m_singleCharacterStrings) / sizeof(m_singleCharacterStrings[0]), IsNumCharactersConstInSyncWithClassUsage);
     
    8385    JSC_COMMON_STRINGS_EACH_NAME(JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE)
    8486#undef JSC_COMMON_STRINGS_ATTRIBUTE_INITIALIZE
     87    initialize(&vm, m_nullObjectString, "[object Null]");
     88    initialize(&vm, m_undefinedObjectString, "[object Undefined]");
    8589}
    8690
     
    9397    JSC_COMMON_STRINGS_EACH_NAME(JSC_COMMON_STRINGS_ATTRIBUTE_VISIT)
    9498#undef JSC_COMMON_STRINGS_ATTRIBUTE_VISIT
     99    visitor.appendUnbarrieredPointer(&m_nullObjectString);
     100    visitor.appendUnbarrieredPointer(&m_undefinedObjectString);
    95101}
    96102
  • trunk/Source/JavaScriptCore/runtime/SmallStrings.h

    r160344 r169316  
    8787#undef JSC_COMMON_STRINGS_ACCESSOR_DEFINITION
    8888
     89        JSString* nullObjectString() const { return m_nullObjectString; }
     90        JSString* undefinedObjectString() const { return m_undefinedObjectString; }
     91
    8992    private:
    9093        static const unsigned singleCharacterStringCount = maxSingleCharacterString + 1;
     
    99102        JSC_COMMON_STRINGS_EACH_NAME(JSC_COMMON_STRINGS_ATTRIBUTE_DECLARATION)
    100103#undef JSC_COMMON_STRINGS_ATTRIBUTE_DECLARATION
     104        JSString* m_nullObjectString;
     105        JSString* m_undefinedObjectString;
    101106        JSString* m_singleCharacterStrings[singleCharacterStringCount];
    102107        OwnPtr<SmallStringsStorage> m_storage;
Note: See TracChangeset for help on using the changeset viewer.