on master help sign in
Remove eta reduction in applicative
commit3f803aaeaf890296de536bdec51a259522bf57c8 authorHarendra Kumar <harendra@composewell.com> dateThu 16 Feb 2023, 11:48 (4 years ago) parentd33205f
1 file changed+7−2
core/src/Streamly/Internal/Data/Parser/ParserK/Type.hs+7−2
core/src/Streamly/Internal/Data/Parser/ParserK/Type.hs+7−2
@@ -78,6 +78,9 @@ instance Functor m => Functor (Step a m) where
7878 fmap f (Continue n k) = Continue n (fmap (fmap f) . k)
7979 fmap _ (Error n e) = Error n e
8080
81+-- Note: Passing position index separately instead of passing it with the
82+-- result causes huge regression in expression parsing becnhmarks.
83+
8184-- | The parser's result.
8285--
8386-- Int is the position index into the current input array. Could be negative.
@@ -114,6 +117,8 @@ newtype Parser a m b = MkParser
114117 -- one more function call to get the input. This could be helpful
115118 -- for cases where we process just one element per call.
116119 --
120+ -- Do not eta reduce the applications of this continuation.
121+ --
117122 (ParseResult b -> Int -> Input a -> m (Step a m r))
118123 -- XXX Maintain and pass the original position in the stream. that
119124 -- way we can also report better errors. Use a Context structure for
@@ -196,8 +201,8 @@ instance Monad m => Applicative (Parser a m) where
196201 {-# INLINE (<*) #-}
197202 p1 <* p2 = MkParser $ \k n st arr ->
198203 let k1 (Success n1 b) s1 input =
199− let k2 (Success n2 _) = k (Success n2 b)
200− k2 (Failure n2 e) = k (Failure n2 e)
204+ let k2 (Success n2 _) s2 input2 = k (Success n2 b) s2 input2
205+ k2 (Failure n2 e) s2 input2 = k (Failure n2 e) s2 input2
201206 in runParser p2 k2 n1 s1 input
202207 k1 (Failure n1 e) s1 input = k (Failure n1 e) s1 input
203208 in runParser p1 k1 n st arr