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

Apply the one-shot trick to Builder #608

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion Data/ByteString/Builder/Internal.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE ScopedTypeVariables, CPP, BangPatterns, RankNTypes, TupleSections #-}
{-# LANGUAGE ScopedTypeVariables, CPP, BangPatterns, RankNTypes, TupleSections,
PatternSynonyms #-}
{-# LANGUAGE Unsafe #-}
{-# OPTIONS_HADDOCK not-home #-}
-- | Copyright : (c) 2010 - 2011 Simon Meier
Expand Down Expand Up @@ -137,6 +138,7 @@
import qualified Data.ByteString.Lazy.Internal as L
import qualified Data.ByteString.Short.Internal as Sh

import qualified GHC.Exts

Check warning on line 141 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.0)

The qualified import of ‘GHC.Exts’ is redundant

Check warning on line 141 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.0)

The qualified import of ‘GHC.Exts’ is redundant
import qualified GHC.IO.Buffer as IO (Buffer(..), newByteBuffer)
import GHC.IO.Handle.Internals (wantWritableHandle, flushWriteBuffer)
import GHC.IO.Handle.Types (Handle__, haByteBuffer, haBufferMode)
Expand Down Expand Up @@ -326,7 +328,31 @@
-- They are 'Monoid's where
-- 'mempty' is the zero-length sequence and
-- 'mappend' is concatenation, which runs in /O(1)/.
#if (MIN_VERSION_base(4,10,0))
newtype Builder = Builder' (forall r. BuildStep r -> BuildStep r)
pattern Builder :: (forall r. BuildStep r -> BuildStep r) -> Builder
pattern Builder f <- Builder' f
where
-- We want to tell the compiler to eta-expand over the BufferRange of a
-- BuildStep the same as it eta-expands over State# tokens.
-- This is important for loops such as `foldMap (B.word8 . fromIntegral) xs`
-- (see https://gitlab.haskell.org/ghc/ghc/-/issues/23822#note_520437)
-- where otherwise the compiler thinks `empty bs` is worth sharing.
-- The usual way to do that is via GHC.Exts.oneShot on `\br`.
--
-- By contrast, we refrain from marking the BuildStep argument as one-shot,
-- because that could lead to undesirable duplication of work in an
-- expression like
--
-- > let t = expensive 42 in stimes 1000 (Builder $ \bs br -> ... t ...)
--
-- Marking `\bs` one-shot as well tells the compiler that it's fine to float
-- the definition of `t` inside the builder -- thus executing `expensive`
-- 1000 times instead of just once.
Builder f = Builder' (\bs -> GHC.Exts.oneShot $ \br -> f bs br)
#else
newtype Builder = Builder (forall r. BuildStep r -> BuildStep r)
#endif

-- | Construct a 'Builder'. In contrast to 'BuildStep's, 'Builder's are
-- referentially transparent.
Expand Down Expand Up @@ -364,7 +390,7 @@
runBuilderWith :: Builder -- ^ 'Builder' to run
-> BuildStep a -- ^ Continuation 'BuildStep'
-> BuildStep a
runBuilderWith (Builder b) = b

Check warning on line 393 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 9.6)

Pattern match(es) are non-exhaustive

Check warning on line 393 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (macOS-latest, latest)

Pattern match(es) are non-exhaustive

Check warning on line 393 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.8)

Pattern match(es) are non-exhaustive

Check warning on line 393 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.6)

Pattern match(es) are non-exhaustive

Check warning on line 393 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.4)

Pattern match(es) are non-exhaustive

Check warning on line 393 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 9.0)

Pattern match(es) are non-exhaustive

Check warning on line 393 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 9.2)

Pattern match(es) are non-exhaustive

Check warning on line 393 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 9.4)

Pattern match(es) are non-exhaustive

Check warning on line 393 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.2)

Pattern match(es) are non-exhaustive

Check warning on line 393 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.10)

Pattern match(es) are non-exhaustive

Check warning on line 393 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / bounds-checking

Pattern match(es) are non-exhaustive

Check warning on line 393 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / windows-build (9.2)

Pattern match(es) are non-exhaustive

Check warning on line 393 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / windows-build (9.6)

Pattern match(es) are non-exhaustive

Check warning on line 393 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / windows-build (9.4)

Pattern match(es) are non-exhaustive

-- | The 'Builder' denoting a zero-length sequence of bytes. This function is
-- only exported for use in rewriting rules. Use 'mempty' otherwise.
Expand All @@ -380,7 +406,7 @@
-- rules. Use 'mappend' otherwise.
{-# INLINE[1] append #-}
append :: Builder -> Builder -> Builder
append (Builder b1) (Builder b2) = Builder $ b1 . b2

Check warning on line 409 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 9.6)

Pattern match(es) are non-exhaustive

Check warning on line 409 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (macOS-latest, latest)

Pattern match(es) are non-exhaustive

Check warning on line 409 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.8)

Pattern match(es) are non-exhaustive

Check warning on line 409 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.6)

Pattern match(es) are non-exhaustive

Check warning on line 409 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.4)

Pattern match(es) are non-exhaustive

Check warning on line 409 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 9.0)

Pattern match(es) are non-exhaustive

Check warning on line 409 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 9.2)

Pattern match(es) are non-exhaustive

Check warning on line 409 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 9.4)

Pattern match(es) are non-exhaustive

Check warning on line 409 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.2)

Pattern match(es) are non-exhaustive

Check warning on line 409 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.10)

Pattern match(es) are non-exhaustive

Check warning on line 409 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / bounds-checking

Pattern match(es) are non-exhaustive

Check warning on line 409 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / windows-build (9.2)

Pattern match(es) are non-exhaustive

Check warning on line 409 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / windows-build (9.6)

Pattern match(es) are non-exhaustive

Check warning on line 409 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / windows-build (9.4)

Pattern match(es) are non-exhaustive

instance Semigroup Builder where
{-# INLINE (<>) #-}
Expand Down Expand Up @@ -496,7 +522,7 @@
-- | Run a 'Builder' as a side-effect of a @'Put' ()@ action.
{-# INLINE[1] putBuilder #-}
putBuilder :: Builder -> Put ()
putBuilder (Builder b) = Put $ \k -> b (k ())

Check warning on line 525 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 9.6)

Pattern match(es) are non-exhaustive

Check warning on line 525 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (macOS-latest, latest)

Pattern match(es) are non-exhaustive

Check warning on line 525 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.8)

Pattern match(es) are non-exhaustive

Check warning on line 525 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.6)

Pattern match(es) are non-exhaustive

Check warning on line 525 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.4)

Pattern match(es) are non-exhaustive

Check warning on line 525 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 9.0)

Pattern match(es) are non-exhaustive

Check warning on line 525 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 9.2)

Pattern match(es) are non-exhaustive

Check warning on line 525 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 9.4)

Pattern match(es) are non-exhaustive

Check warning on line 525 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.2)

Pattern match(es) are non-exhaustive

Check warning on line 525 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 8.10)

Pattern match(es) are non-exhaustive

Check warning on line 525 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / bounds-checking

Pattern match(es) are non-exhaustive

Check warning on line 525 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / windows-build (9.2)

Pattern match(es) are non-exhaustive

Check warning on line 525 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / windows-build (9.6)

Pattern match(es) are non-exhaustive

Check warning on line 525 in Data/ByteString/Builder/Internal.hs

View workflow job for this annotation

GitHub Actions / windows-build (9.4)

Pattern match(es) are non-exhaustive

-- | Convert a @'Put' ()@ action to a 'Builder'.
{-# INLINE fromPut #-}
Expand Down
Loading