Skip to content

Commit

Permalink
Micro-optimize jsStringWithCache()
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=255106

Reviewed by Geoffrey Garen.

Micro-optimize jsStringWithCache():
- Call JSString::getValueImpl() instead of tryGetValueImpl() on the cached
  JSString. We know we never cache JSRopeStrings.
- Call JSString::create() directly in the slow path instead of jsString()
  to avoid special handling of strings with length 0 and 1. It is unecessary
  since the fast path in jsStringWithCache() already did this special handling.

* Source/JavaScriptCore/runtime/JSString.cpp:
(JSC::jsStringWithCacheSlowCase):
* Source/JavaScriptCore/runtime/JSString.h:
(JSC::jsStringWithCache):

Canonical link: https://commits.webkit.org/262712@main
  • Loading branch information
cdumez committed Apr 7, 2023
1 parent 6833b7d commit 668e78f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Source/JavaScriptCore/runtime/JSString.cpp
Expand Up @@ -397,7 +397,8 @@ bool JSString::getStringPropertyDescriptor(JSGlobalObject* globalObject, Propert

JSString* jsStringWithCacheSlowCase(VM& vm, StringImpl& stringImpl)
{
JSString* string = jsString(vm, String(stringImpl));
ASSERT(stringImpl.length() > 1 || (stringImpl.length() == 1 && stringImpl[0] > maxSingleCharacterString));
JSString* string = JSString::create(vm, stringImpl);
vm.lastCachedString.set(vm, string);
return string;
}
Expand Down
20 changes: 11 additions & 9 deletions Source/JavaScriptCore/runtime/JSString.h
Expand Up @@ -267,6 +267,7 @@ class JSString : public JSCell {
friend JSString* jsString(JSGlobalObject*, const String&, const String&);
friend JSString* jsString(JSGlobalObject*, JSString*, JSString*, JSString*);
friend JSString* jsString(JSGlobalObject*, const String&, const String&, const String&);
friend JS_EXPORT_PRIVATE JSString* jsStringWithCacheSlowCase(VM&, StringImpl&);
friend JSString* jsSingleCharacterString(VM&, UChar);
friend JSString* jsSingleCharacterString(VM&, LChar);
friend JSString* jsNontrivialString(VM&, const String&);
Expand Down Expand Up @@ -1036,22 +1037,23 @@ inline JSString* jsOwnedString(VM& vm, const String& s)

ALWAYS_INLINE JSString* jsStringWithCache(VM& vm, const String& s)
{
StringImpl* stringImpl = s.impl();
if (!stringImpl || !stringImpl->length())
unsigned length = s.length();
if (!length)
return jsEmptyString(vm);

if (stringImpl->length() == 1) {
UChar singleCharacter = (*stringImpl)[0u];
if (singleCharacter <= maxSingleCharacterString)
return vm.smallStrings.singleCharacterString(static_cast<unsigned char>(singleCharacter));
auto& stringImpl = *s.impl();
if (length == 1) {
auto c = stringImpl[0];
if (c <= maxSingleCharacterString)
return vm.smallStrings.singleCharacterString(c);
}

if (JSString* lastCachedString = vm.lastCachedString.get()) {
if (lastCachedString->tryGetValueImpl() == stringImpl)
if (auto* lastCachedString = vm.lastCachedString.get()) {
if (lastCachedString->getValueImpl() == &stringImpl)
return lastCachedString;
}

return jsStringWithCacheSlowCase(vm, *stringImpl);
return jsStringWithCacheSlowCase(vm, stringImpl);
}

ALWAYS_INLINE bool JSString::getStringPropertySlot(JSGlobalObject* globalObject, PropertyName propertyName, PropertySlot& slot)
Expand Down

0 comments on commit 668e78f

Please sign in to comment.