Skip to content

Commit fa3737d

Browse files
committed
[JSC] Allocate property storage of arrays without C++ operation call
https://bugs.webkit.org/show_bug.cgi?id=327336 Reviewed by Yusuke Suzuki. When a put adds a named property to an array and the out-of-line property storage has to grow, the butterfly is reallocated together with the indexing header and the vector. Put transition IC stubs, DFG and FTL always called a C++ operation for this. function tag(array, value) { array.tag = value; return array; } This patch allocates the new butterfly in JIT code when the indexing type is Undecided, Int32, Double or Contiguous. The allocation size is computed from the vectorLength, and the indexing header and the vector are copied by a loop. ArrayStorage and typed arrays keep calling the operation. AccessCase::doesCalls() no longer reports these transitions as making calls. This pattern is very common in code compiled by dart2js, such as dart-sass. Its runtime stores the type information of a list to a symbol-keyed property of the array right after creating it. _setArrayType(target, rti) { target[init.arrayRti] = rti; return target; } Baseline Patched put-by-val-with-symbol-transition-on-empty-array 44.7702+-0.4310 ^ 37.1899+-0.3508 ^ definitely 1.2038x faster put-by-id-transition-on-array 46.2070+-0.3462 ^ 37.0726+-0.5931 ^ definitely 1.2464x faster Tests: JSTests/microbenchmarks/put-by-id-transition-on-array.js JSTests/microbenchmarks/put-by-val-with-symbol-transition-on-empty-array.js JSTests/stress/allocate-property-storage-with-indexing-header.js * JSTests/microbenchmarks/put-by-id-transition-on-array.js: Added. (tag): * JSTests/microbenchmarks/put-by-val-with-symbol-transition-on-empty-array.js: Added. (tag): * JSTests/stress/allocate-property-storage-with-indexing-header.js: Added. (shouldBe): (const.taggers.one.check): (const.taggers.checked.check): (const.taggers.symbol.check): (const.taggers.nine.check): (compile): (checkElements): (run): * Source/JavaScriptCore/bytecode/AccessCase.cpp: (JSC::AccessCase::createTransition): (JSC::AccessCase::doesCalls const): * Source/JavaScriptCore/bytecode/InlineCacheCompiler.cpp: (JSC::InlineCacheCompiler::generateAccessCase): * Source/JavaScriptCore/dfg/DFGSpeculativeJIT.cpp: (JSC::DFG::SpeculativeJIT::compileAllocatePropertyStorageWithIndexingHeader): (JSC::DFG::SpeculativeJIT::compileAllocatePropertyStorage): (JSC::DFG::SpeculativeJIT::compileReallocatePropertyStorage): * Source/JavaScriptCore/dfg/DFGSpeculativeJIT.h: * Source/JavaScriptCore/ftl/FTLLowerDFGToB3.cpp: (JSC::FTL::DFG::LowerDFGToB3::compileCompareStrictEq): Canonical link: https://commits.webkit.org/323888@main
1 parent 9a05032 commit fa3737d

9 files changed

