代码之家  ›  专栏  ›  技术社区  ›  Paul Kuliniewicz

GHC探查器输出中的demangling typeclass函数

  •  8
  • Paul Kuliniewicz  · 技术社区  · 14 年前

    当分析用ghc编写的haskell程序时,typeclass函数的名称在.prof文件中被篡改,以区分一个实例对它们的实现。我怎样才能把这些名字分开来找出它是哪种类型的实例?

    例如,假设我有以下程序,其中 Fast Slow 两实施 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)
    

    我编译 -prof -auto-all -caf-all 和一起跑 +RTS -p . 在生成的.prof文件中,我发现最重要的成本中心是:

    COST CENTRE                    MODULE               %time %alloc
    
    show_an9                       Main                  71.0   83.3
    sum'                           Main                  29.0   16.7
    

    在树上,我也看到了(省略无关的行):

                                                    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
    

    我怎么知道 show_an9 缓慢的 的实现 show 而不是 快的 是吗?

    1 回复  |  直到 14 年前
        1
  •  8
  •   kennytm    14 年前

    不,你不能。 _an9 _anx 零件是随机生成的。(当我再次编译时 _ane _anC )

    你可以用 SCC (设置成本中心)pragma手动插入成本中心:

    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]
    

    配置文件应显示:

      main
       show_an9
        show(Slow)
         sum'
       show_anx
        show(Fast)