[Haskell-cafe] Releasing head of lazy ByteString

tsuraan tsuraan at gmail.com
Sun Feb 20 16:38:17 CET 2011

I have a streaming network protocol where each message in the stream
is prefixed by a 64-bit message length marker.  Lazy ByteStrings seem
to be an elegant way to wrap network communications, so I'm using
them.  I have one concern though: how can I prevent my program from
hanging on to the beginning of the stream?  My code looks roughly like

lazy <- getContents clientSock
let (lenBS, rest1) = splitAt 8 lazy
let length = runGet getWord64be lenBS
let (msg, rest2) = splitAt (fromIntegral length) rest1
-- process msg

The program never uses that initial "lazy" again, but it's there, and
it's been assigned, so I assume that reference to the head of the
stream will always be around, and thus always consuming memory.  Is
there a way to indicate to haskell that I want to "forget" about that
assignment, so the runtime doesn't need to keep the stream around?  In
this case, I think I could do the stream processing in a
tail-recursive loop, and thus lose the previous references to the
stream, but is there a more specific way to tell haskell that I'm done
with some data?

More information about the Haskell-Cafe mailing list