From 4b4fbc58d37d37457144014ef82bdd928de175df Mon Sep 17 00:00:00 2001 From: Sylvain Henry Date: Tue, 11 Aug 2020 13:15:41 +0200 Subject: Remove "Ord FastString" instance FastStrings can be compared in 2 ways: by Unique or lexically. We don't want to bless one particular way with an "Ord" instance because it leads to bugs (#18562) or to suboptimal code (e.g. using lexical comparison while a Unique comparison would suffice). UTF-8 encoding has the advantage that sorting strings by their encoded bytes also sorts them by their Unicode code points, without having to decode the actual code points. BUT GHC uses Modified UTF-8 which diverges from UTF-8 by encoding \0 as 0xC080 instead of 0x00 (to avoid null bytes in the middle of a String so that the string can still be null-terminated). This patch adds a new `utf8CompareShortByteString` function that performs sorting by bytes but that also takes Modified UTF-8 into account. It is much more performant than decoding the strings into [Char] to perform comparisons (which we did in the previous patch). Bump haddock submodule --- compiler/GHC/Core/TyCo/Rep.hs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'compiler/GHC/Core/TyCo/Rep.hs') diff --git a/compiler/GHC/Core/TyCo/Rep.hs b/compiler/GHC/Core/TyCo/Rep.hs index 5931d8c94a..5215b5ce2e 100644 --- a/compiler/GHC/Core/TyCo/Rep.hs +++ b/compiler/GHC/Core/TyCo/Rep.hs @@ -252,7 +252,13 @@ instance Outputable Type where data TyLit = NumTyLit Integer | StrTyLit FastString - deriving (Eq, Ord, Data.Data) + deriving (Eq, Data.Data) + +instance Ord TyLit where + compare (NumTyLit _) (StrTyLit _) = LT + compare (StrTyLit _) (NumTyLit _) = GT + compare (NumTyLit x) (NumTyLit y) = compare x y + compare (StrTyLit x) (StrTyLit y) = uniqCompareFS x y instance Outputable TyLit where ppr = pprTyLit -- cgit v1.2.1