[Haskell-beginners] Bytestring question

Peter Braun peter-braun at gmx.net
Wed Jan 26 01:52:33 CET 2011


Hi everyone,

as an exercise for learning Haskell i'm writing a program that converts 
Ascii Stl files (a simple format for 3D model data) into binary Stl 
format. In my first attempt i used normal strings and the result was 
therefore very slow. Now i rewrote the program to use lazy bytestrings 
instead.

But well... it got even slower, so i'm probably doing something terribly 
wrong ;)

Here's what i do (the relevant parts):

...
asciiFile <- L.readFile (args!!0)
binHandle <- openBinaryFile (args!!1) WriteMode
let asciiLines = L.split (c2w '\n') asciiFile
...
parseFile binHandle (Normal, tail asciiLines) -- First line contains a 
comment
...

where L is Data.ByteString.Lazy. readFile ought to be lazy so it should 
not read the whole file into ram at this point. But when i split the 
lines and pass them to a function, is this still carried out lazily?

parseFile processes a line, depending on the StlLineType and then calls 
itself recursive like this:

parseFile :: Handle -> (StlLineType, [L.ByteString]) -> IO ()
...
parseFile h (Vertex1, s) = do
     let vals = extractVertex (head s)
     L.hPutStr h $ runPut (writeFloatArray vals)
     parseFile h (Vertex2, tail s)
...

extractVertex looks like this:

extractVertex :: L.ByteString -> [Float]
extractVertex s = let fracs = filter (\n -> L.length n > 0) $ L.split 
(c2w ' ') s
                                     in    [read (C.unpack(fracs!!1)) :: 
Float,
                                             read (C.unpack(fracs!!2)) 
:: Float,
                                             read (C.unpack(fracs!!3)) 
:: Float]

where C is Data.ByteString.Lazy.Char8. It splits a byte string, filters 
out the whitespaces and converts certain entries to floats. Maybe unpack 
is an expensive operation. Is there a better way to convert a Bytestring 
to float?

I know, this is bad Haskell code ;) But where is my grand, obvious 
misuse of Bytestring?

I'm grateful for any suggestion to improve that code. I'm using ghc, 
version 6.12.1.

Thank you,
Peter



More information about the Beginners mailing list