Commits

Bryan O'Sullivan committed 82660e2

Fix up some performance problems using more specific strict types.

Comments (0)

Files changed (1)

Data/Text/Fusion/Common.hs

           next (x:xs)   = Yield x xs
 
 unstreamList :: Stream a -> [a]
-{-# INLINE [0] unstreamList #-}
 unstreamList (Stream next s0 _len) = unfold s0
     where unfold !s = case next s of
                         Done       -> []
                         Skip s'    -> unfold s'
                         Yield x s' -> x : unfold s'
+{-# INLINE [0] unstreamList #-}
 
 {-# RULES "STREAM streamList/unstreamList fusion" forall s. streamList (unstreamList s) = s #-}
 
 -- ----------------------------------------------------------------------------
 -- * Basic stream functions
 
+data C s = C0 !s
+         | C1 !s
+
 -- | /O(n)/ Adds a character to the front of a Stream Char.
 cons :: Char -> Stream Char -> Stream Char
-cons w (Stream next0 s0 len) = Stream next (S2 :*: s0) (len+1)
+cons w (Stream next0 s0 len) = Stream next (C1 s0) (len+1)
     where
-      {-# INLINE next #-}
-      next (S2 :*: s) = Yield w (S1 :*: s)
-      next (S1 :*: s) = case next0 s of
+      next (C1 s) = Yield w (C0 s)
+      next (C0 s) = case next0 s of
                           Done -> Done
-                          Skip s' -> Skip (S1 :*: s')
-                          Yield x s' -> Yield x (S1 :*: s')
+                          Skip s' -> Skip (C0 s')
+                          Yield x s' -> Yield x (C0 s')
 {-# INLINE [0] cons #-}
 
 -- | /O(n)/ Adds a character to the end of a stream.
 snoc :: Stream Char -> Char -> Stream Char
 snoc (Stream next0 xs0 len) w = Stream next (J xs0) (len+1)
   where
-    {-# INLINE next #-}
     next (J xs) = case next0 xs of
       Done        -> Yield w N
       Skip xs'    -> Skip    (J xs')
     where
       loop_head !s = case next s of
                       Yield x _ -> x
-                      Skip s' -> loop_head s'
-                      Done -> streamError "head" "Empty stream"
+                      Skip s'   -> loop_head s'
+                      Done      -> streamError "head" "Empty stream"
 {-# INLINE [0] head #-}
 
 -- | /O(1)/ Returns the first character and remainder of a 'Stream
 -- | /O(1)/ Returns all characters after the head of a Stream Char, which must
 -- be non-empty.
 tail :: Stream Char -> Stream Char
-tail (Stream next0 s0 len) = Stream next (False :*: s0) (len-1)
+tail (Stream next0 s0 len) = Stream next (C0 s0) (len-1)
     where
-      {-# INLINE next #-}
-      next (False :*: s) = case next0 s of
-                          Done       -> emptyError "tail"
-                          Skip s'    -> Skip (False :*: s')
-                          Yield _ s' -> Skip (True :*: s')
-      next (True :*: s) = case next0 s of
-                          Done       -> Done
-                          Skip s'    -> Skip    (True :*: s')
-                          Yield x s' -> Yield x (True :*: s')
+      next (C0 s) = case next0 s of
+                      Done       -> emptyError "tail"
+                      Skip s'    -> Skip (C0 s')
+                      Yield _ s' -> Skip (C1 s')
+      next (C1 s) = case next0 s of
+                      Done       -> Done
+                      Skip s'    -> Skip    (C1 s')
+                      Yield x s' -> Yield x (C1 s')
 {-# INLINE [0] tail #-}
 
 
 init :: Stream Char -> Stream Char
 init (Stream next0 s0 len) = Stream next (N :*: s0) (len-1)
     where
-      {-# INLINE next #-}
       next (N :*: s) = case next0 s of
                          Done       -> emptyError "init"
                          Skip s'    -> Skip (N :*: s')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.