on master help sign in
Speed up intersperse?

--HG-- extra : convert_revision : c96f970c214d88d211308f33c41114ed7b2185bd

commitc67c26b182c0f07fd5632a8a2a3b9dac6b0e09cb authorBryan O'Sullivan <bos@serpentine.com> dateWed 2 Dec 2009, 06:11 (17 years ago) parentcd6f8af
1 file changed+12−10
Data/Text/Fusion/Common.hs+12−10
Data/Text/Fusion/Common.hs+12−10
@@ -296,22 +296,24 @@ map f (Stream next0 s0 len) = Stream next s0 len
296296 map f (map g s) = map (\x -> f (g x)) s
297297 #-}
298298
299+data I s = I1 !s
300+ | I2 !s {-# UNPACK #-} !Char
301+ | I3 !s
302+
299303-- | /O(n)/ Take a character and place it between each of the
300304-- characters of a 'Stream Char'.
301305intersperse :: Char -> Stream Char -> Stream Char
302−intersperse c (Stream next0 s0 len) = Stream next (s0 :*: N :*: S1) len
306+intersperse c (Stream next0 s0 len) = Stream next (I1 s0) len
303307 where
304− {-# INLINE next #-}
305− next (s :*: N :*: S1) = case next0 s of
308+ next (I1 s) = case next0 s of
306309 Done -> Done
307− Skip s' -> Skip (s' :*: N :*: S1)
308− Yield x s' -> Skip (s' :*: J x :*: S1)
309− next (s :*: J x :*: S1) = Yield x (s :*: N :*: S2)
310− next (s :*: N :*: S2) = case next0 s of
310+ Skip s' -> Skip (I1 s')
311+ Yield x s' -> Skip (I2 s' x)
312+ next (I2 s x) = Yield x (I3 s)
313+ next (I3 s) = case next0 s of
311314 Done -> Done
312− Skip s' -> Skip (s' :*: N :*: S2)
313− Yield x s' -> Yield c (s' :*: J x :*: S1)
314− next _ = internalError "intersperse"
315+ Skip s' -> Skip (I3 s')
316+ Yield x s' -> Yield c (I2 s' x)
315317{-# INLINE [0] intersperse #-}
316318
317319-- ----------------------------------------------------------------------------