Skip to content

Commit a4dc7a0

Browse files
adamgundryBodigrim
authored andcommitted
Improve documentation of customStrategy (#692)
* Clarify documentation of 'customStrategy' based on #690 * Remove outdated comments on AllocationStrategy (These were not visible in the Haddock output anyway.)
1 parent 22dedee commit a4dc7a0

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

Data/ByteString/Builder/Internal.hs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,14 +1042,6 @@ maximalCopySize = 2 * L.smallChunkSize
10421042
------------------------------------------------------------------------------
10431043

10441044
-- | A buffer allocation strategy for executing 'Builder's.
1045-
1046-
-- The strategy
1047-
--
1048-
-- > 'AllocationStrategy' firstBufSize bufSize trim
1049-
--
1050-
-- states that the first buffer is of size @firstBufSize@, all following buffers
1051-
-- are of size @bufSize@, and a buffer of size @n@ filled with @k@ bytes should
1052-
-- be trimmed iff @trim k n@ is 'True'.
10531045
data AllocationStrategy = AllocationStrategy
10541046
(Maybe (Buffer, Int) -> IO Buffer)
10551047
{-# UNPACK #-} !Int
@@ -1060,11 +1052,20 @@ data AllocationStrategy = AllocationStrategy
10601052
{-# INLINE customStrategy #-}
10611053
customStrategy
10621054
:: (Maybe (Buffer, Int) -> IO Buffer)
1063-
-- ^ Buffer allocation function. If 'Nothing' is given, then a new first
1064-
-- buffer should be allocated. If @'Just' (oldBuf, minSize)@ is given,
1065-
-- then a buffer with minimal size @minSize@ must be returned. The
1066-
-- strategy may reuse the @oldBuf@, if it can guarantee that this
1067-
-- referentially transparent and @oldBuf@ is large enough.
1055+
-- ^ Buffer allocation function.
1056+
--
1057+
-- * If 'Nothing' is given, then a new first buffer should be allocated.
1058+
--
1059+
-- * If @'Just' (oldBuf, minSize)@ is given, then a buffer with minimal
1060+
-- size @minSize@ must be returned. The strategy may reuse @oldBuf@ only if
1061+
-- @oldBuf@ is large enough and the consumer can guarantee that this will
1062+
-- not result in a violation of referential transparency.
1063+
--
1064+
-- /Warning:/ for multithreaded programs, it is generally unsafe to reuse
1065+
-- buffers when using the consumers of 'Builder' in this package. For
1066+
-- example, if 'toLazyByteStringWith' is called with an
1067+
-- 'AllocationStrategy' that reuses buffers, evaluating the result by
1068+
-- multiple threads simultaneously may lead to corrupted output.
10681069
-> Int
10691070
-- ^ Default buffer size.
10701071
-> (Int -> Int -> Bool)

0 commit comments

Comments
 (0)