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:
Diego Frias 2024-07-26 11:31:30 -07:00 committed by Andreas Kling
parent 1bd0871ed8
commit 5cde327d46
Notes: github-actions[bot] 2024-07-27 06:20:55 +00:00
2 changed files with 10 additions and 6 deletions

View file

@ -1049,13 +1049,15 @@ ParseResult<MemorySection> MemorySection::parse(Stream& stream)
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);
InstructionPointer ip { 0 };
Vector<InstructionPointer> stack;
Vector<Instruction> instructions;
if (size_hint.has_value())
instructions.ensure_capacity(size_hint.release_value());
while (true) {
auto instruction = TRY(Instruction::parse(stream));
switch (instruction.opcode().value()) {
@ -1239,11 +1241,11 @@ ParseResult<Locals> Locals::parse(Stream& stream)
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);
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 };
}
@ -1257,7 +1259,9 @@ ParseResult<CodeSection::Code> CodeSection::Code::parse(Stream& stream)
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 };
}

View file

@ -676,7 +676,7 @@ public:
auto& instructions() const { return m_instructions; }
static ParseResult<Expression> parse(Stream& stream);
static ParseResult<Expression> parse(Stream& stream, Optional<size_t> size_hint = {});
private:
Vector<Instruction> m_instructions;
@ -855,7 +855,7 @@ public:
auto& locals() const { return m_locals; }
auto& body() const { return m_body; }
static ParseResult<Func> parse(Stream& stream);
static ParseResult<Func> parse(Stream& stream, size_t size_hint);
private:
Vector<Locals> m_locals;