haskell / bytestring

An efficient compact, immutable byte string type (both strict and lazy) suitable for binary or 8-bit character data.
http://hackage.haskell.org/package/bytestring
Other
289 stars 139 forks source link

Uncons should not produce thunks #558

Open clyring opened 1 year ago

clyring commented 1 year ago

Consider Lazy.uncons. Today, we have in Data.ByteString.Lazy:

uncons :: ByteString -> Maybe (Word8, ByteString)
uncons Empty = Nothing
uncons (Chunk c cs)
    = Just (S.unsafeHead c,
            if S.length c == 1 then cs else Chunk (S.unsafeTail c) cs)

Both components of the returned tuple internally perform a small amount of work before constructing a result:

What should we do about this?

clyring commented 1 year ago

The problem with the second component of Lazy.uncons was fixed in #559. But the first component of the result of uncons is still a thunk, so I will re-open.