1

我有以下类型和两个相关函数,我打算将它们作为大型列表折叠的一部分进行测量:

类型和访问功能:

data Aggregate a = Aggregate (Maybe a) (a -> Aggregate a)

get :: Aggregate a -> Maybe a
get (Aggregate get' _) = get'

put :: Aggregate a -> a -> Aggregate a
put (Aggregate _ put') = put'

第一个功能:

updateFirst :: Maybe a -> a -> Aggregate a
updateFirst cur val = Aggregate new (updateFirst new)
  where new = mplus cur (Just val)

first :: Aggregate a
first = Aggregate Nothing (updateFirst Nothing)

第二个功能:

updateMinimum :: Ord a => Maybe a -> a -> Aggregate a
updateMinimum cur val = Aggregate new (updateMinimum new)
  where new = min <$> (mplus cur (Just val)) <*> Just val

minimum :: Ord a => Aggregate a
minimum = Aggregate Nothing (updateMinimum Nothing)

函数的编写方式应使内存保持不变。因此,我选择使用StrictGHC 中的语言扩展,这会强制评估 thunk。该Weigh库用于执行分配测量:

test :: A.Aggregate Double -> Int -> Maybe Double
test agg len = A.get $ F.foldl' A.put agg (take len $ iterate (+0.3) 2.0)

testGroup :: String -> A.Aggregate Double -> Weigh ()
testGroup name agg = sequence_ $ map (\cnt -> func (str cnt) (test agg) cnt) counts
  where
    counts  = map (10^) [0 .. 6]
    str cnt = name ++ (show cnt)

main :: IO ()
main =
  mainWith
    (do setColumns [Case, Allocated, Max, GCs]
        testGroup "fst" A.first
        testGroup "min" A.minimum
    )

Weigh输出如下:

Case          Allocated          Max  GCs
fst1                304           96    0
fst10             2,248           96    0
fst100           21,688           96    0
fst1000         216,088           96    0
fst10000      2,160,088           96    2
fst100000    21,600,088           96   20
fst1000000  216,000,088           96  207
min1                552           96    0
min10             4,728           96    0
min100           46,488           96    0
min1000         464,088           96    0
min10000      4,967,768           96    4
min100000    49,709,656    6,537,712   44
min1000000  497,226,840  103,345,512  445

为什么 GHC 突然在大小为 10^5 和 10^6 的输入中分配更多内存?我的 GHC 版本是8.4.4.

4

1 回答 1

3

Haskell 中的严格性注释可以说是“关系的”。他们说,只要将其他事物评估为 WHNF,就必须将某些事物评估为 WHNF(弱头范式)。

对于函数参数,这意味着函数参数将在函数应用程序本身被评估为 WHNF 之前被评估为 WHNF。

对于严格字段,这意味着每当包含值被评估为 WHNF 时,该字段将被评估为 WHNF。这对于维护用作累加器的数据类型(例如,用作 a 的累加器的数据类型foldl')中的“严格链”很有用。否则,即使包含值保留在 WHNF 中,thunk 也会隐藏在惰性字段后面,并导致空间泄漏。特别是,元组没有严格的组件,并且是累加器中空间泄漏的常见来源。

MaybeJust对构造函数中包含的值也不严格。事实上,这就是问题的根源。内部的值在 的Just过程中从未被强制foldl',并且在那里积累了 thunk。

为什么没有Strict阻止这个问题?因为它只影响当前模块中的函数和数据类型定义,并Maybe在其他地方定义。解决方案是在每次迭代中手动强制值Just,或者定义您自己的“严格Maybe

于 2019-12-29T08:04:18.703 回答