-
Notifications
You must be signed in to change notification settings - Fork 465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inefficient code emitted for pattern match on unboxed variants #7121
Comments
Once the conditional is expressed in this order, no further simplification is possible (the cases are exhaustive only if you carry around the type, which you don't). So an intervention needs to happen at the point where the conditional is generated. If one writes the conditional by hand, with variations on negation, order, etc, it would be interesting to see which cases are problematic. |
All the So one easy possibility would be to compile selected switch statements to @cknitt have you observed similar, but not identical cases? |
Here's another example: @unboxed
type rec t =
| Boolean(bool)
| @as(null) Null
| @as(undefined) Undefined
| String(string)
| Number(float)
| Object(Js_dict.t<t>)
| Array(array<t>)
let f = (x: t) =>
switch x {
| Null | Undefined => Console.log("abc")
| _ => ()
} It seems to happen when all the nullary cases have the same rhs. |
I cannot say for sure. I have observed this behavior (or similar?) a few times before creating this issue, but unfortunately did not collect the exact code involved. |
But even with different rhs, @unboxed
type rec t =
| Boolean(bool)
| @as(null) Null
| @as(undefined) Undefined
| String(string)
| Number(float)
| Object(Js_dict.t<t>)
| Array(array<t>)
let f = (x: t) =>
switch x {
| Null => Console.log("abc")
| Undefined => Console.log("def")
| _ => ()
} yields function f(x) {
if (!(!Array.isArray(x) && (x === null || typeof x !== "object") && typeof x !== "number" && typeof x !== "string" && typeof x !== "boolean")) {
return ;
}
if (x === null) {
console.log("abc");
return ;
}
console.log("def");
} but could be function f(x) {
if (x === null) {
console.log("abc");
}
else if (x === undefined) {
console.log("def");
}
} ? |
OK the behaviour makes sense. With unboxed, there are 2 possibilities of saying the same thing:
In these examples, 2 would be better. @cknitt thoughts? |
is compiled to
Ideally, this would just be
The text was updated successfully, but these errors were encountered: