在haskell中读取大文件?

发布于 2024-10-26 01:39:51 字数 1893 浏览 1 评论 0原文

我一直在尝试在 haskell 中读取一个大文件。

我需要使用大学项目的自定义算法来压缩它。一切正常,直到我开始压缩大文件。

我从程序中提取出了问题所在,并以“Hello big file”的形式将其公开:

import System
import qualified Data.ByteString.Lazy as BL
import Data.Word

fold_tailrec :: (a -> b -> a) -> a -> [b] -> a
fold_tailrec _ acc [] =
    acc
fold_tailrec foldFun acc (x : xs) =
    fold_tailrec foldFun (foldFun acc x) xs

fold_tailrec' :: (a -> b -> a) -> a -> [b] -> a
fold_tailrec' _ acc [] =
    acc
fold_tailrec' foldFun acc (x : xs) =
    let forceEval = fold_tailrec' foldFun (foldFun acc x) xs in
    seq forceEval forceEval

main :: IO ()
main =
    do
        args <- System.getArgs
        let filename = head args
        byteString <- BL.readFile filename
        let wordsList = BL.unpack byteString
        -- wordsList is supposed to be lazy (bufferized)
        let bytesCount = fold_tailrec (\acc word -> acc + 1) 0 wordsList
        print ("Total bytes in " ++ filename ++ ": " 
               ++ (show bytesCount))

我将此文件命名为 Test.hs,然后执行以下操作:

$ ls -l toto
-rwxrwxrwx 1 root root 5455108 2011-03-23 19:08 toto
$ ghc --make -O Test.hs
[1 of 1] Compiling Main             ( Test.hs, Test.o )
Linking Test ...
$ ./Test toto
Stack space overflow: current size 8388608 bytes.
Use `+RTS -Ksize -RTS' to increase it.
$ ./Test toto +RTS -K50M -RTS
Stack space overflow: current size 50000000 bytes.
Use `+RTS -Ksize -RTS' to increase it.
$ ./Test toto +RTS -K500M -RTS
"Total bytes in toto: 5455108"
$ time ./Test toto +RTS -K500M -RTS
"Total bytes in toto: 5455108"

real    0m33.453s
user    0m8.917s
sys 0m10.433s

任何人都可以解释一下为什么我需要 500 MB RAM 和 30 秒的 CPU 才能浏览一个可怜的 5 MB 文件?请问我做错了什么?为什么 [word8] 没有按照 ByteString 文档所述进行缓冲。以及如何解决这个问题?

我尝试定义自己的尾递归折叠而不是foldl、foldr 或foldl'。 我也尝试用 seq 解冻 thunk。 到目前为止我还没有结果。

感谢您的任何帮助,因为我被困住了。

i've been trying to read a large file in haskell.

I need to compress it using a custom algorithm for a university project. Everything works fine untill i start to compress big files.

I extracted what was going wrong out of my program, and i expose it here in the form of a "Hello big file":

import System
import qualified Data.ByteString.Lazy as BL
import Data.Word

fold_tailrec :: (a -> b -> a) -> a -> [b] -> a
fold_tailrec _ acc [] =
    acc
fold_tailrec foldFun acc (x : xs) =
    fold_tailrec foldFun (foldFun acc x) xs

fold_tailrec' :: (a -> b -> a) -> a -> [b] -> a
fold_tailrec' _ acc [] =
    acc
fold_tailrec' foldFun acc (x : xs) =
    let forceEval = fold_tailrec' foldFun (foldFun acc x) xs in
    seq forceEval forceEval

main :: IO ()
main =
    do
        args <- System.getArgs
        let filename = head args
        byteString <- BL.readFile filename
        let wordsList = BL.unpack byteString
        -- wordsList is supposed to be lazy (bufferized)
        let bytesCount = fold_tailrec (\acc word -> acc + 1) 0 wordsList
        print ("Total bytes in " ++ filename ++ ": " 
               ++ (show bytesCount))

I name this file Test.hs, then do the following:

$ ls -l toto
-rwxrwxrwx 1 root root 5455108 2011-03-23 19:08 toto
$ ghc --make -O Test.hs
[1 of 1] Compiling Main             ( Test.hs, Test.o )
Linking Test ...
$ ./Test toto
Stack space overflow: current size 8388608 bytes.
Use `+RTS -Ksize -RTS' to increase it.
$ ./Test toto +RTS -K50M -RTS
Stack space overflow: current size 50000000 bytes.
Use `+RTS -Ksize -RTS' to increase it.
$ ./Test toto +RTS -K500M -RTS
"Total bytes in toto: 5455108"
$ time ./Test toto +RTS -K500M -RTS
"Total bytes in toto: 5455108"

real    0m33.453s
user    0m8.917s
sys 0m10.433s

Could anyone please explain why i need 500 Megabytes of RAM and 30 seconds of CPU in order to browse a miserable 5 Megabytes file ? Please what am i doing wrong ? Why isn't the [word8] bufferized as the ByteString documentation states. And how to fix this ?

I tried to define my own tail recursive fold instead of foldl, foldr, or foldl'.
I tried to unfreeze the thunks as well with seq.
I got no result so far.

Thanks for any kind of help because i'm stuck.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白馒头 2024-11-02 01:39:51

构造“seq x x”始终是无用的。如果 y = seq xx 并且我强制 y,则强制 x 然后返回 x。这相当于 y=x 并强制 y。因此,“seqforceEvalforceEval”只不过是“forceEval”。

使用折叠的错误是一种常见错误。

您正在使用折叠来执行输入中的字节计数。对于这样的总和,您应该使用严格的左折叠,但您的手写折叠是惰性左折叠。 (acc+1) 没有得到评估,因此它构建了 500 万个嵌套应用程序:(((...(0+1)+1)+1)+1)+1)+1)...+1 )。然后在打印时强制执行,求值尝试下降到 500 万个括号。

因此,挂起堆栈对于每个 Word8 都有一个条目。对于短输入,它到达末尾并看到 0。对于长输入,它会耗尽 GHC 的堆栈空间,因为 GHC 的创建者和大多数用户认为尝试分配 500 万个堆栈帧通常是程序员的设计错误。

我预测你可以使用“seq”来解决这个问题:

fold_tailrec' foldFun acc (x : xs) =
    let acc' = foldFun acc x
    in seq acc' (fold_tailrec' foldFun acc' xs)

The construct "seq x x" is always useless. If y = seq x x and I force y then this forces x then returns x. This is equivalent to y=x and forcing y. Thus "seq forceEval forceEval" does nothing more than "forceEval".

The error with your use of a fold is a common one.

You are using a fold to perform a count of the bytes in the input. You should be using a strict left fold for such a sum, but your hand written fold is a lazy left fold. The (acc+1) is not getting evaluated, so it builds 5 million nested applications: (((...(0+1)+1)+1)+1)+1)+1)...+1). Then it is forced when printing, and the evaluation tries to descend into 5 million parentheses.

Thus the pending stack has one entry for each Word8. For short inputs it reaches the end and sees 0. For long inputs it runs out of stack space with GHC because the creators and most users of GHC think that trying to allocate 5 million stack frames is usually a design error by the programmer.

I predict that you can use "seq" to fix this:

fold_tailrec' foldFun acc (x : xs) =
    let acc' = foldFun acc x
    in seq acc' (fold_tailrec' foldFun acc' xs)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文