Title: [184865] trunk/Source/_javascript_Core
Revision
184865
Author
akl...@apple.com
Date
2015-05-26 11:54:01 -0700 (Tue, 26 May 2015)

Log 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):

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (184864 => 184865)


--- trunk/Source/_javascript_Core/ChangeLog	2015-05-26 18:52:34 UTC (rev 184864)
+++ trunk/Source/_javascript_Core/ChangeLog	2015-05-26 18:54:01 UTC (rev 184865)
@@ -1,3 +1,30 @@
+2015-05-26  Andreas Kling  <akl...@apple.com>
+
+        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):
+
 2015-05-26  Yusuke Suzuki  <utatane....@gmail.com>
 
         [ES6] Implement Array.prototype.copyWithin

Modified: trunk/Source/_javascript_Core/runtime/JSString.cpp (184864 => 184865)


--- trunk/Source/_javascript_Core/runtime/JSString.cpp	2015-05-26 18:52:34 UTC (rev 184864)
+++ trunk/Source/_javascript_Core/runtime/JSString.cpp	2015-05-26 18:54:01 UTC (rev 184865)
@@ -374,7 +374,7 @@
         return jsEmptyString(exec);
     ASSERT(!isRope());
     RELEASE_ASSERT(i < m_value.length());
-    return jsSingleCharacterSubstring(exec, m_value, i);
+    return jsSingleCharacterString(exec, m_value[i]);
 }
 
 JSValue JSString::toPrimitive(ExecState*, PreferredPrimitiveType) const

Modified: trunk/Source/_javascript_Core/runtime/JSString.h (184864 => 184865)


--- trunk/Source/_javascript_Core/runtime/JSString.h	2015-05-26 18:52:34 UTC (rev 184864)
+++ trunk/Source/_javascript_Core/runtime/JSString.h	2015-05-26 18:54:01 UTC (rev 184865)
@@ -45,7 +45,6 @@
 
 JSString* jsSingleCharacterString(VM*, UChar);
 JSString* jsSingleCharacterString(ExecState*, UChar);
-JSString* jsSingleCharacterSubstring(ExecState*, const String&, unsigned offset);
 JSString* jsSubstring(VM*, const String&, unsigned offset, unsigned length);
 JSString* jsSubstring(ExecState*, const String&, unsigned offset, unsigned length);
 
@@ -444,16 +443,6 @@
     return JSString::create(*vm, String(&c, 1).impl());
 }
 
-ALWAYS_INLINE JSString* jsSingleCharacterSubstring(ExecState* exec, const String& s, unsigned offset)
-{
-    VM* vm = &exec->vm();
-    ASSERT(offset < static_cast<unsigned>(s.length()));
-    UChar c = s.characterAt(offset);
-    if (c <= maxSingleCharacterString)
-        return vm->smallStrings.singleCharacterString(c);
-    return JSString::create(*vm, StringImpl::createSubstringSharingImpl(s.impl(), offset, 1));
-}
-
 inline JSString* jsNontrivialString(VM* vm, const String& s)
 {
     ASSERT(s.length() > 1);
@@ -507,7 +496,7 @@
     if (isRope())
         return static_cast<JSRopeString*>(this)->getIndexSlowCase(exec, i);
     ASSERT(i < m_value.length());
-    return jsSingleCharacterSubstring(exec, m_value, i);
+    return jsSingleCharacterString(exec, m_value[i]);
 }
 
 inline JSString* jsString(VM* vm, const String& s)

Modified: trunk/Source/_javascript_Core/runtime/StringPrototype.cpp (184864 => 184865)


--- trunk/Source/_javascript_Core/runtime/StringPrototype.cpp	2015-05-26 18:52:34 UTC (rev 184864)
+++ trunk/Source/_javascript_Core/runtime/StringPrototype.cpp	2015-05-26 18:54:01 UTC (rev 184865)
@@ -785,18 +785,17 @@
     JSValue thisValue = exec->thisValue();
     if (!checkObjectCoercible(thisValue))
         return throwVMTypeError(exec);
-    String s = thisValue.toString(exec)->value(exec);
-    unsigned len = s.length();
+    StringView string = thisValue.toString(exec)->view(exec);
     JSValue a0 = exec->argument(0);
     if (a0.isUInt32()) {
         uint32_t i = a0.asUInt32();
-        if (i < len)
-            return JSValue::encode(jsSingleCharacterSubstring(exec, s, i));
+        if (i < string.length())
+            return JSValue::encode(jsSingleCharacterString(exec, string[i]));
         return JSValue::encode(jsEmptyString(exec));
     }
     double dpos = a0.toInteger(exec);
-    if (dpos >= 0 && dpos < len)
-        return JSValue::encode(jsSingleCharacterSubstring(exec, s, static_cast<unsigned>(dpos)));
+    if (dpos >= 0 && dpos < string.length())
+        return JSValue::encode(jsSingleCharacterString(exec, string[static_cast<unsigned>(dpos)]));
     return JSValue::encode(jsEmptyString(exec));
 }
 
@@ -1246,7 +1245,7 @@
             ASSERT(limit);
 
             do {
-                result->putDirectIndex(exec, position, jsSingleCharacterSubstring(exec, input, position));
+                result->putDirectIndex(exec, position, jsSingleCharacterString(exec, input[position]));
             } while (++position < limit);
 
             return JSValue::encode(result);
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to