mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 10:18:11 +00:00
LibJS: Instantiate primitive array expressions using a single operation
This will not meaningfully affect short array literals, but it does give us a bit of extra perf when evaluating huge array expressions like in Kraken/imaging-darkroom.js
This commit is contained in:
parent
5e3a799e97
commit
f19349e1b6
7 changed files with 92 additions and 6 deletions
|
@ -1012,6 +1012,19 @@ Bytecode::CodeGenerationErrorOr<void> ArrayExpression::generate_bytecode(Bytecod
|
|||
return {};
|
||||
}
|
||||
|
||||
if (all_of(m_elements, [](auto element) { return !element || is<PrimitiveLiteral>(*element); })) {
|
||||
// If all elements are constant primitives, we can just emit a single instruction to initialize the array,
|
||||
// instead of emitting instructions to manually evaluate them one-by-one
|
||||
auto values = MUST(FixedArray<Value>::create(m_elements.size()));
|
||||
for (auto i = 0u; i < m_elements.size(); ++i) {
|
||||
if (!m_elements[i])
|
||||
continue;
|
||||
values[i] = static_cast<PrimitiveLiteral const&>(*m_elements[i]).value();
|
||||
}
|
||||
generator.emit<Bytecode::Op::NewPrimitiveArray>(move(values));
|
||||
return {};
|
||||
}
|
||||
|
||||
auto first_spread = find_if(m_elements.begin(), m_elements.end(), [](auto el) { return el && is<SpreadExpression>(*el); });
|
||||
|
||||
Bytecode::Register args_start_reg { 0 };
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue