Demangling typeclass functions in GHC profiler output
When profiling a Haskell program written in GHC, the names of typeclass functions are mangled in the .prof file to distinguish one instance's implementations of them from another. How can I demangle these names to find out which type's instance it is?
For example, suppose I have the following program, where types Fast
and Slow
both implement Show
:
import Data.List (foldl')
sum' = foldl' (+) 0
data Fast = Fast
instance Show Fast where
show _ = show $ sum' [1 .. 10]
data Slow = Slow
instance Show Slow where
show _ = show $ sum' [1 .. 100000000]
main = putStrLn (show Fast ++ show Slow)
I compile with -prof -auto-all -caf-all
and run with +RTS -p
. In the .prof file that gets generated, I see that the top cost centers are:
COST CENTRE M开发者_StackOverflowODULE %time %alloc
show_an9 Main 71.0 83.3
sum' Main 29.0 16.7
And in the tree, I likewise see (omitting irrelevant lines):
individual inherited
COST CENTRE MODULE no. entries %time %alloc %time %alloc
main Main 232 1 0.0 0.0 100.0 100.0
show_an9 Main 235 1 71.0 83.3 100.0 100.0
sum' Main 236 0 29.0 16.7 29.0 16.7
show_anx Main 233 1 0.0 0.0 0.0 0.0
How do I figure out that show_an9
is Slow
's implementation of show
and not Fast
's?
No you can't. The _an9
and _anx
parts are randomly generated. (When I compile again I got _ane
and _anC
.)
You could use the SCC
(set-cost-center) pragma to insert a cost center manually:
data Fast = Fast
instance Show Fast where
show _ = {-# SCC "show(Fast)" #-} show $ sum' [1 .. 10]
data Slow = Slow
instance Show Slow where
show _ = {-# SCC "show(Slow)" #-} show $ sum' [1 .. 100000000]
The profile should show:
main
show_an9
show(Slow)
sum'
show_anx
show(Fast)
精彩评论