Skip to content

Commit 398daeb

Browse files
committed
MutByteArray.new auto-pins memory if size > LARGE_BLOCK_THREASHOLD
BLOCK_SIZE = 4 * 1024 = 4096 bytes LARGE_BLOCK_THREASHOLD = BLOCK_SIZE * 8 / 10 = 3276 bytes
1 parent b186cfa commit 398daeb

File tree

1 file changed

+27
-10
lines changed
  • core/src/Streamly/Internal/Data/MutByteArray

1 file changed

+27
-10
lines changed

core/src/Streamly/Internal/Data/MutByteArray/Type.hs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,20 @@ empty = unsafePerformIO $ new 0
155155
nil :: MutByteArray
156156
nil = empty
157157

158-
-- XXX add "newRoundedUp" to round up the large size to the next page boundary
159-
-- and return the allocated size.
160-
{-# INLINE new #-}
161-
new :: Int -> IO MutByteArray
162-
new nbytes | nbytes < 0 =
163-
errorWithoutStackTrace "newByteArray: size must be >= 0"
164-
new (I# nbytes) = IO $ \s ->
165-
case newByteArray# nbytes s of
158+
-- 4000
159+
{-# INLINE _BLOCK_SIZE #-}
160+
_BLOCK_SIZE :: Int
161+
_BLOCK_SIZE = 4 * 1024
162+
163+
-- 3276
164+
{-# INLINE _LARGE_BLOCK_THRESHOLD #-}
165+
_LARGE_BLOCK_THRESHOLD :: Int
166+
_LARGE_BLOCK_THRESHOLD = _BLOCK_SIZE * 8 / 10
167+
168+
{-# INLINE pinnedNewRaw #-}
169+
pinnedNewRaw :: Int -> IO MutByteArray
170+
pinnedNewRaw (I# nbytes) = IO $ \s ->
171+
case newPinnedByteArray# nbytes s of
166172
(# s', mbarr# #) ->
167173
let c = MutByteArray mbarr#
168174
in (# s', c #)
@@ -171,8 +177,19 @@ new (I# nbytes) = IO $ \s ->
171177
pinnedNew :: Int -> IO MutByteArray
172178
pinnedNew nbytes | nbytes < 0 =
173179
errorWithoutStackTrace "pinnedNew: size must be >= 0"
174-
pinnedNew (I# nbytes) = IO $ \s ->
175-
case newPinnedByteArray# nbytes s of
180+
pinnedNew nbytes = pinnedNewRaw nbytes
181+
182+
-- XXX add "newRoundedUp" to round up the large size to the next page boundary
183+
-- and return the allocated size.
184+
-- Uses the pinned version of allocated if the size required is >
185+
-- _LARGE_BLOCK_THRESHOLD
186+
{-# INLINE new #-}
187+
new :: Int -> IO MutByteArray
188+
new nbytes | nbytes > _LARGE_BLOCK_THRESHOLD = pinnedNewRaw nbytes
189+
new nbytes | nbytes < 0 =
190+
errorWithoutStackTrace "newByteArray: size must be >= 0"
191+
new (I# nbytes) = IO $ \s ->
192+
case newByteArray# nbytes s of
176193
(# s', mbarr# #) ->
177194
let c = MutByteArray mbarr#
178195
in (# s', c #)

0 commit comments

Comments
 (0)