Ignore:
Timestamp:
Jul 13, 2010, 5:29:32 PM (15 years ago)
Author:
[email protected]
Message:

2010-07-13 Andreas Kling <[email protected]>

Reviewed by Darin Adler.

Avoid slow-path for put() in Array.splice()
https://bugs.webkit.org/show_bug.cgi?id=41920

Defer creation of the returned array until its final size is known
to avoid growing it while adding elements.

  • runtime/JSArray.cpp: (JSC::JSArray::JSArray): Add two modes of creation, CreateInitialized (old) and CreateCompact (which should only be used when constructing arrays whose size and contents are known at the time of creation.) (JSC::JSArray::setLength): Skip first consistency check if in CreateCompact initialization mode. (Only applies to non-empty arrays.) (JSC::JSArray::checkConsistency): Build fix (JSValue::type() is gone)
  • runtime/JSArray.h: (JSC::JSArray::uncheckedSetIndex): Added for fast initialization of compact arrays. Does no bounds or other sanity checking.
  • runtime/ArrayPrototype.cpp: (JSC::arrayProtoFuncSplice): Optimized creation of the returned JSArray.
  • runtime/ArrayConstructor.cpp: (JSC::constructArrayWithSizeQuirk): Pass CreateInitialized to ctor.
  • runtime/JSGlobalObject.h: (JSC::constructEmptyArray): Pass CreateInitialized to ctor.
  • runtime/RegExpConstructor.cpp: (JSC::RegExpMatchesArray::RegExpMatchesArray): Pass CreateInitialized to ctor.
File:
1 edited

Legend:

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

    r62677 r63268  
    3333#include <wtf/OwnPtr.h>
    3434#include <Operations.h>
    35 
    36 #define CHECK_ARRAY_CONSISTENCY 0
    3735
    3836using namespace std;
     
    142140}
    143141
    144 JSArray::JSArray(NonNullPassRefPtr<Structure> structure, unsigned initialLength)
     142JSArray::JSArray(NonNullPassRefPtr<Structure> structure, unsigned initialLength, ArrayCreationMode creationMode)
    145143    : JSObject(structure)
    146144{
    147     unsigned initialCapacity = min(initialLength, MIN_SPARSE_ARRAY_INDEX);
     145    unsigned initialCapacity;
     146    if (creationMode == CreateCompact)
     147        initialCapacity = initialLength;
     148    else
     149        initialCapacity = min(initialLength, MIN_SPARSE_ARRAY_INDEX);
    148150
    149151    m_storage = static_cast<ArrayStorage*>(fastMalloc(storageSize(initialCapacity)));
    150     m_storage->m_length = initialLength;
    151152    m_vectorLength = initialCapacity;
    152     m_storage->m_numValuesInVector = 0;
    153153    m_storage->m_sparseValueMap = 0;
    154154    m_storage->subclassData = 0;
    155155    m_storage->reportedMapCapacity = 0;
    156156
    157     JSValue* vector = m_storage->m_vector;
    158     for (size_t i = 0; i < initialCapacity; ++i)
    159         vector[i] = JSValue();
     157    if (creationMode == CreateCompact) {
     158#if CHECK_ARRAY_CONSISTENCY
     159        m_storage->m_inCompactInitialization = !!initialCapacity;
     160#endif
     161        m_storage->m_length = 0;
     162        m_storage->m_numValuesInVector = initialCapacity;
     163    } else {
     164#if CHECK_ARRAY_CONSISTENCY
     165        m_storage->m_inCompactInitialization = false;
     166#endif
     167        m_storage->m_length = initialLength;
     168        m_storage->m_numValuesInVector = 0;
     169        JSValue* vector = m_storage->m_vector;
     170        for (size_t i = 0; i < initialCapacity; ++i)
     171            vector[i] = JSValue();
     172    }
    160173
    161174    checkConsistency();
     
    176189    m_storage->subclassData = 0;
    177190    m_storage->reportedMapCapacity = 0;
     191#if CHECK_ARRAY_CONSISTENCY
     192    m_storage->m_inCompactInitialization = false;
     193#endif
    178194
    179195    size_t i = 0;
     
    525541void JSArray::setLength(unsigned newLength)
    526542{
    527     checkConsistency();
     543#if CHECK_ARRAY_CONSISTENCY
     544    if (!m_storage->m_inCompactInitialization)
     545        checkConsistency();
     546    else
     547        m_storage->m_inCompactInitialization = false;
     548#endif
    528549
    529550    ArrayStorage* storage = m_storage;
     
    10461067            ASSERT(i < m_storage->m_length);
    10471068            if (type != DestructorConsistencyCheck)
    1048                 value->type(); // Likely to crash if the object was deallocated.
     1069                value.isUndefined(); // Likely to crash if the object was deallocated.
    10491070            ++numValuesInVector;
    10501071        } else {
     
    10651086            ASSERT(it->second);
    10661087            if (type != DestructorConsistencyCheck)
    1067                 it->second->type(); // Likely to crash if the object was deallocated.
     1088                it->second.isUndefined(); // Likely to crash if the object was deallocated.
    10681089        }
    10691090    }
Note: See TracChangeset for help on using the changeset viewer.