From 11a3ee42f95e64573050139aeb3c6d1935a759d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Fri, 25 Sep 2020 15:53:03 +0200 Subject: [PATCH 1/3] Unwrap invoke_function --- lib/fizzy/execute.cpp | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/fizzy/execute.cpp b/lib/fizzy/execute.cpp index 673a5e0a1..57a807602 100644 --- a/lib/fizzy/execute.cpp +++ b/lib/fizzy/execute.cpp @@ -509,10 +509,26 @@ inline bool invoke_function(const FuncType& func_type, const F& func, Instance& inline bool invoke_function(const FuncType& func_type, uint32_t func_idx, Instance& instance, OperandStack& stack, int depth) noexcept { - const auto func = [func_idx](Instance& _instance, span args, int _depth) noexcept { - return execute(_instance, func_idx, args.data(), _depth); - }; - return invoke_function(func_type, func, instance, stack, depth); + const auto num_args = func_type.inputs.size(); + assert(stack.size() >= num_args); + span call_args{stack.rend() - num_args, num_args}; + + const auto ret = execute(instance, func_idx, call_args.data(), depth + 1); + // Bubble up traps + if (ret.trapped) + return false; + + stack.drop(num_args); + + const auto num_outputs = func_type.outputs.size(); + // NOTE: we can assume these two from validation + assert(num_outputs <= 1); + assert(ret.has_value == (num_outputs == 1)); + // Push back the result + if (num_outputs != 0) + stack.push(ret.value); + + return true; } } // namespace From f9d4dbf515128cd97ab05cc4422dbf71db481eb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Fri, 25 Sep 2020 15:57:00 +0200 Subject: [PATCH 2/3] Optimize result push --- lib/fizzy/execute.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/fizzy/execute.cpp b/lib/fizzy/execute.cpp index 57a807602..e2319b6f3 100644 --- a/lib/fizzy/execute.cpp +++ b/lib/fizzy/execute.cpp @@ -495,12 +495,11 @@ inline bool invoke_function(const FuncType& func_type, const F& func, Instance& stack.drop(num_args); - const auto num_outputs = func_type.outputs.size(); // NOTE: we can assume these two from validation - assert(num_outputs <= 1); - assert(ret.has_value == (num_outputs == 1)); + assert(func_type.outputs.size() <= 1); + assert(ret.has_value == !func_type.outputs.empty()); // Push back the result - if (num_outputs != 0) + if (ret.has_value != 0) stack.push(ret.value); return true; From 9eaffe6521e12b8ced30265b0b870497597ea308 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Mon, 5 Oct 2020 16:04:24 +0100 Subject: [PATCH 3/3] Further optimisation --- lib/fizzy/execute.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/fizzy/execute.cpp b/lib/fizzy/execute.cpp index e2319b6f3..00d14a74c 100644 --- a/lib/fizzy/execute.cpp +++ b/lib/fizzy/execute.cpp @@ -510,9 +510,8 @@ inline bool invoke_function(const FuncType& func_type, uint32_t func_idx, Instan { const auto num_args = func_type.inputs.size(); assert(stack.size() >= num_args); - span call_args{stack.rend() - num_args, num_args}; - const auto ret = execute(instance, func_idx, call_args.data(), depth + 1); + const auto ret = execute(instance, func_idx, stack.rend() - num_args, depth + 1); // Bubble up traps if (ret.trapped) return false;