optimization - 是否有可能使 GHC 优化(砍伐)通用功能,例如变形?

标签 optimization haskell ghc fusion catamorphism

我真的很喜欢以通用方式处理变形/变形的想法,但在我看来它有一个显着的性能缺陷:

假设我们想要以分类方式使用树结构 - 使用通用 catamorphism function 来描述不同的折叠:

newtype Fix f = Fix { unfix :: f (Fix f) }

data TreeT r = Leaf | Tree r r
instance Functor TreeT where
    fmap f Leaf         = Leaf
    fmap f (Tree l r)   = Tree (f l) (f r)

type Tree = Fix TreeT

catam :: (Functor f) => (f a -> a) -> (Fix f -> a)
catam f = f . fmap (catam f) . unfix

现在我们可以编写如下函数:

depth1 :: Tree -> Int
depth1 = catam g
  where
    g Leaf       = 0
    g (Tree l r) = max l r

不幸的是,这种方法有一个显着的缺点:在计算过程中,TreeT Int 的新实例会在 fmap 的每个级别创建,以便立即被 使用>g。与经典定义相比

depth2 :: Tree -> Int
depth2 (Fix Leaf) = 0
depth2 (Fix (Tree l r)) = max (depth1 l) (depth1 r)

我们的深度1总是会变慢,给GC带来不必要的压力。一种解决方案是使用 hylomorphisms并将创造树和折叠树结合在一起。但通常我们不想这样做,我们可能希望在一个地方创建一棵树,然后传递到其他地方以便稍后折叠。或者,以不同的变形来多次文件夹。

有没有办法让GHC优化深度1?类似于内联 catam g 然后 fusing/deforesting g . fmap ... 里面?

最佳答案

我相信我找到了答案。我记得读过Why does GHC make fix so confounding?这给了我一个解决方案。

catam 之前定义的问题在于它是递归的,因此任何尝试 INLINE它被忽略了。使用 -ddump-simpl -ddump-to-file 编译原始版本并读取 core :

Main.depth1 = Main.catam_$scatam @ GHC.Types.Int Main.depth3

Main.depth3 =
  \ (ds_dyI :: Main.TreeT GHC.Types.Int) ->
    case ds_dyI of _ {
      Main.Leaf -> Main.depth4;
      Main.Tree l_aah r_aai -> GHC.Classes.$fOrdInt_$cmax l_aah r_aai
    }

Main.depth4 = GHC.Types.I# 0

Rec {
Main.catam_$scatam =
  \ (@ a_ajB)
    (eta_B1 :: Main.TreeT a_ajB -> a_ajB)
    (eta1_X2 :: Main.Fix Main.TreeT) ->
    eta_B1
      (case eta1_X2
            `cast` (Main.NTCo:Fix <Main.TreeT>
                    :: Main.Fix Main.TreeT ~# Main.TreeT (Main.Fix Main.TreeT))
       of _ {
         Main.Leaf -> Main.Leaf @ a_ajB;
         Main.Tree l_aan r_aao ->
           Main.Tree
             @ a_ajB
             (Main.catam_$scatam @ a_ajB eta_B1 l_aan)
             (Main.catam_$scatam @ a_ajB eta_B1 r_aao)
       })
end Rec }

相比,

显然更糟糕(catam_$scatam中的构造函数创建/消除,更多的函数调用)

Main.depth2 =
  \ (w_s1Rz :: Main.Tree) ->
    case Main.$wdepth2 w_s1Rz of ww_s1RC { __DEFAULT ->
    GHC.Types.I# ww_s1RC
    }

Rec {
Main.$wdepth2 [Occ=LoopBreaker] :: Main.Tree -> GHC.Prim.Int#
[GblId, Arity=1, Caf=NoCafRefs, Str=DmdType S]
Main.$wdepth2 =
  \ (w_s1Rz :: Main.Tree) ->
    case w_s1Rz
         `cast` (Main.NTCo:Fix <Main.TreeT>
                 :: Main.Fix Main.TreeT ~# Main.TreeT (Main.Fix Main.TreeT))
    of _ {
      Main.Leaf -> 0;
      Main.Tree l_aaj r_aak ->
        case Main.$wdepth2 l_aaj of ww_s1RC { __DEFAULT ->
        case Main.$wdepth2 r_aak of ww1_X1Sh { __DEFAULT ->
        case GHC.Prim.<=# ww_s1RC ww1_X1Sh of _ {
          GHC.Types.False -> ww_s1RC;
          GHC.Types.True -> ww1_X1Sh
        }
        }
        }
    }
end Rec }

但是如果我们将catam定义为

{-# INLINE catam #-}
catam :: (Functor f) => (f a -> a) -> (Fix f -> a)
catam f = let u = f . fmap u . unfix
          in u

那么它就不再是递归的,只有里面的u是递归的。这样,GHC 将 catam 内联到 depth1 的定义中,并将 fmapdepth1g - 正是我们想要的:

Main.depth1 =
  \ (w_s1RJ :: Main.Tree) ->
    case Main.$wdepth1 w_s1RJ of ww_s1RM { __DEFAULT ->
    GHC.Types.I# ww_s1RM
    }

Rec {
Main.$wdepth1 [Occ=LoopBreaker] :: Main.Tree -> GHC.Prim.Int#
[GblId, Arity=1, Caf=NoCafRefs, Str=DmdType S]
Main.$wdepth1 =
  \ (w_s1RJ :: Main.Tree) ->
    case w_s1RJ
         `cast` (Main.NTCo:Fix <Main.TreeT>
                 :: Main.Fix Main.TreeT ~# Main.TreeT (Main.Fix Main.TreeT))
    of _ {
      Main.Leaf -> 0;
      Main.Tree l_aar r_aas ->
        case Main.$wdepth1 l_aar of ww_s1RM { __DEFAULT ->
        case Main.$wdepth1 r_aas of ww1_X1So { __DEFAULT ->
        case GHC.Prim.<=# ww_s1RM ww1_X1So of _ {
          GHC.Types.False -> ww_s1RM;
          GHC.Types.True -> ww1_X1So
        }
        }
        }
    }
end Rec }

现在与 深度2 的转储相同。

关于optimization - 是否有可能使 GHC 优化(砍伐)通用功能,例如变形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13099203/

相关文章:

python - 优化超大 csv 文件中的搜索

haskell - 使用 Writer monad 写入文件时如何避免内存问题?

haskell - 为什么函数的结果没有被重用?

haskell - GHC 无需 Root 即可安装

javascript - 正则表达式测试,有更好的方法来写这个吗?

python - Python 中的优化——该做的、不该做的和经验法则

haskell - 跟踪 ghci 中的主要功能

haskell - 'MutVar#'是什么意思?

c - 有没有办法让这个功能更快? (C)

haskell - 如何简洁地修改记录的字段?