Improve performance of Simplify.simplCast
Splitting off task 3 from #11735. When compiling https://ghc.haskell.org/trac/ghc/attachment/ticket/14683/Grammar.hs,
simplCast eats up more execution time than we think it should.
Something is clearly wrong with
Simplify.simplCast. I think I know what it is. Given
(fun |> co) @t1 @t2 ... @tn
we will call
ntimes, and hence does
nsingleton substitutions, via the
Solution: gather up those type arguments (easy) and define
pushCoTyArgs :: Coercion -> [Type] -> Maybe ([Type], Coercion)
OK. I looked at
pushCoTyArgand friends, and I have a very simple solution: just move the
addCoerce(a local function within
Simplify.simplCast) to the top. That should do it. Then
pushCoTyArgis never called with a reflexive coercion, and so the
piResultTycase won't happen.
pushCoArgsmight still call
pushCoTyArgwith a reflexive coercion, but it can be taught not to as well: Have
Maybe ([CoreArg], Maybe Coercion)and
Maybe (CoreArg, Maybe Coercion). If the second return values are
Nothing, that means that there is no cast (i.e., that the cast would have been reflexive). The only client of
exprIsConApp_maybe, which simply omits a cast if
Nothing. Then, we never have to bother creating the reflexive coercions.
This should be an easy win all around.
|CC||goldfire, simonpj, tdammers|