Skip to content

Commit e04a6f9

Browse files
committed
[JSC] Object spread and rest should have a fast path for functions
https://bugs.webkit.org/show_bug.cgi?id=328286 Reviewed by Yusuke Suzuki. `{ ...fn }` and `let { a, ...rest } = fn` always took the generic path because JSFunction overrides getOwnPropertySlot and getOwnSpecialPropertyNames for its lazy `length`, `name`, and `prototype`. These properties are not enumerable, so they are never copied. This patch lets a function whose `length` and `name` are not modified use the existing fast path, which only reads the Structure. PostCSS does this in the LazyResult constructor: this.helpers = { ...postcss, postcss, result: this.result } Tailwind CSS v3, which depends on PostCSS, creates a LazyResult for each declaration it validates. Baseline Patched object-spread-function 153.2536+-1.3657 ^ 30.7211+-0.2188 ^ definitely 4.9885x faster object-rest-function 167.4097+-2.9725 ^ 30.2451+-2.5882 ^ definitely 5.5351x faster Tests: JSTests/microbenchmarks/object-rest-function.js JSTests/microbenchmarks/object-spread-function.js JSTests/stress/object-spread-and-rest-function-source.js * JSTests/microbenchmarks/object-rest-function.js: Added. (test): * JSTests/microbenchmarks/object-spread-function.js: Added. (library): (string_appeared_here.library.name): * JSTests/stress/object-spread-and-rest-function-source.js: Added. (shouldBe): (describe): (spreadOnly): (spreadFirst): (spreadLast): (restOnly): (restExcludingA): (restExcludingComputed): (describeWithoutX): (check): (withProperties): (let.factories): (method): (Object.getOwnPropertyDescriptor.get getter): (prototype.new.Promise): (i.let.source.withProperties.i.1): (join.let.source.withProperties.named): (join): (prototype.check): (prototype.check.let.source.withProperties.named): (check.name): (check.length): (prototype.check.let.source): (prototype.check.shouldBe.describe.get __proto__): (prototype.check.shouldBe.describe.set __proto__): (prototype.check.Object.freeze.withProperties): (prototype.check.Object.seal.withProperties): (Private.method): (Private.hasField): (Private.hasMethod): (Private): * Source/JavaScriptCore/runtime/JSGlobalObjectFunctions.cpp: (JSC::canPerformFastPropertyEnumerationForCopyDataProperties): (JSC::JSC_DEFINE_HOST_FUNCTION): Canonical link: https://commits.webkit.org/324519@main
1 parent 2bb0a6b commit e04a6f9

6 files changed

