@@ -223,8 +223,24 @@ instance Eq Text where
223223 {-# INLINE (==) #-}
224224
225225instance Ord Text where
226− compare t1 t2 = compare (stream t1) (stream t2)
227− {-# INLINE compare #-}
226+ compare = compareText
227+
228+compareText :: Text -> Text -> Ordering
229+compareText Empty Empty = EQ
230+compareText Empty _ = LT
231+compareText _ Empty = GT
232+compareText (Chunk a0 as) (Chunk b0 bs) = outer a0 b0
233+ where
234+ outer ta@(T.Text arrA offA lenA) tb@(T.Text arrB offB lenB) = go 0 0
235+ where
236+ go !i !j
237+ | i >= lenA = compareText as (chunk (T.Text arrB (offB+j) (lenB-j)) bs)
238+ | j >= lenB = compareText (chunk (T.Text arrA (offA+i) (lenA-i)) as) bs
239+ | a < b = LT
240+ | a > b = GT
241+ | otherwise = go (i+di) (j+dj)
242+ where T.Iter a di = T.iter ta i
243+ T.Iter b dj = T.iter tb j
228244
229245instance Show Text where
230246 showsPrec p ps r = showsPrec p (unpack ps) r