Micro-optimize Strings in JS bindings.
authorakling@apple.com <akling@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 4 Mar 2014 17:05:26 +0000 (17:05 +0000)
committerakling@apple.com <akling@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 4 Mar 2014 17:05:26 +0000 (17:05 +0000)
<https://webkit.org/b/129673>

Source/JavaScriptCore:

Make jsStringWithWeakOwner() take a StringImpl& instead of a String.
This avoids branches in length() and operator[].

Also call JSString::create() directly instead of jsString() and just
assert that the string length is >1. This way we don't duplicate the
optimizations for empty and single-character strings.

Reviewed by Ryosuke Niwa.

* runtime/JSString.h:
(JSC::jsStringWithWeakOwner):

Source/WebCore:

Tweaked for new jsStringWithWeakOwner signature. This patch removes
36 bytes of code from every wrapper getter that returns a DOMString.

Reviewed by Ryosuke Niwa.

* bindings/js/JSDOMBinding.h:
(WebCore::jsStringWithCache):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@165054 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/runtime/JSString.h
Source/WebCore/ChangeLog
Source/WebCore/bindings/js/JSDOMBinding.h

index 386af87e51f4f938b02e6772ec37a2d362f3f460..d13014ac90ffe28537cb11768d92dff1107e4a5c 100644 (file)
@@ -1,3 +1,20 @@
+2014-03-04  Andreas Kling  <akling@apple.com>
+
+        Micro-optimize Strings in JS bindings.
+        <https://webkit.org/b/129673>
+
+        Make jsStringWithWeakOwner() take a StringImpl& instead of a String.
+        This avoids branches in length() and operator[].
+
+        Also call JSString::create() directly instead of jsString() and just
+        assert that the string length is >1. This way we don't duplicate the
+        optimizations for empty and single-character strings.
+
+        Reviewed by Ryosuke Niwa.
+
+        * runtime/JSString.h:
+        (JSC::jsStringWithWeakOwner):
+
 2014-03-04  Dániel Bátyai  <dbatyai.u-szeged@partner.samsung.com>
 
         Implement Number.prototype.clz()
index b7cfdeb30973eb2a932a4972d37743b3007a2bd6..05c32edfeeee7be8e12e4354f61bb7c554e09e39 100644 (file)
@@ -412,25 +412,27 @@ namespace JSC {
         return JSString::create(*vm, s.impl());
     }
 
-    inline JSString* jsStringWithWeakOwner(VM* vm, const String& s)
+    inline JSString* jsStringWithWeakOwner(VM& vm, StringImpl& stringImpl)
     {
-        WeakHandleOwner* jsStringWeakOwner = vm->jsStringWeakOwner.get();
-        StringImpl* impl = s.impl();
+        WeakHandleOwner* jsStringWeakOwner = vm.jsStringWeakOwner.get();
 
-        // If this vm is not allowed to weakly own strings just call jsString.
-        if (!jsStringWeakOwner || !impl)
-            return jsString(vm, s);
+        // Should have picked a VM-global empty or single-character string already.
+        ASSERT(stringImpl.length() > 1);
+
+        // If this VM is not allowed to weakly own strings just make a new JSString.
+        if (!jsStringWeakOwner)
+            return JSString::create(vm, &stringImpl);
 
         // Check for an existing weakly owned JSString.
-        if (WeakImpl* weakImpl = impl->weakJSString()) {
+        if (WeakImpl* weakImpl = stringImpl.weakJSString()) {
             if (weakImpl->state() == WeakImpl::Live)
                 return asString(weakImpl->jsValue());
             WeakSet::deallocate(weakImpl);
-            impl->setWeakJSString(nullptr);
+            stringImpl.setWeakJSString(nullptr);
         }
 
-        JSString* string = jsString(vm, s);
-        impl->setWeakJSString(WeakSet::allocate(string, jsStringWeakOwner, impl));
+        JSString* string = JSString::create(vm, &stringImpl);
+        stringImpl.setWeakJSString(WeakSet::allocate(string, jsStringWeakOwner, &stringImpl));
         return string;
     }
 
index 81318bab79f66845c2ce96983ec2650550c26bb0..b6dab3c19114a6fbdf3db15c86f4d348fde913f5 100644 (file)
@@ -1,3 +1,16 @@
+2014-03-04  Andreas Kling  <akling@apple.com>
+
+        Micro-optimize Strings in JS bindings.
+        <https://webkit.org/b/129673>
+
+        Tweaked for new jsStringWithWeakOwner signature. This patch removes
+        36 bytes of code from every wrapper getter that returns a DOMString.
+
+        Reviewed by Ryosuke Niwa.
+
+        * bindings/js/JSDOMBinding.h:
+        (WebCore::jsStringWithCache):
+
 2014-03-03  David Kilzer  <ddkilzer@apple.com>
 
         SVGPropertyTearOffs should detachChildren before deleting its value.
index 9b0da62f9faf1967a7b2d8b389971e5cee083b7e..48ef697bd20127950500b6d6296a68ac1d2092ab 100644 (file)
@@ -571,19 +571,19 @@ JSC::EncodedJSValue objectToStringFunctionGetter(JSC::ExecState*, JSC::JSObject*
 
 inline JSC::JSValue jsStringWithCache(JSC::ExecState* exec, const String& s)
 {
+    JSC::VM& vm = exec->vm();
+
     StringImpl* stringImpl = s.impl();
     if (!stringImpl || !stringImpl->length())
-        return jsEmptyString(exec);
+        return jsEmptyString(&vm);
 
     if (stringImpl->length() == 1) {
         UChar singleCharacter = (*stringImpl)[0u];
-        if (singleCharacter <= JSC::maxSingleCharacterString) {
-            JSC::VM* vm = &exec->vm();
-            return vm->smallStrings.singleCharacterString(static_cast<unsigned char>(singleCharacter));
-        }
+        if (singleCharacter <= JSC::maxSingleCharacterString)
+            return vm.smallStrings.singleCharacterString(static_cast<unsigned char>(singleCharacter));
     }
 
-    return JSC::jsStringWithWeakOwner(&exec->vm(), s);
+    return JSC::jsStringWithWeakOwner(vm, *stringImpl);
 }
 
 inline String propertyNameToString(JSC::PropertyName propertyName)