Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: ghci parse error on input (Rick Murphy)
2. Re: How to print the name of a variable (Stephen Tetley)
3. Re: How to print the name of a variable (John Velman)
4. Memory usage problem (Sami Liedes)
5. Re: Memory usage problem (Patrick LeBoutillier)
6. Re: Memory usage problem (Sami Liedes)
----------------------------------------------------------------------
Message: 1
Date: Sat, 20 Mar 2010 08:42:19 -0400
From: Rick Murphy <[email protected]>
Subject: Re: [Haskell-beginners] ghci parse error on input
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Thanks, Stephen.
Stephen Tetley wrote:
> True - if you use -fglasgow-exts rather than adding extensions
> selectively it bring in the conflicting extension (probably
> -XMagicHash)
I believe you are correct. If I remove -fglasgow-exts, GHCI does not
report the parse error. I also found the following reference to stolen
syntax for -XMagicHash. (Scroll to bottom of page.)
http://www.haskell.org/ghc/docs/6.10.1/html/users_guide/syntax-extns.html
---
Rick
------------------------------
Message: 2
Date: Sat, 20 Mar 2010 14:05:30 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] How to print the name of a variable
Cc: Haskell Beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi Ozgur
Thanks for the qualification - I can see the benefit now.
Best wishes
Stephen
------------------------------
Message: 3
Date: Sat, 20 Mar 2010 08:40:39 -0700
From: John Velman <[email protected]>
Subject: Re: [Haskell-beginners] How to print the name of a variable
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Thanks, Stephen and Ozgur. Your responses were helpful and
educational.
Best,
John V.
On Sat, Mar 20, 2010 at 02:05:30PM +0000, Stephen Tetley wrote:
> Hi Ozgur
>
> Thanks for the qualification - I can see the benefit now.
>
> Best wishes
>
> Stephen
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 4
Date: Mon, 22 Mar 2010 01:23:21 +0200
From: Sami Liedes <[email protected]>
Subject: [Haskell-beginners] Memory usage problem
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
Hi!
I wrote a simple program to parse Debian package list files (a simple
text record format), sort the list of packages and output the list. I
first wrote the program in C (which I speak fluently), but wanting to
learn Haskell, have been trying to reimplement it in Haskell.
The problem is that my 60-line Haskell program, which doesn't yet do
everything the C program does, uses huge amounts of memory, 1.5
gigabytes when parsing a single file. The C implementation can parse
and sort more than twice that data in less than 10 megabytes. I can't
figure out where the memory goes or how to fix it, but my guess would
be it's some lazy computation thunk.
(See below after the code for description of the input and output
formats.)
With a package file of 28000 records (packages), the memory usage is
roughly 1.5 gigabytes when compiled with ghc - under hugs it just
bails out quickly after stack overflow - and I'd appreciate help in
figuring out
1) Where the memory is actually spent?
2) How would one usually go about figuring this out?
and
3) How to fix it?
[4) I'd also love style hints and other ideas to make my code more idiomatic]
I've tried to randomly add some $! operators to the code (that doesn't
feel right :-), but so far to no avail.
Here's the code:
------------------------------------------------------------
module Main where
import List (sort)
import Maybe (fromJust, mapMaybe)
-- Parse one line of the format "Field: value".
-- Ignore those that start with a space.
readField :: String -> Maybe (String,String)
readField line =
if line == "" || head line == ' ' then Nothing else
let (name,':':' ':val) = break (== ':') line in
Just (name,val)
data Package = Package { name, version :: !String }
deriving (Show, Eq, Ord)
-- Use sprintPackage for formatted output, show for unformatted
sprintPackage :: Package -> String
sprintPackage (Package name ver) =
name ++ replicate (50 - length name) ' ' ++ ver
-- Read one record worth of lines (separated by blank lines)
getOneRecordLines [] = ([],[])
getOneRecordLines lines = (hd, if tl == [] then [] else tail tl) where
(hd,tl) = break (== "") lines
-- Read one record worth of lines and parse, returning (Field, value)
-- tuples and the rest of the lines (less the just read record)
readRecordFields :: [String] -> ([(String,String)], [String])
readRecordFields lines = (mapMaybe readField rl, rest) where
(rl,rest) = getOneRecordLines lines
-- Convert a list of (Field,value) tuples to a Package
recordFieldsToPackage :: [(String,String)] -> Package
recordFieldsToPackage fields =
let names = [y | (x,y) <- fields, x=="Package"]
vers = [y | (x,y) <- fields, x=="Version"]
in
case (names,vers) of
([], _) -> error "Package has no name."
(a:b:rest, _) -> error ("Package has two names ("++a++","++b++").")
([n], []) -> error ("Package "++n++" has no version.")
([n], a:b:rest) -> error ("Package "++n++" has multiple versions.")
([n], [v]) -> Package n v
-- Read one record, returning Just Package if it contained a valid
-- package and version, Nothing otherwise.
readRecord :: [String] -> (Maybe Package,[String])
readRecord lines =
if fields == [] then (Nothing, rest)
else (Just (recordFieldsToPackage fields), rest) where
(fields,rest) = readRecordFields lines
-- Converts the list (stream) of lines to a list of packages
readRecords :: [String] -> [Package]
readRecords [] = []
readRecords lines =
let (rec,rest) = readRecord lines in
case rec of
Just pkg -> pkg : readRecords rest
Nothing -> readRecords rest
processFile = unlines . (map sprintPackage) . sort . readRecords . lines
main :: IO ()
main = interact processFile
------------------------------------------------------------
The input (stdin) for the program is of the format:
------------------------------------------------------------
Package: somepackagename
Version: someversionstring
Other-Fields: ...
Whatever: ...
... (lines that begin with space are just ignored)
Package: pkg2
Version: otherversion
...
------------------------------------------------------------
That is, the file has records separated by blank lines. Only lines
that begin with "Package: " or "Version: " are considered in a record,
the others are just ignored.
When finished, it outputs a formatted list of packages to stdout:
------------------------------------------------------------
a2ps 1:4.14-1
a2ps-perl-ja 1.45-5
a56 1.3-5
a7xpg 0.11.dfsg1-4
a7xpg-data 0.11.dfsg1-4
aa3d 1.0-8
aajm 0.4-3
aap 1.091-1
aap-doc 1.091-1
...
------------------------------------------------------------
GHC profiling hints that the memory is spent in processFile and main,
but no amount of adding $! to those functions seems to help:
------------------------------------------------------------
Thu Mar 18 22:37 2010 Time and Allocation Profiling Report (Final)
aptlistsh +RTS -p -RTS
total time = 3.18 secs (159 ticks @ 20 ms)
total alloc = 1,873,922,000 bytes (excludes profiling overheads)
COST CENTRE MODULE %time %alloc
processFile Main 44.0 61.9
main Main 32.1 20.7
readField Main 11.9 9.0
recordFieldsToPackage Main 4.4 2.0
newPkg Main 3.1 0.8
sprintPackage Main 1.3 2.1
readRecordFields Main 1.3 1.3
CAF Main 1.3 0.4
getOneRecordLines Main 0.6 1.4
------------------------------------------------------------
Sami
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
Url :
http://www.haskell.org/pipermail/beginners/attachments/20100321/271b053f/attachment-0001.bin
------------------------------
Message: 5
Date: Sun, 21 Mar 2010 20:28:23 -0400
From: Patrick LeBoutillier <[email protected]>
Subject: Re: [Haskell-beginners] Memory usage problem
To: Sami Liedes <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Sami,
I'm no profiling expert, but I have a few questions though:
- What is the size (in bytes) of your input file?
- Also, does memory usage improve if you remove the "sort"?
Patrick
On Sun, Mar 21, 2010 at 7:23 PM, Sami Liedes <[email protected]> wrote:
> Hi!
>
> I wrote a simple program to parse Debian package list files (a simple
> text record format), sort the list of packages and output the list. I
> first wrote the program in C (which I speak fluently), but wanting to
> learn Haskell, have been trying to reimplement it in Haskell.
>
> The problem is that my 60-line Haskell program, which doesn't yet do
> everything the C program does, uses huge amounts of memory, 1.5
> gigabytes when parsing a single file. The C implementation can parse
> and sort more than twice that data in less than 10 megabytes. I can't
> figure out where the memory goes or how to fix it, but my guess would
> be it's some lazy computation thunk.
>
> (See below after the code for description of the input and output
> formats.)
>
> With a package file of 28000 records (packages), the memory usage is
> roughly 1.5 gigabytes when compiled with ghc - under hugs it just
> bails out quickly after stack overflow - and I'd appreciate help in
> figuring out
>
> 1) Where the memory is actually spent?
> 2) How would one usually go about figuring this out?
>
> and
>
> 3) How to fix it?
> [4) I'd also love style hints and other ideas to make my code more idiomatic]
>
> I've tried to randomly add some $! operators to the code (that doesn't
> feel right :-), but so far to no avail.
>
> Here's the code:
>
> ------------------------------------------------------------
> module Main where
> import List (sort)
> import Maybe (fromJust, mapMaybe)
>
> -- Parse one line of the format "Field: value".
> -- Ignore those that start with a space.
> readField :: String -> Maybe (String,String)
> readField line =
> if line == "" || head line == ' ' then Nothing else
> let (name,':':' ':val) = break (== ':') line in
> Just (name,val)
>
> data Package = Package { name, version :: !String }
> deriving (Show, Eq, Ord)
>
> -- Use sprintPackage for formatted output, show for unformatted
> sprintPackage :: Package -> String
> sprintPackage (Package name ver) =
> name ++ replicate (50 - length name) ' ' ++ ver
>
> -- Read one record worth of lines (separated by blank lines)
> getOneRecordLines [] = ([],[])
> getOneRecordLines lines = (hd, if tl == [] then [] else tail tl) where
> (hd,tl) = break (== "") lines
>
> -- Read one record worth of lines and parse, returning (Field, value)
> -- tuples and the rest of the lines (less the just read record)
> readRecordFields :: [String] -> ([(String,String)], [String])
> readRecordFields lines = (mapMaybe readField rl, rest) where
> (rl,rest) = getOneRecordLines lines
>
> -- Convert a list of (Field,value) tuples to a Package
> recordFieldsToPackage :: [(String,String)] -> Package
> recordFieldsToPackage fields =
> let names = [y | (x,y) <- fields, x=="Package"]
> vers = [y | (x,y) <- fields, x=="Version"]
> in
> case (names,vers) of
> ([], _) -> error "Package has no name."
> (a:b:rest, _) -> error ("Package has two names ("++a++","++b++").")
> ([n], []) -> error ("Package "++n++" has no version.")
> ([n], a:b:rest) -> error ("Package "++n++" has multiple versions.")
> ([n], [v]) -> Package n v
>
> -- Read one record, returning Just Package if it contained a valid
> -- package and version, Nothing otherwise.
> readRecord :: [String] -> (Maybe Package,[String])
> readRecord lines =
> if fields == [] then (Nothing, rest)
> else (Just (recordFieldsToPackage fields), rest) where
> (fields,rest) = readRecordFields lines
>
> -- Converts the list (stream) of lines to a list of packages
> readRecords :: [String] -> [Package]
> readRecords [] = []
> readRecords lines =
> let (rec,rest) = readRecord lines in
> case rec of
> Just pkg -> pkg : readRecords rest
> Nothing -> readRecords rest
>
> processFile = unlines . (map sprintPackage) . sort . readRecords . lines
>
> main :: IO ()
> main = interact processFile
> ------------------------------------------------------------
>
> The input (stdin) for the program is of the format:
>
> ------------------------------------------------------------
> Package: somepackagename
> Version: someversionstring
> Other-Fields: ...
> Whatever: ...
> ... (lines that begin with space are just ignored)
>
> Package: pkg2
> Version: otherversion
> ...
> ------------------------------------------------------------
>
> That is, the file has records separated by blank lines. Only lines
> that begin with "Package: " or "Version: " are considered in a record,
> the others are just ignored.
>
> When finished, it outputs a formatted list of packages to stdout:
>
> ------------------------------------------------------------
> a2ps 1:4.14-1
> a2ps-perl-ja 1.45-5
> a56 1.3-5
> a7xpg 0.11.dfsg1-4
> a7xpg-data 0.11.dfsg1-4
> aa3d 1.0-8
> aajm 0.4-3
> aap 1.091-1
> aap-doc 1.091-1
> ...
> ------------------------------------------------------------
>
> GHC profiling hints that the memory is spent in processFile and main,
> but no amount of adding $! to those functions seems to help:
>
> ------------------------------------------------------------
> Thu Mar 18 22:37 2010 Time and Allocation Profiling Report (Final)
>
> aptlistsh +RTS -p -RTS
>
> total time = 3.18 secs (159 ticks @ 20 ms)
> total alloc = 1,873,922,000 bytes (excludes profiling overheads)
>
> COST CENTRE MODULE %time %alloc
>
> processFile Main 44.0 61.9
> main Main 32.1 20.7
> readField Main 11.9 9.0
> recordFieldsToPackage Main 4.4 2.0
> newPkg Main 3.1 0.8
> sprintPackage Main 1.3 2.1
> readRecordFields Main 1.3 1.3
> CAF Main 1.3 0.4
> getOneRecordLines Main 0.6 1.4
> ------------------------------------------------------------
>
> Sami
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (GNU/Linux)
>
> iQIcBAEBCAAGBQJLpqppAAoJEKLT589SE0a08FoQALtZf+eO7u+YdVJuvZ6xxg/y
> DVHTnRdemPtlNuBJazV1RlTQovRNyQpX1CHX70MdnHLP4g9CzQhBsIeGTdgA9am+
> pd9NjNFc1+ZDJJWAY24Kyx0pEfvyfLuUvF8KNInZGXeg+BriiFjFFU3RHqY0F3XD
> LIjAq/TaABmMGgahvG/y0EMRhduC+N9VskA4ivd7OCxvA7bGuzZEFxFbz2qqXrcE
> VXzE4ioha8C+WeX6djm/+CSoqN8o7bN1DS+fkEYr+jlU2jYG/xvnnfC8785r+9iP
> wZo1muIP4FVExZ3w5VSjLWkhMjgYDHLiuEs1S5dQdrgAHz+ea+PZqFyOoDEMyYvG
> vhnaTrPev2O4BZLAky5F13tj7fm1tV5CxJ9oq7fuurZoB7OdEpSguOyR3xYgoIYv
> spg7aqRlLuruX7VHbSu6pTtqWCEvxHxQbd+R2f5jqaDOsw+3n1enH6PFqTkiLpHc
> ReTLli4ploUGmiQRmbWpe2urECQ6QAXhqdx93vHwT2TUYVqL9wH84XsNvGBSt8Kq
> 5OowrJOlneeWre0SS1dn96ASE1oKKiCpc9rkGvD3tMxTONHFTPziyMtiC675ReH0
> 9XHvRkn9+8RV2gVnKeOMyfPT3ef/q2aP8uESf4nW6AAHNa41QWiB9/OYS32ljx/9
> Cv/IvQVVIzSYQjNTO7a2
> =yn0D
> -----END PGP SIGNATURE-----
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
--
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada
------------------------------
Message: 6
Date: Mon, 22 Mar 2010 03:06:08 +0200
From: Sami Liedes <[email protected]>
Subject: Re: [Haskell-beginners] Memory usage problem
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
On Sun, Mar 21, 2010 at 08:28:23PM -0400, Patrick LeBoutillier wrote:
> I'm no profiling expert, but I have a few questions though:
>
> - What is the size (in bytes) of your input file?
The input file contains 31,129,639 bytes, with 710,355 lines and 27,954
individual packages/records.
In fact it seems that even if the input stream repeats only the line
" a" (that is, a space and the letter a) infinitely, the program
eventually eats all memory. That's interesting.
So I guess the lines are read in, but then held in memory and lazily
not processed further until the entire file has been read, because
it's not necessary? Or something.
I tried using $! in readRecordFields like
readRecordFields lines = (mapMaybe (readField $!) rl, rest) where
(rl,rest) = getOneRecordLines lines
but that didn't help either... I guess I don't fully understand $!.
Does it force the entire computation below it to finish? To achieve
sane memory behavior, it would seem necessary to parse the lines
before they're all read, and then mapMaybe in readRecordFields would
throw out the Nothings. After that I believe it should all be constant
amount of memory with all lines starting with a space.
> - Also, does memory usage improve if you remove the "sort"?
Yes. Then it only takes a few megabytes, regardless of how large the
file is.
Thanks,
Sami
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
Url :
http://www.haskell.org/pipermail/beginners/attachments/20100321/5ea5e52b/attachment.bin
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 21, Issue 24
*****************************************