on bc0da07 help sign in
56 lines 1.9 kB history raw
1{-# LANGUAGE UnboxedTuples #-}
3-- |
4-- Module : Streamly.Internal.System.IO
5-- Copyright : (c) 2020 Composewell Technologies
6-- License : BSD-3-Clause
7-- Maintainer : streamly@composewell.com
8-- Stability : experimental
9-- Portability : GHC
10--
12module Streamly.Internal.System.IO
13 ( defaultChunkSize
14 , arrayPayloadSize
15 , unsafeInlineIO
16 )
18where
20-------------------------------------------------------------------------------
21-- Imports
22-------------------------------------------------------------------------------
24import Foreign.Storable (Storable(sizeOf))
25import GHC.Base (realWorld#)
26import GHC.IO (IO(IO))
28-------------------------------------------------------------------------------
29-- API
30-------------------------------------------------------------------------------
32{-# INLINE unsafeInlineIO #-}
33unsafeInlineIO :: IO a -> a
34unsafeInlineIO (IO m) = case m realWorld# of (# _, r #) -> r
36-- | Returns the heap allocation overhead for allocating a byte array. Each
37-- heap object contains a one word header. Byte arrays contain the size of the
38-- array after the header.
39--
40-- See https://gitlab.haskell.org/ghc/ghc/-/wikis/commentary/rts/storage/heap-objects#arrays
41--
42byteArrayOverhead :: Int
43byteArrayOverhead = 2 * sizeOf (undefined :: Word)
45-- | When we allocate a byte array of size @k@ the allocator actually allocates
46-- memory of size @k + byteArrayOverhead@. @arrayPayloadSize n@ returns the
47-- size of the array in bytes that would result in an allocation of @n@ bytes.
48--
49arrayPayloadSize :: Int -> Int
50arrayPayloadSize n = let size = n - byteArrayOverhead in max size 0
52-- | Default maximum buffer size in bytes, for reading from and writing to IO
53-- devices, the value is 32KB minus GHC allocation overhead, which is a few
54-- bytes, so that the actual allocation is 32KB.
55defaultChunkSize :: Int
56defaultChunkSize = arrayPayloadSize (32 * 1024)