mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-09 09:39:39 +00:00
LibWasm: Provide size hint when parsing instructions into a vector
Speeds up spidermonkey.wasm instantiation by around 20ms (280ms -> 260ms)
This commit is contained in:
parent
1bd0871ed8
commit
5cde327d46
Notes:
github-actions[bot]
2024-07-27 06:20:55 +00:00
Author: https://github.com/dzfrias
Commit: 5cde327d46
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/850
2 changed files with 10 additions and 6 deletions
|
@ -1049,13 +1049,15 @@ ParseResult<MemorySection> MemorySection::parse(Stream& stream)
|
||||||
return MemorySection { memories };
|
return MemorySection { memories };
|
||||||
}
|
}
|
||||||
|
|
||||||
ParseResult<Expression> Expression::parse(Stream& stream)
|
ParseResult<Expression> Expression::parse(Stream& stream, Optional<size_t> size_hint)
|
||||||
{
|
{
|
||||||
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Expression"sv);
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Expression"sv);
|
||||||
|
|
||||||
InstructionPointer ip { 0 };
|
InstructionPointer ip { 0 };
|
||||||
Vector<InstructionPointer> stack;
|
Vector<InstructionPointer> stack;
|
||||||
Vector<Instruction> instructions;
|
Vector<Instruction> instructions;
|
||||||
|
if (size_hint.has_value())
|
||||||
|
instructions.ensure_capacity(size_hint.release_value());
|
||||||
while (true) {
|
while (true) {
|
||||||
auto instruction = TRY(Instruction::parse(stream));
|
auto instruction = TRY(Instruction::parse(stream));
|
||||||
switch (instruction.opcode().value()) {
|
switch (instruction.opcode().value()) {
|
||||||
|
@ -1239,11 +1241,11 @@ ParseResult<Locals> Locals::parse(Stream& stream)
|
||||||
return Locals { static_cast<u32>(count), type };
|
return Locals { static_cast<u32>(count), type };
|
||||||
}
|
}
|
||||||
|
|
||||||
ParseResult<CodeSection::Func> CodeSection::Func::parse(Stream& stream)
|
ParseResult<CodeSection::Func> CodeSection::Func::parse(Stream& stream, size_t size_hint)
|
||||||
{
|
{
|
||||||
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Func"sv);
|
ScopeLogger<WASM_BINPARSER_DEBUG> logger("Func"sv);
|
||||||
auto locals = TRY(parse_vector<Locals>(stream));
|
auto locals = TRY(parse_vector<Locals>(stream));
|
||||||
auto body = TRY(Expression::parse(stream));
|
auto body = TRY(Expression::parse(stream, size_hint));
|
||||||
return Func { locals, body };
|
return Func { locals, body };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1257,7 +1259,9 @@ ParseResult<CodeSection::Code> CodeSection::Code::parse(Stream& stream)
|
||||||
|
|
||||||
auto constrained_stream = ConstrainedStream { MaybeOwned<Stream>(stream), size };
|
auto constrained_stream = ConstrainedStream { MaybeOwned<Stream>(stream), size };
|
||||||
|
|
||||||
auto func = TRY(Func::parse(constrained_stream));
|
// Emprically, if there are `size` bytes to be read, then there's around
|
||||||
|
// `size / 2` instructions, so we pass that as our size hint.
|
||||||
|
auto func = TRY(Func::parse(constrained_stream, size / 2));
|
||||||
|
|
||||||
return Code { static_cast<u32>(size), func };
|
return Code { static_cast<u32>(size), func };
|
||||||
}
|
}
|
||||||
|
|
|
@ -676,7 +676,7 @@ public:
|
||||||
|
|
||||||
auto& instructions() const { return m_instructions; }
|
auto& instructions() const { return m_instructions; }
|
||||||
|
|
||||||
static ParseResult<Expression> parse(Stream& stream);
|
static ParseResult<Expression> parse(Stream& stream, Optional<size_t> size_hint = {});
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Vector<Instruction> m_instructions;
|
Vector<Instruction> m_instructions;
|
||||||
|
@ -855,7 +855,7 @@ public:
|
||||||
auto& locals() const { return m_locals; }
|
auto& locals() const { return m_locals; }
|
||||||
auto& body() const { return m_body; }
|
auto& body() const { return m_body; }
|
||||||
|
|
||||||
static ParseResult<Func> parse(Stream& stream);
|
static ParseResult<Func> parse(Stream& stream, size_t size_hint);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Vector<Locals> m_locals;
|
Vector<Locals> m_locals;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue