all*_*tom 12 profiling haskell
我正在寻找我的Haskell程序中的优化机会,通过编译-prof,但我不知道如何解释包含省略号的成本中心.什么是filter.(...)和jankRoulette.select.(...)?
COST CENTRE MODULE %time %alloc
filter.(...) Forest 46.5 22.3
set-union Forest 22.5 4.1
cache-lookup Forest 16.0 0.1
removeMany MultiMapSet 3.7 1.9
insertMany MultiMapSet 3.3 1.8
jankRoulette.select.(...) Forest 1.4 15.2
Run Code Online (Sandbox Code Playgroud)
我通过以下方式生成: $ ghc --make -rtsopts -prof -auto-all main.hs && ./main +RTS -p && cat main.prof
该函数filter在where子句中有一些定义,如下所示:
filter a b = blahblah where
foo = bar
bar = baz
baz = bing
Run Code Online (Sandbox Code Playgroud)
但是,这些都显示为filter.foo,filter.bar等等.
我以为它们可能是嵌套的表达式,但jankRoulette.select没有任何表达式.我在其中大多数前面添加了SCC指令,而没有任何成本中心升至顶峰.
由于大部分时间都花在了filter.(...),我想知道那是什么.:)
TL; DR: GHC会在let绑定中执行模式匹配时生成此项,例如let (x,y) = c.成本中心c跟踪评估...成本(因为它没有唯一的名称)`.
那我是怎么发现的呢?(...)GHC源代码中的grep for 找到以下内容(来自compiler/deSugar/Coverage.hs):
-- TODO: Revisit this
addTickLHsBind (L pos (pat@(PatBind { pat_lhs = lhs, pat_rhs = rhs }))) = do
let name = "(...)"
(fvs, rhs') <- getFreeVars $ addPathEntry name $ addTickGRHSs False False rhs
{- ... more code following, but not relevant to this purpose
-}
Run Code Online (Sandbox Code Playgroud)
该代码告诉我们它必须对模式绑定做一些事情.所以我们可以制作一个小的测试程序来检查行为:
x :: Int
(x:_) = reverse [1..1000000000]
main :: IO ()
main = print x
Run Code Online (Sandbox Code Playgroud)
然后,我们可以在启用分析的情况下运行此程序.确实,GHC生成以下输出:
COST CENTRE MODULE no. entries %time %alloc %time
%alloc
MAIN MAIN 42 0 0.0 0.0 100.0 100.0
CAF Main 83 0 0.0 0.0 100.0 100.0
(...) Main 86 1 100.0 100.0 100.0 100.0
x Main 85 1 0.0 0.0 0.0 0.0
main Main 84 1 0.0 0.0 0.0 0.0
Run Code Online (Sandbox Code Playgroud)
事实证明,从代码中做出的假设是正确的.程序的所有时间都用于评估reverse [1..1000000000]表达式,并将其分配给(...)成本中心.