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.
This commit is contained in:
Luuk de Gram 2022-03-22 21:56:38 +01:00
parent 49051c0651
commit a9a629f89a
No known key found for this signature in database
GPG Key ID: A8CFE58E4DC7D664

View File

@ -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) {