Lines changed: 344 additions & 10 deletions
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
(function() {
2+
function tag(array, value) {
3+
array.tag = value;
4+
return array;
5+
}
6+
noInline(tag);
7+
8+
var result = 0;
9+
for (var i = 0; i < 2000000; ++i) {
10+
var array = tag([i, i + 1], i);
11+
result += array.tag + array[1];
12+
}
13+
if (result !== 4000000000000)
14+
throw new Error("Bad result: " + result);
15+
})();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
(function() {
2+
var symbol = Symbol("tag");
3+
4+
function tag(array, value) {
5+
array[symbol] = value;
6+
return array;
7+
}
8+
9+
var result = 0;
10+
for (var i = 0; i < 2000000; ++i) {
11+
var array = tag([], i);
12+
result += array[symbol] + array.length;
13+
}
14+
if (result !== 1999999000000)
15+
throw new Error("Bad result: " + result);
16+
})();
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
function shouldBe(actual, expected) {
2+
if (actual !== expected)
3+
throw new Error(`expected ${String(expected)} but got ${String(actual)}`);
4+
}
5+
6+
const symbol = Symbol("tag");
7+
8+
const makers = {
9+
empty: {
10+
tagger: "symbol",
11+
hot: true,
12+
make: `return [];`,
13+
length: 0,
14+
},
15+
int32: {
16+
tagger: "nine",
17+
make: `return [i, i + 1, i + 2];`,
18+
length: 3,
19+
element: (i, index) => i + index,
20+
},
21+
double: {
22+
tagger: "one",
23+
make: `return [i + 0.5, i + 1.5];`,
24+
length: 2,
25+
element: (i, index) => i + index + 0.5,
26+
},
27+
contiguous: {
28+
tagger: "nine",
29+
hot: true,
30+
make: `return [i, "b", "c", "d", i + 4];`,
31+
length: 5,
32+
element: (i, index) => index % 4 ? "abcd"[index] : i + index,
33+
},
34+
holes: {
35+
tagger: "one",
36+
hot: true,
37+
make: `const array = new Array(9); array[4] = i + 4; array[8] = i + 8; return array;`,
38+
length: 9,
39+
element: (i, index) => index % 4 || !index ? undefined : i + index,
40+
hole: (index) => index % 4 || !index,
41+
push: false,
42+
},
43+
large: {
44+
tagger: "symbol",
45+
make: `const array = new Array(1200); array[0] = i; array[1199] = i + 1; return array;`,
46+
length: 1200,
47+
element: (i, index) => !index ? i : index === 1199 ? i + 1 : undefined,
48+
hole: (index) => index && index !== 1199,
49+
indices: [0, 1, 600, 1198, 1199],
50+
},
51+
copyOnWrite: {
52+
tagger: "nine",
53+
make: `return [1, 2, 3];`,
54+
length: 3,
55+
element: (i, index) => index + 1,
56+
},
57+
arrayStorage: {
58+
tagger: "checked",
59+
hot: true,
60+
make: `const array = []; ensureArrayStorage(array); for (let k = 8; k--;) array.unshift(i + k); return array;`,
61+
length: 8,
62+
element: (i, index) => i + index,
63+
},
64+
object: {
65+
tagger: "nine",
66+
make: `const object = {}; object[0] = i; object[1] = "x"; object[2] = i + 2; return object;`,
67+
element: (i, index) => index === 1 ? "x" : i + index,
68+
indices: [0, 1, 2],
69+
},
70+
typedArray: {
71+
tagger: "symbol",
72+
make: `const array = new Uint16Array(4); array[1] = i; return array;`,
73+
length: 4,
74+
element: (i, index) => index === 1 ? i & 0xffff : 0,
75+
},
76+
};
77+
78+
const taggers = {
79+
one: {
80+
tag: `target.tag = value;`,
81+
check(target, value) {
82+
shouldBe(target.tag, value);
83+
},
84+
},
85+
checked: {
86+
tag: `if (target.tag !== undefined) return null; target.tag = value;`,
87+
check(target, value) {
88+
shouldBe(target.tag, value);
89+
},
90+
},
91+
symbol: {
92+
tag: `target[symbol] = value;`,
93+
check(target, value) {
94+
shouldBe(target[symbol], value);
95+
},
96+
},
97+
nine: {
98+
tag: `target.p0 = value; target.p1 = value + 1; target.p2 = value + 2; target.p3 = value + 3; target.p4 = value + 4; target.p5 = value + 5; target.p6 = value + 6; target.p7 = value + 7; target.p8 = value + 8;`,
99+
check(target, value) {
100+
shouldBe(target.p0, value);
101+
shouldBe(target.p1, value + 1);
102+
shouldBe(target.p2, value + 2);
103+
shouldBe(target.p3, value + 3);
104+
shouldBe(target.p4, value + 4);
105+
shouldBe(target.p5, value + 5);
106+
shouldBe(target.p6, value + 6);
107+
shouldBe(target.p7, value + 7);
108+
shouldBe(target.p8, value + 8);
109+
},
110+
},
111+
};
112+
113+
let counter = 0;
114+
function compile(parameters, body) {
115+
const result = new Function("symbol", `return function (${parameters}) { ${body} /* ${counter++} */ };`)(symbol);
116+
noInline(result);
117+
return result;
118+
}
119+
120+
function checkElements(maker, target, i) {
121+
shouldBe(target.length, maker.length);
122+
const indices = maker.indices;
123+
const count = indices ? indices.length : maker.length;
124+
for (let k = 0; k < count; ++k) {
125+
const index = indices ? indices[k] : k;
126+
shouldBe(target[index], maker.element(i, index));
127+
if (maker.hole)
128+
shouldBe(index in target, !maker.hole(index));
129+
}
130+
}
131+
132+
function run(maker, tagger, make, tag, iterations) {
133+
const push = maker.push !== false && Array.isArray(make(0));
134+
const kept = [];
135+
for (let i = 0; i < iterations; ++i) {
136+
const target = tag(make(i), i);
137+
if (!(i & 63))
138+
kept.push(target, i);
139+
tagger.check(target, i);
140+
if (i < 64 || !(i & 15))
141+
checkElements(maker, target, i);
142+
if (push) {
143+
target.push(i);
144+
shouldBe(target.length, maker.length + 1);
145+
shouldBe(target[maker.length], i);
146+
}
147+
}
148+
for (let k = 0; k < kept.length; k += 2) {
149+
const target = kept[k];
150+
const i = kept[k + 1];
151+
tagger.check(target, i);
152+
if (push)
153+
target.pop();
154+
checkElements(maker, target, i);
155+
}
156+
}
157+
noInline(run);
158+
159+
const few = Math.max(Math.ceil(testLoopCount / 16), 60);
160+
161+
const shared = {};
162+
for (const tagName of ["one", "symbol", "nine"])
163+
shared[tagName] = compile("target, value", `${taggers[tagName].tag} return target;`);
164+
165+
for (const makeName in makers) {
166+
const maker = makers[makeName];
167+
const make = compile("i", maker.make);
168+
const tagger = taggers[maker.tagger];
169+
run(maker, tagger, make, compile("target, value", `${tagger.tag} return target;`), maker.hot ? testLoopCount : few);
170+
if (makeName === "copyOnWrite")
171+
continue;
172+
for (const tagName in shared)
173+
run(maker, taggers[tagName], make, shared[tagName], few);
174+
}

