Last active
March 6, 2022 15:24
-
-
Save noughtmare/8902ddec65fedae9fb24d3826dbe11a0 to your computer and use it in GitHub Desktop.
An alternative implementation of `pack` for bytestrings that can fuse.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# LANGUAGE MagicHash #-} | |
| {-# LANGUAGE UnboxedTuples #-} | |
| module Main (main) where | |
| import Test.Tasty.Bench | |
| import qualified Data.ByteString as S | |
| import qualified Data.ByteString.Internal as SI | |
| import Data.Word | |
| import Foreign.Ptr | |
| import Data.Bits | |
| import System.IO.Unsafe | |
| import GHC.Exts | |
| import GHC.IO | |
| import GHC.Word | |
| data MBA = MBA !(MutableByteArray# RealWorld) | |
| data DMBA = DMBA {-# UNPACK #-} !Int {-# UNPACK #-} !MBA | |
| newMBA :: Int -> IO MBA | |
| newMBA (I# n) = IO $ \s -> | |
| case newPinnedByteArray# n s of | |
| (# s, mba #) -> (# s, MBA mba #) | |
| newDMBA :: Int -> IO DMBA | |
| newDMBA n = DMBA 0 <$> newMBA n | |
| writeMBA :: MBA -> Int -> Word8 -> IO () | |
| writeMBA (MBA mba) (I# i) (W8# x) = IO $ \s -> (# writeWord8Array# mba i x s, () #) | |
| sizeMBA :: MBA -> IO Int | |
| sizeMBA (MBA mba) = IO $ \s -> | |
| case getSizeofMutableByteArray# mba s of | |
| (# s, size #) -> (# s, I# size #) | |
| resizeMBA :: MBA -> Int -> IO MBA | |
| resizeMBA (MBA mba) (I# n) = IO $ \s -> | |
| case resizeMutableByteArray# mba n s of | |
| (# s, mba #) -> (# s, MBA mba #) | |
| growFun cap = cap `unsafeShiftL` 1 | |
| -- growFun cap = (3 * cap) `unsafeShiftR` 1 | |
| push :: DMBA -> Word8 -> IO DMBA | |
| push (DMBA n mba) x = do | |
| size <- sizeMBA mba | |
| if n < size | |
| then DMBA (n + 1) mba <$ writeMBA mba n x | |
| else do | |
| let size' = growFun size | |
| mba <- resizeMBA mba size' | |
| DMBA (n + 1) mba <$ writeMBA mba n x | |
| {-# INLINE push #-} | |
| toByteString :: DMBA -> IO S.ByteString | |
| toByteString (DMBA (I# n) (MBA mba)) = SI.create (I# n) $ \(Ptr addr) -> | |
| IO $ \s -> | |
| let s' = shrinkMutableByteArray# mba n s in | |
| (# copyMutableByteArrayToAddr# mba 0# addr 0# s', () #) | |
| pack :: [Word8] -> S.ByteString | |
| pack xs = unsafeDupablePerformIO $ do | |
| dmba <- newDMBA 8 | |
| foldr | |
| (\x go dmba -> push dmba x >>= go) | |
| (\ dmba@(DMBA n _) -> toByteString dmba) | |
| xs | |
| dmba | |
| {-# INLINE pack #-} | |
| mkBench f = [bench ("10^" ++ show i) $ f (10 ^ i) | i <- [2,4,6]] | |
| main :: IO () | |
| main = defaultMain | |
| [ bgroup "pack" $ mkBench (\n -> nf S.pack (replicate n 0)) | |
| , bgroup "pack fusable" $ mkBench (\n -> nf (S.pack . replicate n) 0) | |
| , bgroup "my-pack" $ mkBench (\n -> nf pack (replicate n 0)) | |
| , bgroup "my-pack fusable" $ mkBench (\n -> nf (pack . replicate n) 0) | |
| ] |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results: