8000 Inlining decoder functions · postgres-haskell/postgres-wire@f7ed159 · GitHub
[go: up one dir, main page]

Skip to content

Commit f7ed159

Browse files
Inlining decoder functions
1 parent 93415e0 commit f7ed159

File tree

1 file changed

+19
-1
lines changed
  • src/Database/PostgreSQL/Protocol/Store

1 file changed

+19
-1
lines changed

src/Database/PostgreSQL/Protocol/Store/Decode.hs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,51 +10,62 @@ import Control.Monad
1010
import Control.Applicative
1111

1212
-- Change to Ptr-based parser later
13-
data Decode a = Decode
13+
newtype Decode a = Decode
1414
{ runDecode :: B.ByteString -> Either String (B.ByteString, a)}
1515

1616
instance Functor Decode where
1717
fmap f p = Decode $ fmap (fmap f) . runDecode p
18+
{-# INLINE fmap #-}
1819

1920
instance Applicative Decode where
2021
pure x = Decode $ \bs -> Right (bs, x)
22+
{-# INLINE pure #-}
2123

2224
p1 <*> p2 = Decode $ \bs -> do
2325
(bs2, f) <- runDecode p1 bs
2426
(bs3, x) <- runDecode p2 bs2
2527
pure (bs3, f x)
28+
{-# INLINE (<*>) #-}
2629

2730
instance Monad Decode where
2831
return = pure
32+
{-# INLINE return #-}
2933

3034
p >>= f = Decode $ \bs -> do
3135
(bs2, x) <- runDecode p bs
3236
runDecode (f x) bs2
37+
{-# INLINE (>>=) #-}
3338

3439
fail = Decode . const . Left
40+
{-# INLINE fail #-}
3541

3642
checkLen :: B.ByteString -> Int -> Either String ()
3743
checkLen bs len | len > B.length bs = Left "too many bytes to read"
3844
| otherwise = Right ()
45+
{-# INLINE checkLen #-}
3946

4047

4148
takeWhile :: (Word8 -> Bool) -> Decode B.ByteString
4249
takeWhile f = Decode $ \bs -> Right . swap $ B.span f bs
50+
{-# INLINE takeWhile #-}
4351

4452
getByte :: Decode Word8
4553
getByte = Decode $ \bs -> do
4654
checkLen bs 1
4755
Right (B.drop 1 bs, B.index bs 0)
56+
{-# INLINE getByte #-}
4857

4958
getTwoBytes :: Decode (Word8, Word8)
5059
getTwoBytes = Decode $ \bs -> do
5160
checkLen bs 2
5261
Right (B.drop 2 bs, (B.index bs 0, B.index bs 1))
62+
{-# INLINE getTwoBytes #-}
5363

5464
getFourBytes :: Decode (Word8, Word8, Word8, Word8)
5565
getFourBytes = Decode $ \bs -> do
5666
checkLen bs 4
5767
Right (B.drop 4 bs, (B.index bs 0, B.index bs 1, B.index bs 2, B.index bs 3))
68+
{-# INLINE getFourBytes #-}
5869

5970
-----------
6071
-- Public
@@ -63,18 +74,22 @@ getByteString :: Int -> Decode B.ByteString
6374
getByteString len = Decode $ \bs -> do
6475
checkLen bs len
6576
Right . swap $ B.splitAt len bs
77+
{-# INLINE getByteString #-}
6678

6779
getByteStringNull :: Decode B.ByteString
6880
getByteStringNull = takeWhile (/= 0) <* getWord8
81+
{-# INLINE getByteStringNull #-}
6982

7083
getWord8 :: Decode Word8
7184
getWord8 = getByte
85+
{-# INLINE getWord8 #-}
7286

7387
getWord16BE :: Decode Word16
7488
getWord16BE = 8000 do
7589
(w1, w2) <- getTwoBytes
7690
pure $ fromIntegral w1 * 256 +
7791
fromIntegral w2
92+
{-# INLINE getWord16BE #-}
7893

7994
getWord32BE :: Decode Word32
8095
getWord32BE = do
@@ -83,10 +98,13 @@ getWord32BE = do
8398
fromIntegral w2 * 256 *256 +
8499
fromIntegral w3 * 256 +
85100
fromIntegral w4
101+
{-# INLINE getWord32BE #-}
86102

87103
getInt16BE :: Decode Int16
88104
getInt16BE = fromIntegral <$> getWord16BE
105+
{-# INLINE getInt16BE #-}
89106

90107
getInt32BE :: Decode Int32
91108
getInt32BE = fromIntegral <$> getWord32BE
109+
{-# INLINE getInt32BE #-}
92110

0 commit comments

Comments
 (0)
0