Commits

Bryan O'Sullivan committed 04b512e

Add some speedup-inducing checks to lazy replicate

  • Participants
  • Parent commits 1bbd917

Comments (0)

Files changed (1)

Data/Text/Lazy.hs

 
 import Prelude (Char, Bool(..), Maybe(..), String,
                 Eq(..), Ord(..), Ordering(..), Read(..), Show(..),
-                (&&), (+), (-), (.), ($), (++),
+                (&&), (||), (+), (-), (.), ($), (++),
                 div, error, flip, fromIntegral, not, otherwise)
 import qualified Prelude as P
 #if defined(HAVE_DEEPSEQ)
 -- | /O(n*m)/ 'replicate' @n@ @t@ is a 'Text' consisting of the input
 -- @t@ repeated @n@ times.
 replicate :: Int64 -> Text -> Text
-replicate n t = concat (rep 0)
-    where rep i | i >= n    = []
-                | otherwise = t : rep (i+1)
+replicate n t
+    | null t || n <= 0 = empty
+    | isSingleton t    = replicateChar n (head t)
+    | otherwise        = concat (rep 0)
+    where rep !i | i >= n    = []
+                 | otherwise = t : rep (i+1)
 {-# INLINE replicate #-}
 
 -- | /O(n)/ 'replicateChar' @n@ @c@ is a 'Text' of length @n@ with @c@ the