git.iko.soy/typeable/attoparsec
on master help sign in
A small improvement to the performance of scan

Take advantage of the fact that we usually expect a single chunk

commit7d5ffd291e0c27598dce6bef4c6a1447a26335e2 authorBryan O'Sullivan <bos@serpentine.com> dateThu 3 Feb 2011, 18:57 (16 years ago) parentb699b04
1 file changed+5−1
Data/Attoparsec/Internal.hs+5−1
Data/Attoparsec/Internal.hs+5−1
@@ -418,7 +418,11 @@ takeWhile p = (B.concat . reverse) `fmap` go []
418418-- combinators such as 'many', because such parsers loop until a
419419-- failure occurs. Careless use will thus result in an infinite loop.
420420scan :: s -> (s -> Word8 -> Maybe s) -> Parser B.ByteString
421−scan s0 p = (B.concat . reverse) `fmap` go [] s0
421+scan s0 p = do
422+ chunks <- go [] s0
423+ case chunks of
424+ [x] -> return x
425+ xs -> return . B.concat . reverse $ xs
422426 where
423427 go acc s1 = do
424428 input <- wantInput