Speed up the Stringifier::toJSON() fast case
authorbenjamin@webkit.org <benjamin@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 29 Jul 2015 02:15:41 +0000 (02:15 +0000)
committerbenjamin@webkit.org <benjamin@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 29 Jul 2015 02:15:41 +0000 (02:15 +0000)
https://bugs.webkit.org/show_bug.cgi?id=147383

Patch by Benjamin Poulain <bpoulain@apple.com> on 2015-07-28
Reviewed by Andreas Kling.

Source/JavaScriptCore:

* runtime/JSONObject.cpp:
(JSC::Stringifier::toJSON):
(JSC::Stringifier::toJSONImpl):

LayoutTests:

Make the fast case of Stringifier::toJSON() inline and the uncommon
case out-of-line.

* js/dom/JSON-stringify-string-object-with-tojson-expected.txt: Added.
* js/dom/JSON-stringify-string-object-with-tojson.html: Added.
* js/resources/JSON-stringify-string-object-with-tojson.js: Added.
(stringObject.toJSON):
(String.prototype.toJSON):

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

LayoutTests/ChangeLog
LayoutTests/js/dom/JSON-stringify-string-object-with-tojson-expected.txt [new file with mode: 0644]
LayoutTests/js/dom/JSON-stringify-string-object-with-tojson.html [new file with mode: 0644]
LayoutTests/js/resources/JSON-stringify-string-object-with-tojson.js [new file with mode: 0644]
Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/runtime/JSONObject.cpp

index 84b950d..2a3d438 100644 (file)
@@ -1,3 +1,19 @@
+2015-07-28  Benjamin Poulain  <bpoulain@apple.com>
+
+        Speed up the Stringifier::toJSON() fast case
+        https://bugs.webkit.org/show_bug.cgi?id=147383
+
+        Reviewed by Andreas Kling.
+
+        Make the fast case of Stringifier::toJSON() inline and the uncommon
+        case out-of-line.
+
+        * js/dom/JSON-stringify-string-object-with-tojson-expected.txt: Added.
+        * js/dom/JSON-stringify-string-object-with-tojson.html: Added.
+        * js/resources/JSON-stringify-string-object-with-tojson.js: Added.
+        (stringObject.toJSON):
+        (String.prototype.toJSON):
+
 2015-07-28  Simon Fraser  <simon.fraser@apple.com>
 
         Animations sometimes fail to start
diff --git a/LayoutTests/js/dom/JSON-stringify-string-object-with-tojson-expected.txt b/LayoutTests/js/dom/JSON-stringify-string-object-with-tojson-expected.txt
new file mode 100644 (file)
index 0000000..5e41c5d
--- /dev/null
@@ -0,0 +1,12 @@
+PASS nativeJSON.stringify(stringObject) is "\"Foo\""
+PASS nativeJSON.stringify(stringObject) is JSON.stringify(stringObject)
+PASS nativeJSON.stringify(stringObject) is "\"Weird Case 1\""
+PASS nativeJSON.stringify(stringObject) is JSON.stringify(stringObject)
+PASS nativeJSON.stringify(stringObject) is "\"Bar\""
+PASS nativeJSON.stringify(stringObject) is JSON.stringify(stringObject)
+PASS nativeJSON.stringify(stringObject) is "\"Weird Case 2\""
+PASS nativeJSON.stringify(stringObject) is JSON.stringify(stringObject)
+PASS successfullyParsed is true
+
+TEST COMPLETE
+
diff --git a/LayoutTests/js/dom/JSON-stringify-string-object-with-tojson.html b/LayoutTests/js/dom/JSON-stringify-string-object-with-tojson.html
new file mode 100644 (file)
index 0000000..14822ec
--- /dev/null
@@ -0,0 +1,15 @@
+<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
+<html>
+<head>
+<script src="../../resources/js-test-pre.js"></script>
+</head>
+<body>
+<script>
+var nativeJSON = this.JSON;
+this.JSON = null;
+</script>
+<script src="../resources/json2-es5-compat.js"></script>
+<script src="../resources/JSON-stringify-string-object-with-tojson.js"></script>
+<script src="../../resources/js-test-post.js"></script>
+</body>
+</html>
\ No newline at end of file
diff --git a/LayoutTests/js/resources/JSON-stringify-string-object-with-tojson.js b/LayoutTests/js/resources/JSON-stringify-string-object-with-tojson.js
new file mode 100644 (file)
index 0000000..45d75a3
--- /dev/null
@@ -0,0 +1,15 @@
+var stringObject = new String("Foo");
+shouldBeEqualToString('nativeJSON.stringify(stringObject)', '"Foo"');
+shouldBe('nativeJSON.stringify(stringObject)', 'JSON.stringify(stringObject)');
+
+stringObject.toJSON = function() { return "Weird Case 1"; }
+shouldBeEqualToString('nativeJSON.stringify(stringObject)', '"Weird Case 1"');
+shouldBe('nativeJSON.stringify(stringObject)', 'JSON.stringify(stringObject)');
+
+var stringObject = new String("Bar");
+shouldBeEqualToString('nativeJSON.stringify(stringObject)', '"Bar"');
+shouldBe('nativeJSON.stringify(stringObject)', 'JSON.stringify(stringObject)');
+
+String.prototype.toJSON = function() { return "Weird Case 2"; }
+shouldBeEqualToString('nativeJSON.stringify(stringObject)', '"Weird Case 2"');
+shouldBe('nativeJSON.stringify(stringObject)', 'JSON.stringify(stringObject)');
\ No newline at end of file
index 95a7164..5743a19 100644 (file)
@@ -1,3 +1,14 @@
+2015-07-28  Benjamin Poulain  <bpoulain@apple.com>
+
+        Speed up the Stringifier::toJSON() fast case
+        https://bugs.webkit.org/show_bug.cgi?id=147383
+
+        Reviewed by Andreas Kling.
+
+        * runtime/JSONObject.cpp:
+        (JSC::Stringifier::toJSON):
+        (JSC::Stringifier::toJSONImpl):
+
 2015-07-28  Sukolsak Sakshuwong  <sukolsak@gmail.com>
 
         Implement WebAssembly module parser
index 49d6b15..f67ba7e 100644 (file)
@@ -108,6 +108,7 @@ private:
     friend class Holder;
 
     JSValue toJSON(JSValue, const PropertyNameForFunctionCall&);
+    JSValue toJSONImpl(JSValue, const PropertyNameForFunctionCall&);
 
     enum StringifyResult { StringifyFailed, StringifySucceeded, StringifyFailedDueToUndefinedValue };
     StringifyResult appendStringifiedValue(StringBuilder&, JSValue, JSObject* holder, const PropertyNameForFunctionCall&);
@@ -253,12 +254,16 @@ Local<Unknown> Stringifier::stringify(Handle<Unknown> value)
     return Local<Unknown>(m_exec->vm(), jsString(m_exec, result.toString()));
 }
 
-inline JSValue Stringifier::toJSON(JSValue value, const PropertyNameForFunctionCall& propertyName)
+ALWAYS_INLINE JSValue Stringifier::toJSON(JSValue value, const PropertyNameForFunctionCall& propertyName)
 {
     ASSERT(!m_exec->hadException());
     if (!value.isObject() || !asObject(value)->hasProperty(m_exec, m_exec->vm().propertyNames->toJSON))
         return value;
+    return toJSONImpl(value, propertyName);
+}
 
+JSValue Stringifier::toJSONImpl(JSValue value, const PropertyNameForFunctionCall& propertyName)
+{
     JSValue toJSONFunction = asObject(value)->get(m_exec, m_exec->vm().propertyNames->toJSON);
     if (m_exec->hadException())
         return jsNull();