[commit: ghc] wip/nfs-locking: Rewrite chunksOfSize so it doesn't go pear shaped on long inputs (763a518)

git at git.haskell.org git at git.haskell.org
Fri Oct 27 00:07:20 UTC 2017


Repository : ssh://git@git.haskell.org/ghc

On branch  : wip/nfs-locking
Link       : http://ghc.haskell.org/trac/ghc/changeset/763a518c64ec88acc29db8cc2f7b17955b02a6df/ghc

>---------------------------------------------------------------

commit 763a518c64ec88acc29db8cc2f7b17955b02a6df
Author: Neil Mitchell <ndmitchell at gmail.com>
Date:   Mon Jan 11 11:06:18 2016 +0000

    Rewrite chunksOfSize so it doesn't go pear shaped on long inputs


>---------------------------------------------------------------

763a518c64ec88acc29db8cc2f7b17955b02a6df
 src/Settings/Builders/Ar.hs | 12 ++----------
 1 file changed, 2 insertions(+), 10 deletions(-)

diff --git a/src/Settings/Builders/Ar.hs b/src/Settings/Builders/Ar.hs
index 86f4310..fc43d45 100644
--- a/src/Settings/Builders/Ar.hs
+++ b/src/Settings/Builders/Ar.hs
@@ -48,13 +48,5 @@ useSuccessiveInvocations path flagArgs fileArgs = do
 -- | @chunksOfSize size strings@ splits a given list of strings into chunks not
 -- exceeding the given @size at .
 chunksOfSize :: Int -> [String] -> [[String]]
-chunksOfSize _    [] = []
-chunksOfSize size strings = reverse chunk : chunksOfSize size rest
-  where
-    (chunk, rest) = go [] 0 strings
-    go res _         []     = (res, [])
-    go res chunkSize (s:ss) =
-        if newSize > size then (res, s:ss) else go (s:res) newSize ss
-      where
-        newSize = chunkSize + length s
-
+chunksOfSize n = repeatedly f
+    where f xs = splitAt (max 1 $ length $ takeWhile (<= n) $ scanl1 (+) $ map length xs) xs



More information about the ghc-commits mailing list