Changeset 115290 in webkit


Ignore:
Timestamp:
Apr 25, 2012, 10:27:52 PM (13 years ago)
Author:
benjamin@webkit.org
Message:

Add a version of StringImpl::find() without offset
https://bugs.webkit.org/show_bug.cgi?id=83968

Source/JavaScriptCore:

Reviewed by Sam Weinig.

Add support for the new StringImpl::find() to UString.

Change stringProtoFuncIndexOf() to specifically take advatage of the feature.
This gives a 12% gains on a distribution of strings between 30 and 100 characters.

  • runtime/StringPrototype.cpp:

(JSC::substituteBackreferences):
(JSC::stringProtoFuncIndexOf):

  • runtime/UString.h:

(UString):
(JSC::UString::find):

Source/WebCore:

Reviewed by Sam Weinig.

Remove the zero offset of the find() functions on strings.

  • html/parser/XSSAuditor.cpp:

(WebCore::XSSAuditor::init):

  • platform/network/ResourceResponseBase.cpp:

(WebCore::trimToNextSeparator):
(WebCore::parseCacheHeader):

Source/WebKit2:

Update the symbols files.

  • win/WebKit2.def:
  • win/WebKit2CFLite.def:

Source/WTF:

Reviewed by Sam Weinig.

This patch add a version of StringImpl::find() without offset, Instead of using the default
value for the index.

By not having the index, we can skip a couple of branches and a few instructions. This gives
significant gains when the strings are short-ish.

The case of empty string is moved below (matchLength == 1) since it is a less common operation.

  • wtf/text/StringImpl.cpp:

(WTF::StringImpl::find):
(WTF):

  • wtf/text/StringImpl.h:

(StringImpl):

  • wtf/text/WTFString.h:

(String):
(WTF::String::find):

