Why summing native lists is slower than summing church-encoded lists with `GHC -O2`?

前端 未结 1 1249
囚心锁ツ
囚心锁ツ 2021-02-05 10:22

In order to test how church-encoded lists perform against user-defiend lists and native lists, I\'ve prepared 3 benchmarks:

User-defined lists

data Lis         


        
相关标签:
1条回答
  • 2021-02-05 11:28

    GHC 7.10 has call arity analysis, which lets us define foldl in terms of foldr and thus let left folds, including sum, participate in fusion. GHC 7.8 also defines sum with foldl but it can't fuse the lists away. Thus GHC 7.10 performs optimally and identically to the Church version.

    The Church version is child's play to optimize in either GHC versions. We just have to inline (+) and 0 into fenumTil, and then we have a patently tail-recursive go which can be readily unboxed and then turned into a loop by the code generator.

    The user-defined version is not tail-recursive and it works in linear space, which wrecks performance, of course.

    0 讨论(0)
提交回复
热议问题