Lines changed: 317 additions & 5 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function test(index)
2+
{
3+
var source = () => index;
4+
source.first = 1;
5+
source.second = 2;
6+
source.third = 3;
7+
source.fourth = 4;
8+
var { first, ...rest } = source;
9+
return rest;
10+
}
11+
noInline(test);
12+
13+
for (var i = 0; i < 5e5; ++i)
14+
test(i);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function library()
2+
{
3+
}
4+
5+
for (var name of ["plugin", "stringify", "parse", "fromJSON", "list", "comment", "atRule", "decl", "rule", "root", "document", "CssSyntaxError", "Declaration", "Container", "Processor", "Document", "Comment", "Warning", "AtRule", "Result", "Input", "Rule", "Root", "Node", "default"])
6+
library[name] = function () { };
7+
8+
function test(result)
9+
{
10+
return { ...library, library, result };
11+
}
12+
noInline(test);
13+
14+
for (var i = 0; i < 1e5; ++i)
15+
test(i);
Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
function shouldBe(actual, expected) {
2+
if (actual !== expected)
3+
throw new Error("bad value: " + actual + ", expected: " + expected);
4+
}
5+
6+
function describe(object) {
7+
return Reflect.ownKeys(object).map((key) => {
8+
let descriptor = Object.getOwnPropertyDescriptor(object, key);
9+
shouldBe(descriptor.writable && descriptor.enumerable && descriptor.configurable, true);
10+
return String(key) + "=" + String(descriptor.value);
11+
}).join();
12+
}
13+
noInline(describe);
14+
15+
function spreadOnly(source) {
16+
return { ...source };
17+
}
18+
noInline(spreadOnly);
19+
20+
function spreadFirst(source) {
21+
return { ...source, x: "x" };
22+
}
23+
noInline(spreadFirst);
24+
25+
function spreadLast(source) {
26+
return { x: "x", ...source };
27+
}
28+
noInline(spreadLast);
29+
30+
function restOnly(source) {
31+
let { ...rest } = source;
32+
return rest;
33+
}
34+
noInline(restOnly);
35+
36+
function restExcludingA(source) {
37+
let { a, ...rest } = source;
38+
return rest;
39+
}
40+
noInline(restExcludingA);
41+
42+
function restExcludingComputed(source, key) {
43+
let { [key]: ignored, ...rest } = source;
44+
return rest;
45+
}
46+
noInline(restExcludingComputed);
47+
48+
function describeWithoutX(object) {
49+
shouldBe(object.x, "x");
50+
delete object.x;
51+
return describe(object);
52+
}
53+
noInline(describeWithoutX);
54+
55+
function check(source, expected) {
56+
shouldBe(describe(spreadOnly(source)), expected);
57+
shouldBe(describe(restOnly(source)), expected);
58+
shouldBe(describeWithoutX(spreadFirst(source)), expected);
59+
shouldBe(describeWithoutX(spreadLast(source)), expected);
60+
}
61+
noInline(check);
62+
63+
function withProperties(source) {
64+
source.a = 1;
65+
source.b = 2;
66+
source.c = 3;
67+
return source;
68+
}
69+
70+
let factories = [
71+
() => function () { },
72+
() => function named(x, y) { },
73+
() => function () { "use strict"; },
74+
() => () => { },
75+
() => ({ method() { } }).method,
76+
() => Object.getOwnPropertyDescriptor({ get getter() { return 1; } }, "getter").get,
77+
() => class { },
78+
() => class Named { constructor(x) { } },
79+
() => class extends Object { },
80+
() => function* () { },
81+
() => async function () { },
82+
() => async function* () { },
83+
() => async () => { },
84+
() => function (x) { }.bind(null),
85+
() => { let result; new Promise((resolve) => { result = resolve; }); return result; },
86+
() => new Function("x", "return x"),
87+
() => new (class extends Function { }),
88+
() => createGlobalObject().Function("x", "return x"),
89+
];
90+
91+
for (let factory of factories) {
92+
check(factory(), "");
93+
94+
let source = withProperties(factory());
95+
check(source, "a=1,b=2,c=3");
96+
shouldBe(describe(restExcludingA(source)), "b=2,c=3");
97+
shouldBe(describe(restExcludingComputed(source, "b")), "a=1,c=3");
98+
shouldBe(describe(restExcludingComputed(source, "name")), "a=1,b=2,c=3");
99+
shouldBe(Object.getPrototypeOf(spreadOnly(source)), Object.prototype);
100+
shouldBe(Reflect.ownKeys(source).join().replace(",prototype", ""), "length,name,a,b,c");
101+
source.prototype;
102+
check(source, "a=1,b=2,c=3");
103+
Object.defineProperty(source, "name", { enumerable: true });
104+
shouldBe(Object.keys(spreadOnly(source)).join(), "name,a,b,c");
105+
shouldBe(Object.keys(restOnly(source)).join(), "name,a,b,c");
106+
Object.defineProperty(source, "length", { enumerable: true });
107+
shouldBe(Object.keys(spreadOnly(source)).join(), "length,name,a,b,c");
108+
shouldBe(Object.keys(restOnly(source)).join(), "length,name,a,b,c");
109+
}
110+
111+
for (let i = 0; i < testLoopCount; ++i) {
112+
let source = withProperties(i & 1 ? function () { } : () => { });
113+
shouldBe(Object.keys(spreadOnly(source)).join(), "a,b,c");
114+
shouldBe(Object.keys(spreadFirst(source)).join(), "a,b,c,x");
115+
shouldBe(Object.keys(spreadLast(source)).join(), "x,a,b,c");
116+
shouldBe(Object.keys(restExcludingA(source)).join(), "b,c");
117+
shouldBe(Object.keys(spreadOnly(class { static a = 1; static name = "field"; })).join(), "name,a");
118+
}
119+
120+
{
121+
let source = withProperties(function named() { });
122+
Object.defineProperty(source, "name", { enumerable: true });
123+
check(source, "name=named,a=1,b=2,c=3");
124+
}
125+
126+
{
127+
let source = withProperties(function (x, y) { });
128+
Object.defineProperty(source, "length", { enumerable: true });
129+
check(source, "length=2,a=1,b=2,c=3");
130+
}
131+
132+
{
133+
let source = withProperties((x) => { });
134+
Object.defineProperty(source, "name", { value: "defined", enumerable: true });
135+
Object.defineProperty(source, "length", { enumerable: true });
136+
check(source, "length=1,name=defined,a=1,b=2,c=3");
137+
}
138+
139+
{
140+
let source = withProperties(function named() { });
141+
check(source, "a=1,b=2,c=3");
142+
Reflect.defineProperty(source, "name", { enumerable: true });
143+
check(source, "name=named,a=1,b=2,c=3");
144+
}
145+
146+
{
147+
let source = withProperties(function named(x) { }.bind(null));
148+
Object.defineProperty(source, "name", { enumerable: true });
149+
check(source, "name=bound named,a=1,b=2,c=3");
150+
}
151+
152+
check(class { static a = 1; static name = "field"; static b = 2; }, "name=field,a=1,b=2");
153+
check(class { static a = 1; static length = "field"; static b = 2; }, "length=field,a=1,b=2");
154+
check(class { static a = 1; static ["name"] = "field"; static ["length"] = "other"; }, "length=other,name=field,a=1");
155+
check(class { static a = 1; static name() { } static length() { } static b = 2; }, "a=1,b=2");
156+
check(class extends class { static inherited = 1; } { static a = 1; }, "a=1");
157+
158+
{
159+
let source = withProperties(function () { });
160+
Object.defineProperty(source, "hidden", { value: 1, enumerable: false });
161+
Object.defineProperty(source, "readOnly", { value: 2, enumerable: true });
162+
check(source, "a=1,b=2,c=3,readOnly=2");
163+
}
164+
165+
{
166+
let source = function () { };
167+
source[Symbol("symbol")] = 1;
168+
source.a = 2;
169+
check(source, "a=2,Symbol(symbol)=1");
170+
}
171+
172+
{
173+
let source = withProperties(function () { });
174+
source[1] = "one";
175+
source[0] = "zero";
176+
check(source, "0=zero,1=one,a=1,b=2,c=3");
177+
}
178+
179+
{
180+
let source = withProperties(() => { });
181+
source.prototype = 4;
182+
check(source, "a=1,b=2,c=3,prototype=4");
183+
}
184+
185+
{
186+
let source = withProperties(function () { });
187+
source.prototype = 4;
188+
check(source, "a=1,b=2,c=3");
189+
}
190+
191+
{
192+
let source = withProperties(function () { });
193+
Object.defineProperty(source, "__proto__", { value: 4, writable: true, enumerable: true, configurable: true });
194+
check(source, "a=1,b=2,c=3,__proto__=4");
195+
shouldBe(describe({ get __proto__() { return "getter"; }, set __proto__(value) { throw new Error("setter"); }, ...source }), "__proto__=4,a=1,b=2,c=3");
196+
}
197+
198+
check(Object.freeze(withProperties(function () { })), "a=1,b=2,c=3");
199+
check(Object.seal(withProperties(function () { })), "a=1,b=2,c=3");
200+
201+
{
202+
let source = withProperties(function () { });
203+
delete source.b;
204+
delete source.name;
205+
delete source.length;
206+
check(source, "a=1,c=3");
207+
}
208+
209+
{
210+
let source = function () { };
211+
for (let i = 0; i < 200; ++i)
212+
source["k" + i] = i;
213+
shouldBe(Object.keys(spreadOnly(source)).length, 200);
214+
shouldBe(Object.keys(restOnly(source)).length, 200);
215+
for (let i = 0; i < 200; ++i) {
216+
if (i % 50)
217+
delete source["k" + i];
218+
}
219+
check(source, "k0=0,k50=50,k100=100,k150=150");
220+
}
221+
222+
{
223+
let log = [];
224+
let source = function () { };
225+
source.a = 1;
226+
Object.defineProperty(source, "b", { get() { log.push("b"); delete source.c; source.d = 4; return 2; }, enumerable: true, configurable: true });
227+
source.c = 3;
228+
shouldBe(describe(spreadOnly(source)), "a=1,b=2");
229+
shouldBe(log.join(), "b");
230+
shouldBe(describe(restOnly(source)), "a=1,b=2,d=4");
231+
shouldBe(log.join(), "b,b");
232+
}
233+
234+
{
235+
class Private {
236+
static #field = 1;
237+
static #method() { }
238+
static a = 1;
239+
static hasField(object) { return #field in object; }
240+
static hasMethod(object) { return #method in object; }
241+
}
242+
shouldBe(Private.hasField(Private), true);
243+
shouldBe(Private.hasMethod(Private), true);
244+
for (let result of [spreadOnly(Private), spreadFirst(Private), spreadLast(Private), restOnly(Private)]) {
245+
shouldBe(result.a, 1);
246+
shouldBe(Private.hasField(result), false);
247+
shouldBe(Private.hasMethod(result), false);
248+
}
249+
}
250+
251+
{
252+
let source = withProperties(function () { });
253+
let result = spreadOnly(source);
254+
result.a = 10;
255+
source.b = 20;
256+
shouldBe(describe(result), "a=10,b=2,c=3");
257+
shouldBe(describe(spreadOnly(source)), "a=1,b=20,c=3");
258+
}
259+
260+
check(Number, "");
261+
check(Promise, "");
262+
check(Math.max, "");
263+
check(Array.prototype.map, "");

‎Source/JavaScriptCore/runtime/JSFunction.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ class JSFunction : public JSCallee {
178178

179179
bool mayHaveNonReifiedPrototype();
180180

181+
bool canPerformFastEnumerablePropertyEnumeration(VM&);
182+
181183
// This method may be called for host functions, in which case it
182184
// will return an arbitrary value. This should only be used for
183185
// optimized paths in which the return value does not matter for

‎Source/JavaScriptCore/runtime/JSFunctionInlines.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,20 @@ inline bool JSFunction::mayHaveNonReifiedPrototype()
229229
return !isHostOrBuiltinFunction() && jsExecutable()->hasPrototypeProperty();
230230
}
231231

232+
inline bool JSFunction::canPerformFastEnumerablePropertyEnumeration(VM& vm)
233+
{
234+
if (!canAssumeNameAndLengthAreOriginal(vm))
235+
return false;
236+
Structure* structure = this->structure();
237+
if (structure->hasAnyKindOfGetterSetterProperties())
238+
return false;
239+
if (structure->isUncacheableDictionary())
240+
return false;
241+
if (structure->hasUnderscoreProtoPropertyExcludingOriginalProto())
242+
return false;
243+
return true;
244+
}
245+
232246
inline bool JSFunction::canUseAllocationProfiles()
233247
{
234248
if (isHostOrBuiltinFunction()) {

‎Source/JavaScriptCore/runtime/JSGlobalObjectFunctions.cpp‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -836,9 +836,13 @@ JSC_DEFINE_HOST_FUNCTION(globalFuncImportModule, (JSGlobalObject* globalObject,
836836
return JSValue::encode(importPromise);
837837
}
838838

839-
static bool LEGACY_NODELETE canPerformFastPropertyEnumerationForCopyDataProperties(Structure* structure)
839+
static bool LEGACY_NODELETE canPerformFastPropertyEnumerationForCopyDataProperties(VM& vm, JSObject* source)
840840
{
841-
if (!structure->canPerformFastPropertyEnumerationCommon())
841+
Structure* structure = source->structure();
842+
if (auto* function = dynamicDowncast<JSFunction>(source)) {
843+
if (!function->canPerformFastEnumerablePropertyEnumeration(vm))
844+
return false;
845+
} else if (!structure->canPerformFastPropertyEnumerationCommon())
842846
return false;
843847
// FIXME: Indexed properties can be handled.
844848
// https://bugs.webkit.org/show_bug.cgi?id=185358
@@ -908,8 +912,8 @@ JSC_DEFINE_HOST_FUNCTION(globalFuncCopyDataProperties, (JSGlobalObject* globalOb
908912
}
909913

910914
auto sourceStructure = source->structure();
911-
if (canPerformFastPropertyEnumerationForCopyDataProperties(sourceStructure)) [[likely]] {
912-
if ((!excludedSet || excludedSet->isEmpty()) && objectCloneFast(vm, target, source))
915+
if (canPerformFastPropertyEnumerationForCopyDataProperties(vm, source)) [[likely]] {
916+
if ((!excludedSet || excludedSet->isEmpty()) && sourceStructure->typeInfo().type() == FinalObjectType && objectCloneFast(vm, target, source))
913917
return JSValue::encode(target);
914918

915919
EnsureStillAliveScope sourceStructureScope(sourceStructure);
@@ -1009,7 +1013,7 @@ JSC_DEFINE_HOST_FUNCTION(globalFuncCloneObject, (JSGlobalObject* globalObject, C
10091013
JSObject* target = constructEmptyObject(globalObject);
10101014
RETURN_IF_EXCEPTION(scope, { });
10111015

1012-
if (canPerformFastPropertyEnumerationForCopyDataProperties(sourceStructure)) [[likely]] {
1016+
if (canPerformFastPropertyEnumerationForCopyDataProperties(vm, source)) [[likely]] {
10131017
EnsureStillAliveScope sourceStructureScope(sourceStructure);
10141018
Vector<UniquedStringImpl*, 8> properties; // sourceStructure ensures the lifetimes of these strings.
10151019
MarkedArgumentBuffer values;

0 commit comments

Comments
 (0)