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

Lazy zip is stricter than expected #667

Closed
clyring opened this issue Mar 31, 2024 · 3 comments · Fixed by #668
Closed

Lazy zip is stricter than expected #667

clyring opened this issue Mar 31, 2024 · 3 comments · Fixed by #668
Milestone

Comments

@clyring
Copy link
Member

clyring commented Mar 31, 2024

I expect BL.zip x y to be equivalent to List.zip (BL.unpack x) (BL.unpack y). And yet if x = BL.replicate 2 0 <> error "lazy tail" and y = BL.singleton 0 we observe the former to be (0, 0) : error "lazy tail" and the latter to be (0, 0) : [].

clyring added a commit to clyring/bytestring that referenced this issue Mar 31, 2024
As a bonus, the new code is easier to read and doesn't trigger
a spurious incomplete-pattern-match warning.  (Or finding the
bug can be seen as a bonus for cleaning up that messy code.)

Fixes haskell#667.
clyring added a commit that referenced this issue Apr 9, 2024
* Make D.B.Lazy.zipWith properly lazy

As a bonus, the new code is easier to read and doesn't trigger
a spurious incomplete-pattern-match warning.  (Or finding the
bug can be seen as a bonus for cleaning up that messy code.)

Fixes #667.

* Lazy.zipWith: evaluate the 'unsafeHead' calls eagerly
@clyring clyring added this to the 0.12.2.0 milestone Apr 9, 2024
@Bodigrim
Copy link
Contributor

Bodigrim commented Oct 9, 2024

I'm not sure if this is fine to go into 0.12 series, kinda a change of behavior, even if minor and benign. Removing a milestone for now.

@clyring
Copy link
Member Author

clyring commented Oct 15, 2024

I think it's fine to backport. We have included similar laziness-increasing changes in other recent minor releases:

The old strictness behavior for Lazy.zip and Lazy.zipWith strikes me as an outright bug. It is the following:

  • Before (potentially) examining the second byte of any input chunk, force the next chunk of the same input bytestring.

And that behavior...

  • was rather arbitrary
  • was not documented
  • seems to have no benefits whatsoever

And of course internal-chunk-boundary-dependent strictness means that the old behavior can distinguish between "equal" LazyByteStrings:

ghci> import qualified Data.ByteString.Lazy as BL
ghci> import qualified Data.ByteString as BS
ghci> let x = BL.repeat 0
ghci> let y1 = BL.take 10000 x
ghci> let y2 = BL.replicate 10000 0
ghci> y1 == y2
True
ghci> map (map BS.length . BL.toChunks) [y1, y2]
[[4080,4080,1840],[1840,4080,4080]]
ghci> last $ take 8000 $ BL.zip x (y1 <> undefined)
(0,0)
ghci> last $ take 8000 $ BL.zip x (y2 <> undefined)
*** Exception: Prelude.undefined
CallStack (from HasCallStack):
  undefined, called at <interactive>:9:36 in interactive:Ghci9

Bodigrim pushed a commit that referenced this issue Oct 15, 2024
* Make D.B.Lazy.zipWith properly lazy

As a bonus, the new code is easier to read and doesn't trigger
a spurious incomplete-pattern-match warning.  (Or finding the
bug can be seen as a bonus for cleaning up that messy code.)

Fixes #667.

* Lazy.zipWith: evaluate the 'unsafeHead' calls eagerly
@Bodigrim
Copy link
Contributor

Fair enough, backported.

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 a pull request may close this issue.

2 participants