Bryan O'Sullivan avatar Bryan O'Sullivan committed a5d2bd4

Speed the Ord instance for lazy Text.

Comments (0)

Files changed (1)

Data/Text/Lazy.hs

     {-# INLINE (==) #-}
 
 instance Ord Text where
-    compare t1 t2 = compare (stream t1) (stream t2)
-    {-# INLINE compare #-}
+    compare = compareText
+
+compareText :: Text -> Text -> Ordering
+compareText Empty Empty = EQ
+compareText Empty _     = LT
+compareText _     Empty = GT
+compareText (Chunk a0 as) (Chunk b0 bs) = outer a0 b0
+ where
+  outer ta@(T.Text arrA offA lenA) tb@(T.Text arrB offB lenB) = go 0 0
+   where
+    go !i !j
+      | i >= lenA = compareText as (chunk (T.Text arrB (offB+j) (lenB-j)) bs)
+      | j >= lenB = compareText (chunk (T.Text arrA (offA+i) (lenA-i)) as) bs
+      | a < b     = LT
+      | a > b     = GT
+      | otherwise = go (i+di) (j+dj)
+      where T.Iter a di = T.iter ta i
+            T.Iter b dj = T.iter tb j
 
 instance Show Text where
     showsPrec p ps r = showsPrec p (unpack ps) r
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.