Open alexhumphreys opened 3 years ago
The case tree generated by Idris directly depends on the order in which the patterns are spelt out. If you spell out stricter patterns first that may lead to a lot of duplicated code in the "catchall" branches.
These RHS should perhaps be let-bound first so that the code can be shared.
Not sure I understand what you mean: Is let binding on RHS something I can do, or would that be a change to how the idris compiler handles pattern matching?
No, I mean in the generated code: when the same RHS is used for multiple branches of the case generated tree, we may want to introduce them as separate auxiliary definitions to avoid code duplication.
Self contained gist
Steps to Reproduce
run
idris2 CodeGen.idr --client ':di someFunc' | wc -c
for the following file (sorry it's still a bit long):There are 4 cases
someFunc xs _ (VListFold y z w v s)
insomeFunc
, one at the start, one at the middle and one at the end. Uncomment each one in turn and runidris2 CodeGen.idr --client ':di someFunc' | wc -c
for each.Expected Behavior
Similar sized chez output.
Observed Behavior
When the
someFunc xs _ (VListFold y z w v s)
is in the middle of the function, the outputted chez size increases by a factor of about 4 compared to if that case is at the start or the end. It seems to be caused by the_
match, because when an actualValue
is matched on it goes back to similar values to the others.Notes
This is a contrived example, and output size isn't the metric I care about, but I ran into in the wild this PR, where my tests slowed down from 15s to 40s based on where the case was in a function. There's more details at the PR, but the tests once they were running seemed just as fast, so it looks like the chez startup time increased as a result of the much larger output sizes.
Since this example is small it won't show the slow startup times, hence why I concentrated on output size, but the example at the certainly PR exhibits this behaviour.