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

Implement 'unlines' directly #479

Merged
merged 2 commits into from
Feb 6, 2022
Merged

Conversation

clyring
Copy link
Member

@clyring clyring commented Feb 1, 2022

Closes #478.

I wanted to share some code between strict unlines and strict intercalate, but wasn't able to easily get the singleton-copying to rewrite to a poke or similar, resulting in a performance hit. I think this level of duplication is acceptable.

My idea to make Lazy.concat fuse with the existing lazy unlines definition was a slight improvement, but just writing the fused fold manually produced bigger gains. I think this is because now no attempt is made to iterate over a singleton.

Benchmark results:

master:

  unlines
    lazy:   OK (0.94s)
      854  μs ±  17 μs
    strict: OK (0.74s)
      688  μs ±  13 μs

topic branch:

  unlines
    lazy:   OK (19.72s)
      599  μs ± 4.7 μs
    strict: OK (68.04s)
      249  μs ±  18 μs

@Bodigrim Bodigrim requested a review from sjakobi February 2, 2022 00:29
Copy link
Member

@sjakobi sjakobi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks good!

But "near-inverse" sounds pretty mysterious. It would be good to clarify what that means (an example would be helpful), or alternatively drop that word entirely.

@clyring
Copy link
Member Author

clyring commented Feb 3, 2022

A digression into the ways in which lines and unlines fail to be perfect inverses would be out-of-place, I think. I'd go for left- or right-inverse, if either one was quite correct. There's a restriction on both domain and co-domain that works, but it's a bit wordy to describe directly. There is a mathematical notion called inverse that does apply, but it's not the first one most people think of when they read the word without more context.

Data/ByteString/Char8.hs Outdated Show resolved Hide resolved
@Bodigrim Bodigrim added this to the 0.11.3.0 milestone Feb 6, 2022
@Bodigrim Bodigrim merged commit 5e59f1d into haskell:master Feb 6, 2022
@Bodigrim
Copy link
Contributor

Bodigrim commented Feb 6, 2022

Thanks @clyring!

sjakobi pushed a commit that referenced this pull request Feb 15, 2022
* Implement 'unlines' directly

* Rephrase public documentation for unlines

(cherry picked from commit 5e59f1d)
Bodigrim pushed a commit that referenced this pull request Feb 15, 2022
* Implement 'unlines' directly

* Rephrase public documentation for unlines

(cherry picked from commit 5e59f1d)
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.

Improve performance of Data.ByteString.unlines
3 participants