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

Optimize Eq and Ord for LazyByteString using pointer equality #404

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sjakobi
Copy link
Member

@sjakobi sjakobi commented Jun 30, 2021

This is inspired by a discussion in Haskell-Cafe:
https://mail.haskell.org/pipermail/haskell-cafe/2021-June/134073.html

@sjakobi
Copy link
Member Author

sjakobi commented Jul 1, 2021

TODO for myself:

  • Benchmarking

Comment on lines +236 to +239
| otherwise = case compare al bl of
LT -> a == S.BS bp al && eq as (Chunk (S.BS (S.plusForeignPtr bp al) (bl - al)) bs)
EQ -> a == b && eq as bs
GT -> S.BS ap bl == b && eq (Chunk (S.BS (S.plusForeignPtr ap bl) (al - bl)) as) bs
Copy link
Member Author

Choose a reason for hiding this comment

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

In the LT and GT cases, we'd recursively run the pointer equality checks on freshly allocated Chunks – which is totally wasteful

It might be better to call an "inner" function here which doesn't perform the pointer equality check.

cmp has the same issue.

@clyring
Copy link
Member

clyring commented Sep 22, 2021

I suspect that sharing-based equality-checks on lazy data structures are inherently at odds with referential transparency due to infinite and partially-defined values. So, it's my opinion that these comparison functions should only be offered from an Unsafe module, if at all.

I will also point out that since the Eq instance for strict ByteString already performs a sharing-based equality check, the existing Eq instance for lazy ByteString should already be pretty fast in most cases where there is a long shared tail. The same does not appear to be true for the Ord instance.

@sjakobi
Copy link
Member Author

sjakobi commented Sep 23, 2021

Thanks for your comments, @clyring!

I suspect that sharing-based equality-checks on lazy data structures are inherently at odds with referential transparency due to infinite and partially-defined values. So, it's my opinion that these comparison functions should only be offered from an Unsafe module, if at all.

Could you clarify where exactly you see the problem? My intention was that the changed instances would behave just like the old ones. But maybe this won't work out?!

I will also point out that since the Eq instance for strict ByteString already performs a sharing-based equality check, the existing Eq instance for lazy ByteString should already be pretty fast in most cases where there is a long shared tail. The same does not appear to be true for the Ord instance.

I'm also not convinced yet that this patch will pay off performance-wise.

@clyring
Copy link
Member

clyring commented Sep 23, 2021

These comparators give the same result as those in the existing instances, as long as at least one argument is a finite, total ByteString. But for infinite and partial ByteStrings, they will (sometimes) produce EQ where the existing instance would produce bottom. For example, let x = LBS8.repeat 'c' in x == x. I'm not sure off the top of my head if any "reasonable" code can actually be broken by this, but I am wary nevertheless.

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.

3 participants