Title: [292327] branches/safari-613-branch/Source/_javascript_Core/runtime
Revision
292327
Author
alanc...@apple.com
Date
2022-04-04 14:55:22 -0700 (Mon, 04 Apr 2022)

Log Message

Cherry-pick r289177. rdar://91259284

    Cache the most recent AtomString produced by JSString::toIdentifier
    https://bugs.webkit.org/show_bug.cgi?id=236124

    Reviewed by Yusuke Suzuki.

    JSString::toIdentifier does not store the result of atomizing its string
    value, except when it is a rope. We can often end up atomizing the same
    JSString a number of times.

    This patch caches the last atomized string produced from
    JSString::toIdentifier in a given VM. From local testing, this is a 0.5%
    Speedometer2 improvement on an M1 MacBook Air, although surprisingly is
    neutral on a recent Intel MacBook Pro.

    runtime/JSString.h:
    (JSC::JSRopeString::toIdentifier const):
    (JSC::JSString::toIdentifier const):

    runtime/VM.h:

Modified Paths

Diff

Modified: branches/safari-613-branch/Source/_javascript_Core/runtime/JSString.h (292326 => 292327)


--- branches/safari-613-branch/Source/_javascript_Core/runtime/JSString.h	2022-04-04 21:55:19 UTC (rev 292326)
+++ branches/safari-613-branch/Source/_javascript_Core/runtime/JSString.h	2022-04-04 21:55:22 UTC (rev 292327)
@@ -602,6 +602,7 @@
     JS_EXPORT_PRIVATE RefPtr<AtomStringImpl> resolveRopeToExistingAtomString(JSGlobalObject*) const;
     template<typename CharacterType> NEVER_INLINE void resolveRopeSlowCase(CharacterType*) const;
     template<typename CharacterType> void resolveRopeInternalNoSubstring(CharacterType*) const;
+    Identifier toIdentifier(JSGlobalObject*) const;
     void outOfMemory(JSGlobalObject* nullOrGlobalObjectForOOM) const;
     void resolveRopeInternal8(LChar*) const;
     void resolveRopeInternal16(UChar*) const;
@@ -759,15 +760,31 @@
     return JSString::create(vm, s.releaseImpl().releaseNonNull());
 }
 
-ALWAYS_INLINE Identifier JSString::toIdentifier(JSGlobalObject* globalObject) const
+ALWAYS_INLINE Identifier JSRopeString::toIdentifier(JSGlobalObject* globalObject) const
 {
     VM& vm = getVM(globalObject);
     auto scope = DECLARE_THROW_SCOPE(vm);
-    AtomString atomString = toAtomString(globalObject);
+    auto atomString = static_cast<const JSRopeString*>(this)->resolveRopeToAtomString(globalObject);
     RETURN_IF_EXCEPTION(scope, { });
     return Identifier::fromString(vm, atomString);
 }
 
+ALWAYS_INLINE Identifier JSString::toIdentifier(JSGlobalObject* globalObject) const
+{
+    if constexpr (validateDFGDoesGC)
+        vm().verifyCanGC();
+    if (isRope())
+        return static_cast<const JSRopeString*>(this)->toIdentifier(globalObject);
+    VM& vm = getVM(globalObject);
+    if (valueInternal().impl()->isAtom())
+        return Identifier::fromString(vm, Ref { *static_cast<AtomStringImpl*>(valueInternal().impl()) });
+    if (vm.lastAtomizedIdentifierStringImpl.ptr() != valueInternal().impl()) {
+        vm.lastAtomizedIdentifierStringImpl = *valueInternal().impl();
+        vm.lastAtomizedIdentifierAtomStringImpl = AtomStringImpl::add(valueInternal().impl()).releaseNonNull();
+    }
+    return Identifier::fromString(vm, Ref { vm.lastAtomizedIdentifierAtomStringImpl });
+}
+
 ALWAYS_INLINE AtomString JSString::toAtomString(JSGlobalObject* globalObject) const
 {
     if constexpr (validateDFGDoesGC)

Modified: branches/safari-613-branch/Source/_javascript_Core/runtime/VM.h (292326 => 292327)


--- branches/safari-613-branch/Source/_javascript_Core/runtime/VM.h	2022-04-04 21:55:19 UTC (rev 292326)
+++ branches/safari-613-branch/Source/_javascript_Core/runtime/VM.h	2022-04-04 21:55:22 UTC (rev 292327)
@@ -586,6 +586,8 @@
     NumericStrings numericStrings;
     std::unique_ptr<SimpleStats> machineCodeBytesPerBytecodeWordForBaselineJIT;
     Strong<JSString> lastCachedString;
+    Ref<StringImpl> lastAtomizedIdentifierStringImpl { *StringImpl::empty() };
+    Ref<AtomStringImpl> lastAtomizedIdentifierAtomStringImpl { *static_cast<AtomStringImpl*>(StringImpl::empty()) };
     JSONAtomStringCache jsonAtomStringCache;
 
     AtomStringTable* atomStringTable() const { return m_atomStringTable; }
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to