Skip to content

Commit

Permalink
Replace _BLOCK_SIZE & _LARGE_BLOCK_THRESHOLD w existing definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
adithyaov committed Dec 5, 2024
1 parent 1098d1f commit 867c0f7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
16 changes: 2 additions & 14 deletions core/src/Streamly/Internal/Data/MutArray/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,8 @@ import Streamly.Internal.Data.MutByteArray.Type
, PinnedState(..)
, getMutByteArray#
, unsafePutSlice
, blockSize
, largeObjectThreshold
)
import Streamly.Internal.Data.Unbox (Unbox(..))
import GHC.Base
Expand Down Expand Up @@ -824,20 +826,6 @@ swapIndices i1 i2 MutArray{..} = liftIO $ do
-- Rounding
-------------------------------------------------------------------------------

-- XXX Should we use bitshifts in calculations or it gets optimized by the
-- compiler/processor itself?
--
-- | The page or block size used by the GHC allocator. Allocator allocates at
-- least a block and then allocates smaller allocations from within a block.
blockSize :: Int
blockSize = 4 * 1024

-- | Allocations larger than 'largeObjectThreshold' are in multiples of block
-- size and are always pinned. The space beyond the end of a large object up to
-- the end of the block is unused.
largeObjectThreshold :: Int
largeObjectThreshold = (blockSize * 8) `div` 10

-- XXX Should be done only when we are using the GHC allocator.
-- | Round up an array larger than 'largeObjectThreshold' to use the whole
-- block.
Expand Down
28 changes: 18 additions & 10 deletions core/src/Streamly/Internal/Data/MutByteArray/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ module Streamly.Internal.Data.MutByteArray.Type
, unsafePinnedAsPtr
, unsafeAsPtr

-- ** Capacity Management
, blockSize
, largeObjectThreshold

-- ** Deprecated
, MutableByteArray
, getMutableByteArray#
Expand Down Expand Up @@ -155,15 +159,19 @@ empty = unsafePerformIO $ new 0
nil :: MutByteArray
nil = empty

-- 4000
{-# INLINE _BLOCK_SIZE #-}
_BLOCK_SIZE :: Int
_BLOCK_SIZE = 4 * 1024
-- XXX Should we use bitshifts in calculations or it gets optimized by the
-- compiler/processor itself?
--
-- | The page or block size used by the GHC allocator. Allocator allocates at
-- least a block and then allocates smaller allocations from within a block.
blockSize :: Int
blockSize = 4 * 1024

-- 3276
{-# INLINE _LARGE_BLOCK_THRESHOLD #-}
_LARGE_BLOCK_THRESHOLD :: Int
_LARGE_BLOCK_THRESHOLD = (_BLOCK_SIZE * 8) `div` 10
-- | Allocations larger than 'largeObjectThreshold' are in multiples of block
-- size and are always pinned. The space beyond the end of a large object up to
-- the end of the block is unused.
largeObjectThreshold :: Int
largeObjectThreshold = (blockSize * 8) `div` 10

{-# INLINE pinnedNewRaw #-}
pinnedNewRaw :: Int -> IO MutByteArray
Expand All @@ -182,10 +190,10 @@ pinnedNew nbytes = pinnedNewRaw nbytes
-- XXX add "newRoundedUp" to round up the large size to the next page boundary
-- and return the allocated size.
-- Uses the pinned version of allocated if the size required is >
-- _LARGE_BLOCK_THRESHOLD
-- largeObjectThreshold
{-# INLINE new #-}
new :: Int -> IO MutByteArray
new nbytes | nbytes > _LARGE_BLOCK_THRESHOLD = pinnedNewRaw nbytes
new nbytes | nbytes > largeObjectThreshold = pinnedNewRaw nbytes
new nbytes | nbytes < 0 =
errorWithoutStackTrace "newByteArray: size must be >= 0"
new (I# nbytes) = IO $ \s ->
Expand Down

0 comments on commit 867c0f7

Please sign in to comment.