當我們談論編譯器優化中間列表時,通常我們討論的是在GHC的RULES
pragma中實現的「融合」你可以關注它是如何工作的,以及哪些列表功能是「好消費者」和「生產者」here。
不幸的是,它看起來不像span
是一個「好生產者」。你可以問一下GHC的核心輸出,並獲得與ghc -O2 -ddump-simpl -dsuppress-module-prefixes -dsuppress-uniques -ddump-core-stats -ddump-inlinings -ddump-rule-firings test.hs
這裏發射的規則列表中看到自己是一個清理輸出:
Rule fired: Class op >=
Rule fired: SPEC Data.List.sum
Inlining done: geInt{v r3n} [gid]
Inlining done: sum_sum1{v rkV} [gid]
Inlining done: span{v r1Q} [gid]
Inlining done: sum_sum'1{v rl6} [gid]
==================== Tidy Core ====================
Result size of Tidy Core = {terms: 24, types: 27, coercions: 0}
addPos1 :: Int -> Bool
addPos1 = \ (ds :: Int) -> case ds of _ { I# x -> >=# x 0 }
addPos [InlPrag=INLINE[0]] :: [Int] -> (Int, [Int])
addPos =
\ (w :: [Int]) ->
case $wspan @ Int addPos1 w of _ { (# ww1, ww2 #) ->
case $wsum' ww1 0 of ww3 { __DEFAULT -> (I# ww3, ww2) }
}
你可以看到我們所說的某種重寫/專門span
,然後是sum
。
您可能會看到vector
庫是否可以將它們融合,或者看看性能如何比較以獲得樂趣。
「另外,有人可以解釋一下(如果有的話)它會發現它可以嚴格累加嗎?」 foldl被內聯並且plus是嚴格的。嚴格分析器可以完成其他任何事情。 foldl'很少需要這個原因。 –
跨度或多或少是'takeWhile'p xs,'dropWhile'p xs。但是,如果您的addPlus出現性能問題,爲什麼還需要使用span?按照jberryman的說法研究http://www.haskell.org/ghc/docs/latest/html/users_guide/rewrite-rules.html。 – Jonke