4 Dec 2012 15:11
Is it possible to have constant-space JSON decoding?
Iustin Pop <iustin <at> google.com>
2012-12-04 14:11:07 GMT
2012-12-04 14:11:07 GMT
Hi, I'm trying to parse a rather simple but big JSON message, but it turns out that memory consumption is a problem, and I'm not sure what the actual cause is. Let's say we have a simple JSON message: an array of 5 million numbers. I would like to parse this in constant space, such that if I only need the last element, overall memory usage is low (yes, unrealistic use, but please bear with me for a moment). Using aeson, I thought the following program will be nicely-behaved: > import Data.Aeson > import qualified Data.Attoparsec.ByteString.Lazy as AL > import qualified Data.ByteString.Lazy as L > > main = do > r <- L.readFile "numbers" > case AL.parse json r :: AL.Result Value of > AL.Fail _ context errs -> do > print context > print errs > AL.Done _ d -> case fromJSON d::Result [Value] of > Error x -> putStrLn x > Success d -> print $ last d However, this uses (according to +RTS -s) ~1150 GB of memory. I've tried switching from json to json', but while that uses slightly less memory (~1020 MB) it clearly can't be fully lazy, since it forces conversion to(Continue reading)
RSS Feed