Ignore:
Timestamp:
May 24, 2014, 8:50:43 PM (11 years ago)
Author:
[email protected]
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):

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.