{-# language BangPatterns #-}
{-# language DerivingStrategies #-}
{-# language DuplicateRecordFields #-}
{-# language TypeFamilies #-}
{-# language MagicHash #-}
{-# language UnboxedTuples #-}
{-# language NamedFieldPuns #-}
module Data.Bytes.Chunks
(
Chunks(..)
, length
, concat
, concatU
, reverse
, reverseOnto
, fromBytes
, fromByteArray
, unsafeCopy
, hGetContents
) where
import Prelude hiding (length,concat,reverse)
import Control.Monad.ST.Run (runIntByteArrayST)
import Data.Bytes.Types (Bytes(Bytes))
import Data.Primitive (ByteArray(..),MutableByteArray(..))
import GHC.Exts (ByteArray#,MutableByteArray#)
import GHC.Exts (Int#,State#,Int(I#),(+#))
import GHC.ST (ST(..))
import System.IO (Handle)
import qualified GHC.Exts as Exts
import qualified Data.Primitive as PM
import qualified Data.Bytes.Types as B
import qualified Data.Bytes as Bytes
data Chunks
= ChunksCons {-# UNPACK #-} !Bytes !Chunks
| ChunksNil
deriving stock (Show)
instance Semigroup Chunks where
ChunksNil <> a = a
cs@(ChunksCons _ _) <> ChunksNil = cs
as@(ChunksCons _ _) <> bs@(ChunksCons _ _) =
reverseOnto bs (reverse as)
instance Monoid Chunks where
mempty = ChunksNil
instance Eq Chunks where
a == b = concat a == concat b
concat :: Chunks -> Bytes
concat x = case x of
ChunksNil -> Bytes.empty
ChunksCons b y -> case y of
ChunksNil -> b
ChunksCons c z -> case concatFollowing2 b c z of
(# len, r #) -> Bytes (ByteArray r) 0 (I# len)
concatU :: Chunks -> ByteArray
concatU x = case x of
ChunksNil -> mempty
ChunksCons b y -> case y of
ChunksNil -> Bytes.toByteArray b
ChunksCons c z -> case concatFollowing2 b c z of
(# _, r #) -> ByteArray r
concatFollowing2 :: Bytes -> Bytes -> Chunks -> (# Int#, ByteArray# #)
concatFollowing2
(Bytes{array=c,offset=coff,length=szc})
(Bytes{array=d,offset=doff,length=szd}) ds =
let !(I# x, ByteArray y) = runIntByteArrayST $ do
let !szboth = szc + szd
!len = chunksLengthGo szboth ds
dst <- PM.newByteArray len
PM.copyByteArray dst 0 c coff szc
PM.copyByteArray dst szc d doff szd
!len2 <- unsafeCopy dst szboth ds
result <- PM.unsafeFreezeByteArray dst
pure (len2,result)
in (# x, y #)
length :: Chunks -> Int
length = chunksLengthGo 0
chunksLengthGo :: Int -> Chunks -> Int
chunksLengthGo !n ChunksNil = n
chunksLengthGo !n (ChunksCons (Bytes{B.length=len}) cs) =
chunksLengthGo (n + len) cs
unsafeCopy ::
MutableByteArray s
-> Int
-> Chunks
-> ST s Int
{-# inline unsafeCopy #-}
unsafeCopy (MutableByteArray dst) (I# off) cs = ST
(\s0 -> case copy# dst off cs s0 of
(# s1, nextOff #) -> (# s1, I# nextOff #)
)
copy# :: MutableByteArray# s -> Int# -> Chunks -> State# s -> (# State# s, Int# #)
copy# _ off ChunksNil s0 = (# s0, off #)
copy# marr off (ChunksCons (Bytes{B.array,B.offset,B.length=len}) cs) s0 =
case Exts.copyByteArray# (unBa array) (unI offset) marr off (unI len) s0 of
s1 -> copy# marr (off +# unI len) cs s1
reverse :: Chunks -> Chunks
reverse = reverseOnto ChunksNil
reverseOnto :: Chunks -> Chunks -> Chunks
reverseOnto !x ChunksNil = x
reverseOnto !x (ChunksCons y ys) =
reverseOnto (ChunksCons y x) ys
unI :: Int -> Int#
unI (I# i) = i
unBa :: ByteArray -> ByteArray#
unBa (ByteArray x) = x
hGetContents :: Handle -> IO Chunks
hGetContents !h = do
result <- go ChunksNil
pure $! reverse result
where
go !acc = do
c <- Bytes.hGet h chunkSize
let !r = ChunksCons c acc
if Bytes.length c == chunkSize
then go r
else pure r
chunkSize :: Int
chunkSize = 16384 - 16
fromBytes :: Bytes -> Chunks
fromBytes !b = ChunksCons b ChunksNil
fromByteArray :: ByteArray -> Chunks
fromByteArray !b = fromBytes (Bytes.fromByteArray b)