Location:
trunk/Source
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/ChangeLog

    r115288 r115290  
     12012-04-25  Benjamin Poulain  <benjamin@webkit.org>
     2
     3        Add a version of StringImpl::find() without offset
     4        https://bugs.webkit.org/show_bug.cgi?id=83968
     5
     6        Reviewed by Sam Weinig.
     7
     8        Add support for the new StringImpl::find() to UString.
     9
     10        Change stringProtoFuncIndexOf() to specifically take advatage of the feature.
     11        This gives a 12% gains on a distribution of strings between 30 and 100 characters.
     12
     13        * runtime/StringPrototype.cpp:
     14        (JSC::substituteBackreferences):
     15        (JSC::stringProtoFuncIndexOf):
     16        * runtime/UString.h:
     17        (UString):
     18        (JSC::UString::find):
     19
    1202012-04-25  Mark Hahnenberg  <mhahnenberg@apple.com>
    221
  • trunk/Source/JavaScriptCore/runtime/StringPrototype.cpp

    r115132 r115290  
    241241static inline UString substituteBackreferences(const UString& replacement, const UString& source, const int* ovector, RegExp* reg)
    242242{
    243     size_t i = replacement.find('$', 0);
     243    size_t i = replacement.find('$');
    244244    if (UNLIKELY(i != notFound)) {
    245245        if (replacement.is8Bit() && source.is8Bit())
     
    755755        return throwVMTypeError(exec);
    756756    UString s = thisValue.toString(exec)->value(exec);
    757     int len = s.length();
    758757
    759758    JSValue a0 = exec->argument(0);
    760759    JSValue a1 = exec->argument(1);
    761760    UString u2 = a0.toString(exec)->value(exec);
    762     int pos;
     761
     762    size_t result;
    763763    if (a1.isUndefined())
    764         pos = 0;
    765     else if (a1.isUInt32())
    766         pos = min<uint32_t>(a1.asUInt32(), len);
     764        result = s.find(u2);
    767765    else {
    768         double dpos = a1.toInteger(exec);
    769         if (dpos < 0)
    770             dpos = 0;
    771         else if (dpos > len)
    772             dpos = len;
    773         pos = static_cast<int>(dpos);
    774     }
    775 
    776     size_t result = s.find(u2, pos);
     766        unsigned pos;
     767        int len = s.length();
     768        if (a1.isUInt32())
     769            pos = min<uint32_t>(a1.asUInt32(), len);
     770        else {
     771            double dpos = a1.toInteger(exec);
     772            if (dpos < 0)
     773                dpos = 0;
     774            else if (dpos > len)
     775                dpos = len;
     776            pos = static_cast<unsigned>(dpos);
     777        }
     778        result = s.find(u2, pos);
     779    }
     780
    777781    if (result == notFound)
    778782        return JSValue::encode(jsNumber(-1));
  • trunk/Source/JavaScriptCore/runtime/UString.h

    r106257 r115290  
    122122    size_t find(UChar c, unsigned start = 0) const
    123123        { return m_impl ? m_impl->find(c, start) : notFound; }
    124     size_t find(const UString& str, unsigned start = 0) const
     124
     125    size_t find(const UString& str) const
     126        { return m_impl ? m_impl->find(str.impl()) : notFound; }
     127    size_t find(const UString& str, unsigned start) const
    125128        { return m_impl ? m_impl->find(str.impl(), start) : notFound; }
     129
    126130    size_t find(const LChar* str, unsigned start = 0) const
    127131        { return m_impl ? m_impl->find(str, start) : notFound; }
  • trunk/Source/WTF/ChangeLog

    r115272 r115290  
     12012-04-25  Benjamin Poulain  <benjamin@webkit.org>
     2
     3        Add a version of StringImpl::find() without offset
     4        https://bugs.webkit.org/show_bug.cgi?id=83968
     5
     6        Reviewed by Sam Weinig.
     7
     8        This patch add a version of StringImpl::find() without offset, Instead of using the default
     9        value for the index.
     10
     11        By not having the index, we can skip a couple of branches and a few instructions. This gives
     12        significant gains when the strings are short-ish.
     13
     14        The case of empty string is moved below (matchLength == 1) since it is a less common operation.
     15
     16        * wtf/text/StringImpl.cpp:
     17        (WTF::StringImpl::find):
     18        (WTF):
     19        * wtf/text/StringImpl.h:
     20        (StringImpl):
     21        * wtf/text/WTFString.h:
     22        (String):
     23        (WTF::String::find):
     24
    1252012-04-25  Darin Adler  <darin@apple.com>
    226
  • trunk/Source/WTF/wtf/text/StringImpl.cpp

    r115132 r115290  
    895895}
    896896
     897size_t StringImpl::find(StringImpl* matchString)
     898{
     899    // Check for null string to match against
     900    if (UNLIKELY(!matchString))
     901        return notFound;
     902    unsigned matchLength = matchString->length();
     903
     904    // Optimization 1: fast case for strings of length 1.
     905    if (matchLength == 1) {
     906        if (is8Bit()) {
     907            if (matchString->is8Bit())
     908                return WTF::find(characters8(), length(), matchString->characters8()[0]);
     909            return WTF::find(characters8(), length(), matchString->characters16()[0]);
     910        }
     911        if (matchString->is8Bit())
     912            return WTF::find(characters16(), length(), matchString->characters8()[0]);
     913        return WTF::find(characters16(), length(), matchString->characters16()[0]);
     914    }
     915
     916    // Check matchLength is in range.
     917    if (matchLength > length())
     918        return notFound;
     919
     920    // Check for empty string to match against
     921    if (UNLIKELY(!matchLength))
     922        return 0;
     923
     924    if (is8Bit() && matchString->is8Bit())
     925        return findInner(characters8(), matchString->characters8(), 0, length(), matchLength);
     926
     927    return findInner(characters(), matchString->characters(), 0, length(), matchLength);
     928}
     929
    897930size_t StringImpl::find(StringImpl* matchString, unsigned index)
    898931{
    899932    // Check for null or empty string to match against
    900     if (!matchString)
    901         return notFound;
     933    if (UNLIKELY(!matchString))
     934        return notFound;
     935
    902936    unsigned matchLength = matchString->length();
    903     if (!matchLength)
    904         return min(index, length());
    905937
    906938    // Optimization 1: fast case for strings of length 1.
     
    916948    }
    917949
     950    if (UNLIKELY(!matchLength))
     951        return min(index, length());
     952
    918953    // Check index & matchLength are in range.
    919954    if (index > length())
     
    927962
    928963    return findInner(characters() + index, matchString->characters(), index, searchLength, matchLength);
    929 
    930964}
    931965
  • trunk/Source/WTF/wtf/text/StringImpl.h

    r115132 r115290  
    488488    size_t find(const LChar*, unsigned index = 0);
    489489    ALWAYS_INLINE size_t find(const char* s, unsigned index = 0) { return find(reinterpret_cast<const LChar*>(s), index); };
    490     WTF_EXPORT_PRIVATE size_t find(StringImpl*, unsigned index = 0);
     490    WTF_EXPORT_PRIVATE size_t find(StringImpl*);
     491    WTF_EXPORT_PRIVATE size_t find(StringImpl*, unsigned index);
    491492    size_t findIgnoringCase(const LChar*, unsigned index = 0);
    492493    ALWAYS_INLINE size_t findIgnoringCase(const char* s, unsigned index = 0) { return findIgnoringCase(reinterpret_cast<const LChar*>(s), index); };
  • trunk/Source/WTF/wtf/text/WTFString.h

    r114071 r115290  
    206206    size_t find(UChar c, unsigned start = 0) const
    207207        { return m_impl ? m_impl->find(c, start) : notFound; }
    208     size_t find(const String& str, unsigned start = 0) const
     208
     209    size_t find(const String& str) const
     210        { return m_impl ? m_impl->find(str.impl()) : notFound; }
     211    size_t find(const String& str, unsigned start) const
    209212        { return m_impl ? m_impl->find(str.impl(), start) : notFound; }
     213
    210214    size_t find(CharacterMatchFunctionPtr matchFunction, unsigned start = 0) const
    211215        { return m_impl ? m_impl->find(matchFunction, start) : notFound; }
  • trunk/Source/WebCore/ChangeLog

    r115288 r115290  
     12012-04-25  Benjamin Poulain  <benjamin@webkit.org>
     2
     3        Add a version of StringImpl::find() without offset
     4        https://bugs.webkit.org/show_bug.cgi?id=83968
     5
     6        Reviewed by Sam Weinig.
     7
     8        Remove the zero offset of the find() functions on strings.
     9
     10        * html/parser/XSSAuditor.cpp:
     11        (WebCore::XSSAuditor::init):
     12        * platform/network/ResourceResponseBase.cpp:
     13        (WebCore::trimToNextSeparator):
     14        (WebCore::parseCacheHeader):
     15
    1162012-04-25  Mark Hahnenberg  <mhahnenberg@apple.com>
    217
  • trunk/Source/WebCore/html/parser/XSSAuditor.cpp

    r113286 r115290  
    212212    TextResourceDecoder* decoder = m_parser->document()->decoder();
    213213    m_decodedURL = fullyDecodeString(url.string(), decoder);
    214     if (m_decodedURL.find(isRequiredForInjection, 0) == notFound)
     214    if (m_decodedURL.find(isRequiredForInjection) == notFound)
    215215        m_decodedURL = String();
    216216
     
    224224            if (!httpBodyAsString.isEmpty()) {
    225225                m_decodedHTTPBody = fullyDecodeString(httpBodyAsString, decoder);
    226                 if (m_decodedHTTPBody.find(isRequiredForInjection, 0) == notFound)
     226                if (m_decodedHTTPBody.find(isRequiredForInjection) == notFound)
    227227                    m_decodedHTTPBody = String();
    228228                if (m_decodedHTTPBody.length() >= miniumLengthForSuffixTree)
  • trunk/Source/WebCore/platform/network/ResourceResponseBase.cpp

    r114165 r115290  
    631631static inline String trimToNextSeparator(const String& str)
    632632{
    633     return str.substring(0, str.find(isCacheHeaderSeparator, 0));
     633    return str.substring(0, str.find(isCacheHeaderSeparator));
    634634}
    635635
     
    667667            } else {
    668668                // The value is a token until the next comma
    669                 size_t nextCommaPosition2 = value.find(',', 0);
     669                size_t nextCommaPosition2 = value.find(',');
    670670                if (nextCommaPosition2 != notFound) {
    671671                    // The value is delimited by the next comma
  • trunk/Source/WebKit2/ChangeLog

    r115288 r115290  
     12012-04-25  Benjamin Poulain  <benjamin@webkit.org>
     2
     3        Add a version of StringImpl::find() without offset
     4        https://bugs.webkit.org/show_bug.cgi?id=83968
     5
     6        Update the symbols files.
     7
     8        * win/WebKit2.def:
     9        * win/WebKit2CFLite.def:
     10
    1112012-04-25  Mark Hahnenberg  <mhahnenberg@apple.com>
    212
  • trunk/Source/WebKit2/win/WebKit2.def

    r114877 r115290  
    169169        ?externalRepresentation@WebCore@@YA?AVString@WTF@@PAVElement@1@I@Z
    170170        ?find@StringImpl@WTF@@QAEIPAV12@I@Z
     171        ?find@StringImpl@WTF@@QAEIPAV12@@Z
    171172        ?frameDestroyed@FrameDestructionObserver@WebCore@@UAEXXZ
    172173        ?fromUTF8WithLatin1Fallback@String@WTF@@SA?AV12@PBEI@Z
  • trunk/Source/WebKit2/win/WebKit2CFLite.def

    r114877 r115290  
    162162        ?externalRepresentation@WebCore@@YA?AVString@WTF@@PAVElement@1@I@Z
    163163        ?find@StringImpl@WTF@@QAEIPAV12@I@Z
     164        ?find@StringImpl@WTF@@QAEIPAV12@@Z
    164165        ?frameDestroyed@FrameDestructionObserver@WebCore@@UAEXXZ
    165166        ?fromUTF8WithLatin1Fallback@String@WTF@@SA?AV12@PBEI@Z
Note: See TracChangeset for help on using the changeset viewer.