Changeset 184865 in webkit for trunk/Source/JavaScriptCore


Ignore:
Timestamp:
May 26, 2015, 11:54:01 AM (10 years ago)
Author:
[email protected]
Message:

String.prototype.charAt() should use StringView.
<https://webkit.org/b/145352>

Reviewed by Darin Adler.

Remove the jsSingleCharacterSubstring() function since it's actually completely
counter-productive: it could create a single-character string that would retain
a much larger string for the duration of its lifetime.

This made sense before StringImpl learned to put its characters at the tail end
of its own allocation. Now that it does, it's far better to just create a new
single-character StringImpl.

With that out of the way, we can make String.prototype.charAt() use StringView
to avoid reifying substring JSStrings (and avoid some ref churn too.)

  • runtime/JSString.cpp:

(JSC::JSRopeString::getIndexSlowCase):

  • runtime/JSString.h:

(JSC::JSString::getIndex):
(JSC::jsSingleCharacterSubstring): Deleted.

  • runtime/StringPrototype.cpp:

(JSC::stringProtoFuncCharAt):
(JSC::stringProtoFuncSplit):

Location:
trunk/Source/JavaScriptCore
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/ChangeLog

    r184863 r184865  
     12015-05-26  Andreas Kling  <[email protected]>
     2
     3        String.prototype.charAt() should use StringView.
     4        <https://webkit.org/b/145352>
     5
     6        Reviewed by Darin Adler.
     7
     8        Remove the jsSingleCharacterSubstring() function since it's actually completely
     9        counter-productive: it could create a single-character string that would retain
     10        a much larger string for the duration of its lifetime.
     11
     12        This made sense before StringImpl learned to put its characters at the tail end
     13        of its own allocation. Now that it does, it's far better to just create a new
     14        single-character StringImpl.
     15
     16        With that out of the way, we can make String.prototype.charAt() use StringView
     17        to avoid reifying substring JSStrings (and avoid some ref churn too.)
     18
     19        * runtime/JSString.cpp:
     20        (JSC::JSRopeString::getIndexSlowCase):
     21        * runtime/JSString.h:
     22        (JSC::JSString::getIndex):
     23        (JSC::jsSingleCharacterSubstring): Deleted.
     24        * runtime/StringPrototype.cpp:
     25        (JSC::stringProtoFuncCharAt):
     26        (JSC::stringProtoFuncSplit):
     27
    1282015-05-26  Yusuke Suzuki  <[email protected]>
    229
  • trunk/Source/JavaScriptCore/runtime/JSString.cpp

    r184612 r184865  
    375375    ASSERT(!isRope());
    376376    RELEASE_ASSERT(i < m_value.length());
    377     return jsSingleCharacterSubstring(exec, m_value, i);
     377    return jsSingleCharacterString(exec, m_value[i]);
    378378}
    379379
  • trunk/Source/JavaScriptCore/runtime/JSString.h

    r184612 r184865  
    4646JSString* jsSingleCharacterString(VM*, UChar);
    4747JSString* jsSingleCharacterString(ExecState*, UChar);
    48 JSString* jsSingleCharacterSubstring(ExecState*, const String&, unsigned offset);
    4948JSString* jsSubstring(VM*, const String&, unsigned offset, unsigned length);
    5049JSString* jsSubstring(ExecState*, const String&, unsigned offset, unsigned length);
     
    445444}
    446445
    447 ALWAYS_INLINE JSString* jsSingleCharacterSubstring(ExecState* exec, const String& s, unsigned offset)
    448 {
    449     VM* vm = &exec->vm();
    450     ASSERT(offset < static_cast<unsigned>(s.length()));
    451     UChar c = s.characterAt(offset);
    452     if (c <= maxSingleCharacterString)
    453         return vm->smallStrings.singleCharacterString(c);
    454     return JSString::create(*vm, StringImpl::createSubstringSharingImpl(s.impl(), offset, 1));
    455 }
    456 
    457446inline JSString* jsNontrivialString(VM* vm, const String& s)
    458447{
     
    508497        return static_cast<JSRopeString*>(this)->getIndexSlowCase(exec, i);
    509498    ASSERT(i < m_value.length());
    510     return jsSingleCharacterSubstring(exec, m_value, i);
     499    return jsSingleCharacterString(exec, m_value[i]);
    511500}
    512501
  • trunk/Source/JavaScriptCore/runtime/StringPrototype.cpp

    r184346 r184865  
    786786    if (!checkObjectCoercible(thisValue))
    787787        return throwVMTypeError(exec);
    788     String s = thisValue.toString(exec)->value(exec);
    789     unsigned len = s.length();
     788    StringView string = thisValue.toString(exec)->view(exec);
    790789    JSValue a0 = exec->argument(0);
    791790    if (a0.isUInt32()) {
    792791        uint32_t i = a0.asUInt32();
    793         if (i < len)
    794             return JSValue::encode(jsSingleCharacterSubstring(exec, s, i));
     792        if (i < string.length())
     793            return JSValue::encode(jsSingleCharacterString(exec, string[i]));
    795794        return JSValue::encode(jsEmptyString(exec));
    796795    }
    797796    double dpos = a0.toInteger(exec);
    798     if (dpos >= 0 && dpos < len)
    799         return JSValue::encode(jsSingleCharacterSubstring(exec, s, static_cast<unsigned>(dpos)));
     797    if (dpos >= 0 && dpos < string.length())
     798        return JSValue::encode(jsSingleCharacterString(exec, string[static_cast<unsigned>(dpos)]));
    800799    return JSValue::encode(jsEmptyString(exec));
    801800}
     
    12471246
    12481247            do {
    1249                 result->putDirectIndex(exec, position, jsSingleCharacterSubstring(exec, input, position));
     1248                result->putDirectIndex(exec, position, jsSingleCharacterString(exec, input[position]));
    12501249            } while (++position < limit);
    12511250
Note: See TracChangeset for help on using the changeset viewer.