Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent Lazy.uncons from allocating a thunk #559

Merged
merged 1 commit into from
Nov 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Data/ByteString/Lazy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,13 @@ head (Chunk c _) = S.unsafeHead c
-- if it is empty.
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)
uncons (Chunk c cs) = case S.length c of
-- Don't move this test inside of the Just or (,).
-- We don't want to allocate a thunk to put inside of the tuple!
-- And if "let !tl = ... in Just (..., tl)" seems more appealing,
-- remember that this function must remain lazy in cs.
1 -> Just (S.unsafeHead c, cs)
_ -> Just (S.unsafeHead c, Chunk (S.unsafeTail c) cs)
{-# INLINE uncons #-}

-- | /O(1)/ Extract the elements after the head of a ByteString, which must be
Expand Down