on 40e89fb help sign in
58 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 , byteArrayOverhead
17 )
19where
21-------------------------------------------------------------------------------
22-- Imports
23-------------------------------------------------------------------------------
25#include "MachDeps.h"
27import GHC.Base (realWorld#)
28import GHC.IO (IO(IO))
30-------------------------------------------------------------------------------
31-- API
32-------------------------------------------------------------------------------
34{-# INLINE unsafeInlineIO #-}
35unsafeInlineIO :: IO a -> a
36unsafeInlineIO (IO m) = case m realWorld# of (# _, r #) -> r
38-- | Returns the heap allocation overhead for allocating a byte array. Each
39-- heap object contains a one word header. Byte arrays contain the size of the
40-- array after the header.
41--
42-- See https://gitlab.haskell.org/ghc/ghc/-/wikis/commentary/rts/storage/heap-objects#arrays
43--
44byteArrayOverhead :: Int
45byteArrayOverhead = 2 * SIZEOF_HSWORD
47-- | When we allocate a byte array of size @k@ the allocator actually allocates
48-- memory of size @k + byteArrayOverhead@. @arrayPayloadSize n@ returns the
49-- size of the array in bytes that would result in an allocation of @n@ bytes.
50--
51arrayPayloadSize :: Int -> Int
52arrayPayloadSize n = let size = n - byteArrayOverhead in max size 0
54-- | Default maximum buffer size in bytes, for reading from and writing to IO
55-- devices, the value is 32KB minus GHC allocation overhead, which is a few
56-- bytes, so that the actual allocation is 32KB.
57defaultChunkSize :: Int
58defaultChunkSize = arrayPayloadSize (32 * 1024)