on master help sign in
Some careful performance tweaking. Everything twice as fast.
commitfd680fe7fcc251b30b657a5a008ebc99c48e440e authorLennart Kolmodin <kolmodin@gmail.com> dateWed 25 Apr 2012, 21:47 (14 years ago) parentfe6389e
1 file changed+6−8
Data/Binary/Bits/Get.hs+6−8
Data/Binary/Bits/Get.hs+6−8
@@ -146,13 +146,13 @@ import GHC.Word
146146--ipv6Header = 'runBitGet' ('block' ipv6headerblock)
147147-- @
148148
149−data S = S !ByteString -- Input
150− !Int -- Bit offset (0-7)
149+data S = S {-# UNPACK #-} !ByteString -- Input
150+ {-# UNPACK #-} !Int -- Bit offset (0-7)
151151 deriving (Show)
152152
153153-- | A block that will be read with only one boundry check. Needs to know the
154154-- number of bits in advance.
155−data Block a = Block !Int (S -> a)
155+data Block a = Block Int (S -> a)
156156
157157instance Functor Block where
158158 fmap f (Block i p) = Block i (\s -> f (p s))
@@ -170,8 +170,9 @@ instance Applicative Block where
170170block :: Block a -> BitGet a
171171block (Block i p) = do
172172 ensureBits i
173− modifyState (\s -> (incS i s, p s))
174−
173+ s <- getState
174+ putState $! (incS i s)
175+ return $! p s
175176
176177incS :: Int -> S -> S
177178incS o (S bs n) =
@@ -373,9 +374,6 @@ getState = B $ \s -> return (s,s)
373374putState :: S -> BitGet ()
374375putState s = B $ \_ -> return (s,())
375376
376−modifyState :: (S -> (S,a)) -> BitGet a
377−modifyState f = B $ \s -> return (f s)
378−
379377-- | Make sure there are at least @n@ bits.
380378ensureBits :: Int -> BitGet ()
381379ensureBits n = do