Something is amiss with fusion in UniqSet construction
While looking at the Core of GHC.CmmToAsm.Reg.Liveness
I noticed that we weren't deforesting intermediate lists which are passed to mkUniqSet
. For instance, consider the program:
module Hi where
import GHC.Types.Unique
import GHC.Types.Unique.Set
f :: [Unique] -> UniqSet
f xs = mkUniqSet $ filter (even . getKey) xs
In principle, this should compile to a fold over xs
, inserting into the accumulator only those elements which are even
. Indeed, if we write this same program in terms of IntSet
this is precisely what happens:
module Hi where
import Data.Foldable
import qualified Data.IntSet as IS
f :: [Int] -> IS.IntSet
f xs = foldl' (flip IS.insert) IS.empty $ filter even xs
However, for some reason this fusion does not occur with mkUniqSet
, despite the latter being defined in terms of foldl'
We rather end up producing an intermediate filter
'd list.
It would be good to understand why we currently miss this opportunity.