Saving a data structure to a file

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Saving a data structure to a file

Neil Mitchell
Hi,

I have a program which needs to cache intermediate data structures to
a file (finite, non-cyclic, relatively simple, around ~100Kb when
shown). Is there any easy way to acheive this?

The current approach I am using is to add deriving Read, Show to all
the data structures, and then

writeFile "output.txt" (show x)
read =<< readFile "output.txt"

The problem is that this is too slow, and itsn't lazy. Is there any
way to do better than this? I can imagine something like
showBinary/readBinary which did this using binary files directly, but
is there anything that does this?

I'd like it to work with GHC and Hugs at least, and the less extra
code I have to write the better :)

Thanks

Neil
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Saving a data structure to a file

Bulat Ziganshin-2
Hello Neil,

Friday, April 28, 2006, 7:59:22 PM, you wrote:

> I have a program which needs to cache intermediate data structures to
> a file (finite, non-cyclic, relatively simple, around ~100Kb when
> shown). Is there any easy way to acheive this?

use NewBinary (darcs get http://www.n-heptane.com/nhlab/repos/NewBinary/)
or jhc's Binary module together with DrIFT to generate Binary
instances. these modules need minimal tweaks to implement Hugs compatibility

code generated by DrIFT will use strict get. if you want to get lazy
reading, you can replace get/put_ with lazyGet/lazyPut in appropriate
instances

in order to make it run faster, you should work with memory buffer
and read/write entire buffer from/to file:

writing:
  bh <- openBinMem 1 undefined
  lazyPut bh data
  ... put more data
  writeBinMem bh "filename"

reading:
  bh <- readBinMem "filename"
  data <- lazyGet bh
  .... get more data

--
Best regards,
 Bulat                            mailto:[hidden email]

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe