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

Improve documentation of customStrategy #692

Merged
merged 2 commits into from
Sep 19, 2024
Merged
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
27 changes: 14 additions & 13 deletions Data/ByteString/Builder/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1042,14 +1042,6 @@ maximalCopySize = 2 * L.smallChunkSize
------------------------------------------------------------------------------

-- | A buffer allocation strategy for executing 'Builder's.

-- The strategy
--
-- > 'AllocationStrategy' firstBufSize bufSize trim
--
-- states that the first buffer is of size @firstBufSize@, all following buffers
-- are of size @bufSize@, and a buffer of size @n@ filled with @k@ bytes should
-- be trimmed iff @trim k n@ is 'True'.
data AllocationStrategy = AllocationStrategy
(Maybe (Buffer, Int) -> IO Buffer)
{-# UNPACK #-} !Int
Expand All @@ -1060,11 +1052,20 @@ data AllocationStrategy = AllocationStrategy
{-# INLINE customStrategy #-}
customStrategy
:: (Maybe (Buffer, Int) -> IO Buffer)
-- ^ Buffer allocation function. If 'Nothing' is given, then a new first
-- buffer should be allocated. If @'Just' (oldBuf, minSize)@ is given,
-- then a buffer with minimal size @minSize@ must be returned. The
-- strategy may reuse the @oldBuf@, if it can guarantee that this
-- referentially transparent and @oldBuf@ is large enough.
-- ^ Buffer allocation function.
--
-- * If 'Nothing' is given, then a new first buffer should be allocated.
--
-- * If @'Just' (oldBuf, minSize)@ is given, then a buffer with minimal
-- size @minSize@ must be returned. The strategy may reuse @oldBuf@ only if
-- @oldBuf@ is large enough and the consumer can guarantee that this will
-- not result in a violation of referential transparency.
--
-- /Warning:/ for multithreaded programs, it is generally unsafe to reuse
-- buffers when using the consumers of 'Builder' in this package. For
-- example, if 'toLazyByteStringWith' is called with an
-- 'AllocationStrategy' that reuses buffers, evaluating the result by
-- multiple threads simultaneously may lead to corrupted output.
-> Int
-- ^ Default buffer size.
-> (Int -> Int -> Bool)
Expand Down
Loading