Skip to content

Replace 'lines' implementation with commented one - #722

Merged
Bodigrim merged 3 commits into
haskell:masterfrom
alexfmpe:lines-split
Sep 1, 2026
Merged

Bodigrim merged 3 commits into
haskell:masterfrom
alexfmpe:lines-split

Conversation

@alexfmpe

Copy link
Copy Markdown
Contributor

I was using Data.ByteString.Char8.split '\n' on a project of mine then noticed it could be simplified to Data.ByteString.Char8.lines but surprisingly the specialized version was actually ~10% slower.

I assume it's because Data.ByteString.split is heavily optimized at low-level. Turns out there's a commented implementation that's on par as far as running time is concerned but allocates slightly more. Looks like it felt through the cracks: #264 (comment)

current
    strict: OK
      126  μs ± 7.1 μs, 1.4 MB allocated,  11 B  copied,  54 MB peak memory

commented
    strict: OK
      113  μs ± 6.8 μs, 1.0 MB allocated,   5 B  copied,  54 MB peak memory

lines = split '\n'
    strict: OK
      113  μs ± 7.8 μs, 859 KB allocated,   5 B  copied,  54 MB peak memory

Oddly, lines = split '\n' actually fails the test suite

  Properties
    StrictChar8
      lines:             OK
        +++ OK, passed 100 tests.
      lines \n:          FAIL
        *** Failed! Falsified (after 1 test):
        ["one","two","","","five","","seven",""] /= ["one","two","","","five","","seven"]
        Use --quickcheck-replay="(SMGen 16185955913172335579 11815773358582755353,0)" to reproduce.
        Use -p '/lines/&&/StrictChar8.lines \\n/' to rerun this test only.
      unlines:           OK (0.02s)
        +++ OK, passed 100 tests.

Shouldn't these consume the delimiter even when trailing?

ghci> Data.ByteString.split 10 "a\nb\n"
["a","b",""]
ghci> Data.ByteString.Char8.split '\n' "a\nb\n"
["a","b",""]

@alexfmpe alexfmpe changed the title Implement strict 'lines' via 'split' Replace 'lines' implementation with commented one Aug 24, 2026
@Bodigrim

Copy link
Copy Markdown
Contributor

Shouldn't these consume the delimiter even when trailing?

What's the behaviour of Data.List.lines and Data.Text.lines in such case?

@alexfmpe

Copy link
Copy Markdown
Contributor Author
ghci> Data.List.lines "\na\nb\n"
["","a","b"]

ghci> Data.Text.lines "\na\nb\n"
["","a","b"]
ghci> Data.Text.split (== '\n') "\na\nb\n"
["","a","b",""]

@Bodigrim

Copy link
Copy Markdown
Contributor

i386 job probably needs BOOTSTRAP_HASKELL_GHC_VERSION=9.6.7 before sh.

Comment thread Data/ByteString/Char8.hs Resolved
@Bodigrim

Copy link
Copy Markdown
Contributor

Looks good, would you mind to fix i386 job as suggested above please?

@Bodigrim
Bodigrim merged commit 0514299 into haskell:master Sep 1, 2026
26 checks passed
@Bodigrim

Bodigrim commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants