Ignore:
Timestamp:
Jul 4, 2009, 7:21:30 AM (16 years ago)
Author:
[email protected]
Message:

2009-07-03 Yong Li <[email protected]>

Reviewed by Maciej Stachowiak (and revised slightly)

RegExp::match to be optimized
https://bugs.webkit.org/show_bug.cgi?id=26957

Allow regexp matching to use Vectors with inline capacity instead of
allocating a new ovector buffer every time.


~5% speedup on SunSpider string-unpack-code test, 0.3% on SunSpider overall.

  • runtime/RegExp.cpp: (JSC::RegExp::match):
  • runtime/RegExp.h:
  • runtime/RegExpConstructor.cpp: (JSC::RegExpConstructorPrivate::RegExpConstructorPrivate): (JSC::RegExpConstructorPrivate::lastOvector): (JSC::RegExpConstructorPrivate::tempOvector): (JSC::RegExpConstructorPrivate::changeLastOvector): (JSC::RegExpConstructor::performMatch): (JSC::RegExpMatchesArray::RegExpMatchesArray): (JSC::RegExpMatchesArray::fillArrayInstance): (JSC::RegExpConstructor::getBackref): (JSC::RegExpConstructor::getLastParen): (JSC::RegExpConstructor::getLeftContext): (JSC::RegExpConstructor::getRightContext):
  • runtime/StringPrototype.cpp: (JSC::stringProtoFuncSplit):
File:
1 edited

Legend:

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

    r45269 r45545  
    22 *  Copyright (C) 1999-2000 Harri Porten ([email protected])
    33 *  Copyright (C) 2003, 2007, 2008 Apple Inc. All Rights Reserved.
     4 *  Copyright (C) 2009 Torch Mobile, Inc.
    45 *
    56 *  This library is free software; you can redistribute it and/or
     
    9495        : lastNumSubPatterns(0)
    9596        , multiline(false)
     97        , lastOvectorIndex(0)
    9698    {
    9799    }
     100
     101    const Vector<int, 32>& lastOvector() const { return ovector[lastOvectorIndex]; }
     102    Vector<int, 32>& lastOvector() { return ovector[lastOvectorIndex]; }
     103    Vector<int, 32>& tempOvector() { return ovector[lastOvectorIndex ? 0 : 1]; }
     104    void changeLastOvector() { lastOvectorIndex = lastOvectorIndex ? 0 : 1; }
    98105
    99106    UString input;
    100107    UString lastInput;
    101     OwnArrayPtr<int> lastOvector;
    102     unsigned lastNumSubPatterns : 31;
     108    Vector<int, 32> ovector[2];
     109    unsigned lastNumSubPatterns : 30;
    103110    bool multiline : 1;
     111    unsigned lastOvectorIndex : 1;
    104112};
    105113
     
    122130void RegExpConstructor::performMatch(RegExp* r, const UString& s, int startOffset, int& position, int& length, int** ovector)
    123131{
    124     OwnArrayPtr<int> tmpOvector;
    125     position = r->match(s, startOffset, &tmpOvector);
     132    position = r->match(s, startOffset, &d->tempOvector());
    126133
    127134    if (ovector)
    128         *ovector = tmpOvector.get();
     135        *ovector = d->tempOvector().data();
    129136
    130137    if (position != -1) {
    131         ASSERT(tmpOvector);
    132 
    133         length = tmpOvector[1] - tmpOvector[0];
     138        ASSERT(!d->tempOvector().isEmpty());
     139
     140        length = d->tempOvector()[1] - d->tempOvector()[0];
    134141
    135142        d->input = s;
    136143        d->lastInput = s;
    137         d->lastOvector.set(tmpOvector.release());
     144        d->changeLastOvector();
    138145        d->lastNumSubPatterns = r->numSubpatterns();
    139146    }
     
    148155    d->lastNumSubPatterns = data->lastNumSubPatterns;
    149156    unsigned offsetVectorSize = (data->lastNumSubPatterns + 1) * 2; // only copying the result part of the vector
    150     d->lastOvector.set(new int[offsetVectorSize]);
    151     memcpy(d->lastOvector.get(), data->lastOvector.get(), offsetVectorSize * sizeof(int));
     157    d->lastOvector().resize(offsetVectorSize);
     158    memcpy(d->lastOvector().data(), data->lastOvector().data(), offsetVectorSize * sizeof(int));
    152159    // d->multiline is not needed, and remains uninitialized
    153160
     
    168175
    169176    for (unsigned i = 0; i <= lastNumSubpatterns; ++i) {
    170         int start = d->lastOvector[2 * i];
     177        int start = d->lastOvector()[2 * i];
    171178        if (start >= 0)
    172             JSArray::put(exec, i, jsSubstring(exec, d->lastInput, start, d->lastOvector[2 * i + 1] - start));
     179            JSArray::put(exec, i, jsSubstring(exec, d->lastInput, start, d->lastOvector()[2 * i + 1] - start));
    173180    }
    174181
    175182    PutPropertySlot slot;
    176     JSArray::put(exec, exec->propertyNames().index, jsNumber(exec, d->lastOvector[0]), slot);
     183    JSArray::put(exec, exec->propertyNames().index, jsNumber(exec, d->lastOvector()[0]), slot);
    177184    JSArray::put(exec, exec->propertyNames().input, jsString(exec, d->input), slot);
    178185
     
    188195JSValue RegExpConstructor::getBackref(ExecState* exec, unsigned i) const
    189196{
    190     if (d->lastOvector && i <= d->lastNumSubPatterns) {
    191         int start = d->lastOvector[2 * i];
     197    if (!d->lastOvector().isEmpty() && i <= d->lastNumSubPatterns) {
     198        int start = d->lastOvector()[2 * i];
    192199        if (start >= 0)
    193             return jsSubstring(exec, d->lastInput, start, d->lastOvector[2 * i + 1] - start);
     200            return jsSubstring(exec, d->lastInput, start, d->lastOvector()[2 * i + 1] - start);
    194201    }
    195202    return jsEmptyString(exec);
     
    201208    if (i > 0) {
    202209        ASSERT(d->lastOvector);
    203         int start = d->lastOvector[2 * i];
     210        int start = d->lastOvector()[2 * i];
    204211        if (start >= 0)
    205             return jsSubstring(exec, d->lastInput, start, d->lastOvector[2 * i + 1] - start);
     212            return jsSubstring(exec, d->lastInput, start, d->lastOvector()[2 * i + 1] - start);
    206213    }
    207214    return jsEmptyString(exec);
     
    210217JSValue RegExpConstructor::getLeftContext(ExecState* exec) const
    211218{
    212     if (d->lastOvector)
    213         return jsSubstring(exec, d->lastInput, 0, d->lastOvector[0]);
     219    if (!d->lastOvector().isEmpty())
     220        return jsSubstring(exec, d->lastInput, 0, d->lastOvector()[0]);
    214221    return jsEmptyString(exec);
    215222}
     
    217224JSValue RegExpConstructor::getRightContext(ExecState* exec) const
    218225{
    219     if (d->lastOvector)
    220         return jsSubstring(exec, d->lastInput, d->lastOvector[1], d->lastInput.size() - d->lastOvector[1]);
     226    if (!d->lastOvector().isEmpty())
     227        return jsSubstring(exec, d->lastInput, d->lastOvector()[1], d->lastInput.size() - d->lastOvector()[1]);
    221228    return jsEmptyString(exec);
    222229}
Note: See TracChangeset for help on using the changeset viewer.