简体   繁体   English

调试由GHC的约束求解器引起的编译时性能问题

[英]Debugging compile time performance issues caused by GHC's constraint solver

Haskell has many great tools for debugging run time performance issues, but what tools/rules of thumb exist for debugging compile time performance issues? Haskell有许多用于调试运行时性能问题的好工具,但是有哪些工具/经验法则可用于调试编译时性能问题?

Specifically, the constraint solver in some of my code is taking forever (anywhere from 1-2 seconds to several minutes). 具体来说,我的一些代码中的约束求解器将永远占用(从1-2秒到几分钟)。 I'm pretty sure this is due to how I'm using type families in the constraints, but I have no idea what sort of things are expensive in this context or how to see where the constraint solver is spending its time. 我很确定这是由于我在约束中如何使用类型族,但我不知道在这种情况下哪种东西是昂贵的,或者如何看待约束求解器花费时间的位置。 My best guess is that one of my operations on type lists is taking quadratic time instead of linear. 我最好的猜测是我对类型列表的操作之一是采用二次时间而不是线性时间。

Let's see an example of why I suspect the constraint solver. 让我们看一个为什么我怀疑约束求解器的例子。 In my files, I have code that looks like: 在我的文件中,我的代码如下:

class ExampleClass a where
    type ExampleType a
    f :: ExampleType a -> a

data ExampleData (xs :: [a]) = ...

instance 
    ( Constraint1
    , Constraint2
    , ...
    ) => ExampleClass (ExampleData xs) 
        where
    type ExampleType (ExampleData xs) = Int
    f = undefined

When I load this file into ghci 当我将此文件加载到ghci中时

ghci> :l Example.hs

compilation happens very quickly, much less than 1 second. 编译很快发生,远不到1秒。 Then, I execute the following line: 然后,我执行以下行:

ghci> let test = f Int :: ExampleData

No actual computation is going on, but this still takes a very long time. 没有实际的计算,但这仍然需要很长时间。 The more constraints in ExampleData 's instance declaration, the longer it takes. ExampleData的实例声明中的约束越多,所需的时间就越长。 (Actually evaluating test later happens instantly.) The best way I've figured out how to debug these performance issues is by commenting out constraints one-by-one and seeing which ones are causing the performance hit. (实际上后来评估测试会立即发生。)我弄清楚如何调试这些性能问题的最好方法是逐个注释掉约束并查看哪些因素导致了性能损失。 But this is very time consuming, and when those constraints involve complex type families, it's not really all that informative. 但这是非常耗时的,当这些限制涉及复杂类型的家庭时,它并不是真正的信息。

So, is there a better approach I can take to debug this problem? 那么,我可以采用更好的方法来调试这个问题吗?


Edit: It turns out I discovered a bug in GHC . 编辑:事实证明我发现了GHC中的一个错误 There's a script associated with the bug that demonstrates that the constraint solver is taking quadratic time on inputs that should be linear. 有一个与该bug相关联的脚本,表明约束求解器在输入上采用二次时间应该是线性的。

For what it's worth, you can evaluate constraints one at a time using :kind! 对于它的价值,您可以使用以下方法一次评估约束:亲切! to see how long it takes, instead of needing to comment them out individually. 看看需要多长时间,而不是需要单独评论它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM