我在并行3.2.0.4中查看了parBuffer的代码,但我遗漏了它的工作原理.我不知道除了最初的火花之外它怎么能产生新的火花.据我所知,它在parBufferWHNF中使用start来强制第一个n用par引发,然后再通过ret在同一个条目上再次使用par(不应该只丢弃y而不是冒险获得火花) GC'd?)同时返回相应的结果?然后它直接返回xs,没有任何额外的火花创建,因为rdeepseq只是调用pseq.
但显然测试这样的代码
withStrategy (parBuffer 10 rdeepseq) $ take 100 [ expensive stuff ]
Run Code Online (Sandbox Code Playgroud)
我可以看到ghc RTS信息中的所有100个火花,但是其他90个创建在哪里?
这是我正在查看的代码:
parBufferWHNF :: Int -> Strategy [a]
parBufferWHNF n0 xs0 = return (ret xs0 (start n0 xs0))
where -- ret :: [a] -> [a] -> [a]
ret (x:xs) (y:ys) = y `par` (x : ret xs ys)
ret xs _ = xs
-- start :: Int -> [a] -> [a]
start 0 ys = ys
start !_n [] = []
start !n (y:ys) = …Run Code Online (Sandbox Code Playgroud)