Bryan O'Sullivan avatar Bryan O'Sullivan committed 6937a20

Speed up lazy text equality

Comments (0)

Files changed (2)

Data/Text/Lazy.hs

 import qualified Data.Text.Util as U
 import Data.Text.Lazy.Search (indices)
 
+equal :: Text -> Text -> Bool
+equal Empty Empty = True
+equal Empty _     = False
+equal _ Empty     = False
+equal (Chunk a as) (Chunk b bs) =
+    case compare lenA lenB of
+      LT -> a == (T.takeWord16 lenA b) &&
+            as `equal` Chunk (T.dropWord16 lenA b) bs
+      EQ -> a == b && as `equal` bs
+      GT -> T.takeWord16 lenB a == b &&
+            Chunk (T.dropWord16 lenB a) as `equal` bs
+  where lenA = T.lengthWord16 a
+        lenB = T.lengthWord16 b
+
 instance Eq Text where
-    t1 == t2 = stream t1 == stream t2
+    (==) = equal
     {-# INLINE (==) #-}
 
 instance Ord Text where

Data/Text/Unsafe.hs

     , unsafeHead
     , unsafeTail
     , lengthWord16
+    , takeWord16
+    , dropWord16
     ) where
      
 #if defined(ASSERTS)
 lengthWord16 :: Text -> Int
 lengthWord16 (Text _arr _off len) = len
 {-# INLINE lengthWord16 #-}
+
+-- | /O(1)/ Unchecked take of 'k' 'Word16's from the front of a 'Text'.
+takeWord16 :: Int -> Text -> Text
+takeWord16 k (Text arr off _len) = Text arr off k
+{-# INLINE takeWord16 #-}
+
+-- | /O(1)/ Unchecked drop of 'k' 'Word16's from the front of a 'Text'.
+dropWord16 :: Int -> Text -> Text
+dropWord16 k (Text arr off len) = Text arr (off+k) (len-k)
+{-# INLINE dropWord16 #-}
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.