From a9a629f89a1d72130103bd59800e1257af84c97c Mon Sep 17 00:00:00 2001 From: Luuk de Gram Date: Tue, 22 Mar 2022 21:56:38 +0100 Subject: [PATCH] wasm: Fix switching on errors Error sets contain the entire global error set. Users are often switching on specific errors only present within that operand. This means that cases are almost always sparse and not contiguous. For this reason, we will instead emit the default case for error values not present in that specific operand error set. This is fine as those cases will never be hit, as prevented by the type system. By still allowing jump tables for those cases, rather than if-else chains, we save runtime cost as well as binary size. --- src/arch/wasm/CodeGen.zig | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/arch/wasm/CodeGen.zig b/src/arch/wasm/CodeGen.zig index 136ef5cc45..0300c0b612 100644 --- a/src/arch/wasm/CodeGen.zig +++ b/src/arch/wasm/CodeGen.zig @@ -2452,7 +2452,11 @@ fn airSwitchBr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { if (case_value.integer == value) break :blk @intCast(u32, idx); } } - break :blk if (has_else_body) case_i else unreachable; + // error sets are almost always sparse so we use the default case + // for errors that are not present in any branch. This is fine as this default + // case will never be hit for those cases but we do save runtime cost and size + // by using a jump table for this instead of if-else chains. + break :blk if (has_else_body or target_ty.zigTypeTag() == .ErrorSet) case_i else unreachable; }; self.mir_extra.appendAssumeCapacity(idx); } else if (has_else_body) {