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

Tweak documentation #523

Merged
merged 3 commits into from
Jul 2, 2022
Merged

Tweak documentation #523

merged 3 commits into from
Jul 2, 2022

Conversation

Bodigrim
Copy link
Contributor

No description provided.

@Bodigrim Bodigrim added this to the 0.11.4.0 milestone Jun 25, 2022
@Bodigrim Bodigrim requested a review from sjakobi June 25, 2022 20:27
Comment on lines 393 to 397
-- There are zero reasons to use 'head': it is both partial and slow.
-- Please consider using either 'uncons', which is total, or, if you are
-- adamant that the argument is non-empty, 'Data.ByteString.Unsafe.unsafeHead',
-- which is branchless and fast.
head :: HasCallStack => ByteString -> Word8
Copy link
Member

Choose a reason for hiding this comment

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

I think it's good to reference uncons and unsafeHead here, but I disagree with the "zero resons to use head" bit. In certain situations head might still be the shortest / most ergonomic choice, and with its HasCallStack constraint it should be pretty easy to debug. unsafeHead may produce a segmentation fault – a risk that some users probably don't want to take on.

(Dito for last, tail, init and index.)

@@ -342,12 +342,14 @@ snoc cs w = foldrChunks Chunk (singleton w) cs
{-# INLINE snoc #-}

-- | /O(1)/ Extract the first element of a ByteString, which must be non-empty.
--
-- This is a partial function, please consider using 'uncons' instead.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
-- This is a partial function, please consider using 'uncons' instead.
-- This is a partial function. Please consider using 'uncons' instead.

Maybe even drop the "Please" – to me it sounds like the bytestring maintainers would benefit personally if users would use uncons instead.

(Dito for the other changes in this module.)

@Bodigrim
Copy link
Contributor Author

@sjakobi thanks, updated.

Comment on lines 393 to 396
-- This function is both partial and slow.
-- Please consider using either 'uncons', which is total, or, if you are
-- adamant that the argument is non-empty, 'Data.ByteString.Unsafe.unsafeHead',
-- which is branchless and fast.
Copy link
Member

Choose a reason for hiding this comment

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

This reads like we want users to pick either uncons or unsafeHead instead of head. Is this your goal?

How about something like this:

Suggested change
-- This function is both partial and slow.
-- Please consider using either 'uncons', which is total, or, if you are
-- adamant that the argument is non-empty, 'Data.ByteString.Unsafe.unsafeHead',
-- which is branchless and fast.
-- Related functions:
--
-- * 'uncons', for safe access to the 'head' and 'tail' of a ByteString
-- * 'Data.ByteString.Unsafe.unsafeHead'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is this your goal?

To be honest yes, I'd like to see things rolling away from head. What's your take about it?

Copy link
Member

Choose a reason for hiding this comment

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

I think users ought to be careful with head. But there still are legitimate use cases for it, where the code using head is preceded by a length check on the bytestring.

So I'm fine with warning about the partiality, and pointing out the alternatives.

But I don't want to push users to use unsafeHead rather than head. I think it's too dangerous to be used outside of the most performance-critical code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, let's make it less controversial. Updated.

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.

Thanks!

@Bodigrim Bodigrim merged commit 2b9416d into haskell:master Jul 2, 2022
@Bodigrim Bodigrim deleted the tweak-docs branch July 2, 2022 00:31
sjakobi pushed a commit that referenced this pull request Jul 25, 2022
(cherry picked from commit 2b9416d)
sjakobi pushed a commit that referenced this pull request Jul 25, 2022
(cherry picked from commit 2b9416d)
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.

None yet

2 participants