‎Source/JavaScriptCore/bytecode/AccessCase.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ RefPtr<AccessCase> AccessCase::createTransition(
246246
return nullptr;
247247

248248
// If we are (re)allocating inline, jsc needs two extra scratchGPRs
249-
if (!oldStructure->couldHaveIndexingHeader() && GPRInfo::numberOfRegisters < (requiredRegisters + 2))
249+
if (oldStructure->canAllocatePropertyStorageInJIT() && GPRInfo::numberOfRegisters < (requiredRegisters + 2))
250250
return nullptr;
251251
}
252252

@@ -991,7 +991,7 @@ bool AccessCase::doesCalls(VM&) const
991991
case IndexedNullKeyTransition:
992992
case IndexedTrueKeyTransition:
993993
case IndexedFalseKeyTransition:
994-
doesCalls = newStructure()->outOfLineCapacity() != structure()->outOfLineCapacity() && structure()->couldHaveIndexingHeader();
994+
doesCalls = newStructure()->outOfLineCapacity() != structure()->outOfLineCapacity() && !structure()->canAllocatePropertyStorageInJIT();
995995
break;
996996
case Getter:
997997
case Setter:

‎Source/JavaScriptCore/bytecode/InlineCacheCompiler.cpp‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3655,7 +3655,7 @@ void InlineCacheCompiler::generateAccessCase(unsigned index, AccessCase& accessC
36553655
// exactly when this would make calls.
36563656
bool allocating = accessCase.newStructure()->outOfLineCapacity() != accessCase.structure()->outOfLineCapacity();
36573657
bool reallocating = allocating && accessCase.structure()->outOfLineCapacity();
3658-
bool allocatingInline = allocating && !accessCase.structure()->couldHaveIndexingHeader();
3658+
bool allocatingInline = allocating && accessCase.structure()->canAllocatePropertyStorageInJIT();
36593659

36603660
auto allocator = makeDefaultScratchAllocator(scratchGPR);
36613661

@@ -3677,10 +3677,25 @@ void InlineCacheCompiler::generateAccessCase(unsigned index, AccessCase& accessC
36773677
size_t newSize = accessCase.newStructure()->outOfLineCapacity() * sizeof(JSValue);
36783678

36793679
if (allocatingInline) {
3680-
Allocator allocator = vm.auxiliarySpace().allocatorForNonInline(newSize, AllocatorForMode::AllocatorIfExists);
3680+
if (hasIndexedProperties(accessCase.structure()->indexingType())) {
3681+
jit.loadPtr(CCallHelpers::Address(baseGPR, JSObject::butterflyOffset()), scratchGPR2);
3682+
jit.load32(CCallHelpers::Address(scratchGPR2, Butterfly::offsetOfVectorLength()), scratchGPR2);
3683+
jit.lshift64(CCallHelpers::TrustedImm32(3), scratchGPR2);
3684+
jit.add64(CCallHelpers::TrustedImm32(newSize + sizeof(IndexingHeader)), scratchGPR2);
3685+
jit.emitAllocateVariableSized(scratchGPR, vm.auxiliarySpace(), scratchGPR2, scratchGPR2, scratchGPR3, slowPath);
3686+
jit.addPtr(CCallHelpers::TrustedImm32(newSize + sizeof(IndexingHeader)), scratchGPR);
36813687

3682-
jit.emitAllocate(scratchGPR, JITAllocator::constant(allocator), scratchGPR2, scratchGPR3, slowPath);
3683-
jit.addPtr(CCallHelpers::TrustedImm32(newSize + sizeof(IndexingHeader)), scratchGPR);
3688+
jit.loadPtr(CCallHelpers::Address(baseGPR, JSObject::butterflyOffset()), scratchGPR3);
3689+
jit.load32(CCallHelpers::Address(scratchGPR3, Butterfly::offsetOfVectorLength()), scratchGPR2);
3690+
auto loop = jit.label();
3691+
jit.transfer64(CCallHelpers::BaseIndex(scratchGPR3, scratchGPR2, CCallHelpers::TimesEight, -static_cast<ptrdiff_t>(sizeof(IndexingHeader))), CCallHelpers::BaseIndex(scratchGPR, scratchGPR2, CCallHelpers::TimesEight, -static_cast<ptrdiff_t>(sizeof(IndexingHeader))));
3692+
jit.branchSub32(CCallHelpers::PositiveOrZero, CCallHelpers::TrustedImm32(1), scratchGPR2).linkTo(loop, &jit);
3693+
} else {
3694+
Allocator allocator = vm.auxiliarySpace().allocatorForNonInline(newSize, AllocatorForMode::AllocatorIfExists);
3695+
3696+
jit.emitAllocate(scratchGPR, JITAllocator::constant(allocator), scratchGPR2, scratchGPR3, slowPath);
3697+
jit.addPtr(CCallHelpers::TrustedImm32(newSize + sizeof(IndexingHeader)), scratchGPR);
3698+
}
36843699

36853700
size_t oldSize = accessCase.structure()->outOfLineCapacity() * sizeof(JSValue);
36863701
ASSERT(newSize > oldSize);
@@ -3708,8 +3723,7 @@ void InlineCacheCompiler::generateAccessCase(unsigned index, AccessCase& accessC
37083723
}
37093724
}
37103725

3711-
for (size_t offset = oldSize; offset < newSize; offset += sizeof(void*))
3712-
jit.storePtr(CCallHelpers::TrustedImmPtr(nullptr), CCallHelpers::Address(scratchGPR, -static_cast<ptrdiff_t>(offset + sizeof(JSValue) + sizeof(void*))));
3726+
jit.emitFillStorageWithJSEmpty(scratchGPR, -static_cast<ptrdiff_t>(newSize + sizeof(IndexingHeader)), (newSize - oldSize) / sizeof(JSValue), scratchGPR2);
37133727
} else {
37143728
// Handle the case where we are allocating out-of-line using an operation.
37153729
RegisterSet extraRegistersToPreserve;
@@ -3772,8 +3786,6 @@ void InlineCacheCompiler::generateAccessCase(unsigned index, AccessCase& accessC
37723786
}
37733787

37743788
if (allocatingInline) {
3775-
// If we were to have any indexed properties, then we would need to update the indexing mask on the base object.
3776-
RELEASE_ASSERT(!accessCase.newStructure()->couldHaveIndexingHeader());
37773789
// We set the new butterfly and the structure last. Doing it this way ensures that
37783790
// whatever we had done up to this point is forgotten if we choose to branch to slow
37793791
// path.

‎Source/JavaScriptCore/dfg/DFGSpeculativeJIT.cpp‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10711,11 +10711,57 @@ void SpeculativeJIT::compileCheckStructure(Node* node)
1071110711
}
1071210712
}
1071310713

10714+
void SpeculativeJIT::compileAllocatePropertyStorageWithIndexingHeader(Node* node)
10715+
{
10716+
size_t oldSize = node->transition()->previous->outOfLineCapacity() * sizeof(JSValue);
10717+
size_t newSize = node->transition()->next->outOfLineCapacity() * sizeof(JSValue);
10718+
10719+
SpeculateCellOperand base(this, node->child1());
10720+
GPRTemporary result(this);
10721+
GPRTemporary scratch1(this);
10722+
GPRTemporary scratch2(this);
10723+
10724+
GPRReg baseGPR = base.gpr();
10725+
GPRReg resultGPR = result.gpr();
10726+
GPRReg scratchGPR1 = scratch1.gpr();
10727+
GPRReg scratchGPR2 = scratch2.gpr();
10728+
10729+
loadPtr(Address(baseGPR, JSObject::butterflyOffset()), scratchGPR1);
10730+
load32(Address(scratchGPR1, Butterfly::offsetOfVectorLength()), scratchGPR1);
10731+
lshift64(TrustedImm32(3), scratchGPR1);
10732+
add64(TrustedImm32(newSize + sizeof(IndexingHeader)), scratchGPR1);
10733+
10734+
JumpList slowPath;
10735+
emitAllocateVariableSized(resultGPR, vm().auxiliarySpace(), scratchGPR1, scratchGPR1, scratchGPR2, slowPath, SlowAllocationResult::UndefinedBehavior);
10736+
addPtr(TrustedImm32(newSize + sizeof(IndexingHeader)), resultGPR);
10737+
10738+
emitFillStorageWithJSEmpty(resultGPR, -static_cast<ptrdiff_t>(newSize + sizeof(IndexingHeader)), (newSize - oldSize) / sizeof(JSValue), scratchGPR1);
10739+
10740+
loadPtr(Address(baseGPR, JSObject::butterflyOffset()), scratchGPR1);
10741+
for (ptrdiff_t offset = 0; offset < static_cast<ptrdiff_t>(oldSize); offset += sizeof(void*))
10742+
transferPtr(Address(scratchGPR1, -(offset + sizeof(JSValue) + sizeof(void*))), Address(resultGPR, -(offset + sizeof(JSValue) + sizeof(void*))));
10743+
10744+
load32(Address(scratchGPR1, Butterfly::offsetOfVectorLength()), scratchGPR2);
10745+
Label loop = label();
10746+
transfer64(BaseIndex(scratchGPR1, scratchGPR2, TimesEight, -static_cast<ptrdiff_t>(sizeof(IndexingHeader))), BaseIndex(resultGPR, scratchGPR2, TimesEight, -static_cast<ptrdiff_t>(sizeof(IndexingHeader))));
10747+
branchSub32(PositiveOrZero, TrustedImm32(1), scratchGPR2).linkTo(loop, this);
10748+
10749+
addSlowPathGenerator(
10750+
slowPathCall(slowPath, this, operationAllocateComplexPropertyStorage, resultGPR, TrustedImmPtr(&vm()), baseGPR, newSize / sizeof(JSValue)));
10751+
10752+
storageResult(resultGPR, node);
10753+
}
10754+
1071410755
void SpeculativeJIT::compileAllocatePropertyStorage(Node* node)
1071510756
{
1071610757
ASSERT(!node->transition()->previous->outOfLineCapacity());
1071710758
ASSERT(initialOutOfLineCapacity == node->transition()->next->outOfLineCapacity());
1071810759

10760+
if (node->transition()->previous->couldHaveIndexingHeader() && node->transition()->previous->canAllocatePropertyStorageInJIT()) {
10761+
compileAllocatePropertyStorageWithIndexingHeader(node);
10762+
return;
10763+
}
10764+
1071910765
size_t size = initialOutOfLineCapacity * sizeof(JSValue);
1072010766

1072110767
Allocator allocator = vm().auxiliarySpace().allocatorFor(size, AllocatorForMode::AllocatorIfExists);
@@ -10761,6 +10807,11 @@ void SpeculativeJIT::compileReallocatePropertyStorage(Node* node)
1076110807
size_t newSize = oldSize * outOfLineGrowthFactor;
1076210808
ASSERT(newSize == node->transition()->next->outOfLineCapacity() * sizeof(JSValue));
1076310809

10810+
if (node->transition()->previous->couldHaveIndexingHeader() && node->transition()->previous->canAllocatePropertyStorageInJIT()) {
10811+
compileAllocatePropertyStorageWithIndexingHeader(node);
10812+
return;
10813+
}
10814+
1076410815
Allocator allocator = vm().auxiliarySpace().allocatorFor(newSize, AllocatorForMode::AllocatorIfExists);
1076510816

1076610817
if (!allocator || node->transition()->previous->couldHaveIndexingHeader()) {

‎Source/JavaScriptCore/dfg/DFGSpeculativeJIT.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,6 +1347,7 @@ class SpeculativeJIT : public JITCompiler {
13471347

13481348
void compileSameValue(Node*);
13491349

1350+
void compileAllocatePropertyStorageWithIndexingHeader(Node*);
13501351
void compileAllocatePropertyStorage(Node*);
13511352
void compileReallocatePropertyStorage(Node*);
13521353
void compileNukeStructureAndSetButterfly(Node*);

0 commit comments

Comments
 (0)