Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions lib/fizzy/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,15 @@ std::optional<uint32_t> find_export(const Module& module, ExternalKind kind, std
return (it != module.exportsec.end() ? std::make_optional(it->index) : std::nullopt);
}

inline uint64_t& get_local(
std::vector<uint64_t>& args, std::vector<uint64_t>& locals, uint32_t idx) noexcept
{
if (idx < args.size())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without making any further optimisations, isn't this making it slower? Previously we copied arguments once.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much slower. See description.

return args[idx];
assert((idx - args.size()) <= locals.size());
return locals[idx - args.size()];
}

} // namespace

std::unique_ptr<Instance> instantiate(Module module,
Expand Down Expand Up @@ -641,8 +650,7 @@ execution_result execute(
const auto& code = instance.module.codesec[code_idx];
auto* const memory = instance.memory.get();

std::vector<uint64_t> locals = std::move(args);
locals.resize(locals.size() + code.local_count);
std::vector<uint64_t> locals(code.local_count, 0);

OperandStack stack(static_cast<size_t>(code.max_stack_height));

Expand Down Expand Up @@ -842,23 +850,17 @@ execution_result execute(
}
case Instr::local_get:
{
const auto idx = read<uint32_t>(immediates);
assert(idx <= locals.size());
stack.push(locals[idx]);
stack.push(get_local(args, locals, read<uint32_t>(immediates)));
break;
}
case Instr::local_set:
{
const auto idx = read<uint32_t>(immediates);
assert(idx <= locals.size());
locals[idx] = stack.pop();
get_local(args, locals, read<uint32_t>(immediates)) = stack.pop();
break;
}
case Instr::local_tee:
{
const auto idx = read<uint32_t>(immediates);
assert(idx <= locals.size());
locals[idx] = stack.top();
get_local(args, locals, read<uint32_t>(immediates)) = stack.top();
break;
}
case Instr::global_get:
Expand Down