Skip to content

Commit 426587d

Browse files
committed
chore: Use unsafe indexing in internal binary functions.
1 parent 00fe186 commit 426587d

1 file changed

Lines changed: 17 additions & 28 deletions

File tree

src/DataFrame/Internal/Binary.hs

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1+
{-# LANGUAGE BangPatterns #-}
2+
13
module DataFrame.Internal.Binary where
24

3-
import Data.Bits
5+
import Data.Bits (Bits (unsafeShiftL, (.|.)))
6+
import Data.ByteString (toStrict)
47
import qualified Data.ByteString as BS
5-
import Data.Int
6-
import Data.Word
8+
import Data.ByteString.Builder (toLazyByteString, word32LE, word64LE)
9+
import qualified Data.ByteString.Unsafe as BS
10+
import Data.Int (Int32)
11+
import Data.Word (Word32, Word64, Word8)
712

813
littleEndianWord32 :: BS.ByteString -> Word32
914
littleEndianWord32 bytes
1015
| len >= 4 =
1116
assembleWord32
12-
(BS.index bytes 0)
13-
(BS.index bytes 1)
14-
(BS.index bytes 2)
15-
(BS.index bytes 3)
17+
(BS.unsafeIndex bytes 0)
18+
(BS.unsafeIndex bytes 1)
19+
(BS.unsafeIndex bytes 2)
20+
(BS.unsafeIndex bytes 3)
1621
| otherwise =
1722
assembleWord32
1823
(byteAtOrZero len bytes 0)
@@ -54,37 +59,21 @@ littleEndianInt32 = fromIntegral . littleEndianWord32
5459
{-# INLINE littleEndianInt32 #-}
5560

5661
word64ToLittleEndian :: Word64 -> BS.ByteString
57-
word64ToLittleEndian w =
58-
BS.pack
59-
[ fromIntegral w
60-
, fromIntegral (w `unsafeShiftR` 8)
61-
, fromIntegral (w `unsafeShiftR` 16)
62-
, fromIntegral (w `unsafeShiftR` 24)
63-
, fromIntegral (w `unsafeShiftR` 32)
64-
, fromIntegral (w `unsafeShiftR` 40)
65-
, fromIntegral (w `unsafeShiftR` 48)
66-
, fromIntegral (w `unsafeShiftR` 56)
67-
]
62+
word64ToLittleEndian = toStrict . toLazyByteString . word64LE
6863
{-# INLINE word64ToLittleEndian #-}
6964

7065
word32ToLittleEndian :: Word32 -> BS.ByteString
71-
word32ToLittleEndian w =
72-
BS.pack
73-
[ fromIntegral w
74-
, fromIntegral (w `unsafeShiftR` 8)
75-
, fromIntegral (w `unsafeShiftR` 16)
76-
, fromIntegral (w `unsafeShiftR` 24)
77-
]
66+
word32ToLittleEndian = toStrict . toLazyByteString . word32LE
7867
{-# INLINE word32ToLittleEndian #-}
7968

8069
byteAtOrZero :: Int -> BS.ByteString -> Int -> Word8
8170
byteAtOrZero len bytes i
82-
| i >= 0 && i < len = BS.index bytes i
71+
| i >= 0 && i < len = BS.unsafeIndex bytes i
8372
| otherwise = 0
8473
{-# INLINE byteAtOrZero #-}
8574

8675
assembleWord32 :: Word8 -> Word8 -> Word8 -> Word8 -> Word32
87-
assembleWord32 b0 b1 b2 b3 =
76+
assembleWord32 !b0 !b1 !b2 !b3 =
8877
fromIntegral b0
8978
.|. (fromIntegral b1 `unsafeShiftL` 8)
9079
.|. (fromIntegral b2 `unsafeShiftL` 16)
@@ -93,7 +82,7 @@ assembleWord32 b0 b1 b2 b3 =
9382

9483
assembleWord64 ::
9584
Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word8 -> Word64
96-
assembleWord64 b0 b1 b2 b3 b4 b5 b6 b7 =
85+
assembleWord64 !b0 !b1 !b2 !b3 !b4 !b5 !b6 !b7 =
9786
fromIntegral b0
9887
.|. (fromIntegral b1 `unsafeShiftL` 8)
9988
.|. (fromIntegral b2 `unsafeShiftL` 16)

0 commit comments

Comments
 (0)