on master help sign in
Add specialized Eq and Ord instances for names

Should require fewer comparisons with canonical names, starting with map vs filter before going on to List and elm/core.

commit84985dd4c32e674ba4603a58d418bafad65f051c authorEvan Czaplicki <evancz@users.noreply.github.com> dateTue 16 Apr 2019, 16:36 (8 years ago) parentddc90c7
3 files changed+49−24
compiler/src/AST/Optimized.hs+17−1 compiler/src/Elm/ModuleName.hs+8−8 compiler/src/Elm/Package.hs+24−15
compiler/src/AST/Optimized.hs+17−1
@@ -74,7 +74,6 @@ data Expr
7474
7575
7676data Global = Global ModuleName.Canonical Name
77− deriving (Eq, Ord)
7877
7978
8079
@@ -222,6 +221,23 @@ toKernelGlobal shortName =
222221
223222
224223
224+-- INSTANCES
225+
226+
227+instance Eq Global where
228+ (==) (Global home1 name1) (Global home2 name2) =
229+ name1 == name2 && home1 == home2
230+
231+
232+instance Ord Global where
233+ compare (Global home1 name1) (Global home2 name2) =
234+ case compare name1 name2 of
235+ LT -> LT
236+ EQ -> compare home1 home2
237+ GT -> GT
238+
239+
240+
225241-- BINARY
226242
227243
compiler/src/Elm/ModuleName.hs+8−8
@@ -9,7 +9,6 @@ module Elm.ModuleName
99 , encode
1010 , decoder
1111 , Canonical(..)
12− , canonicalIsKernel
1312 , basics, char, string
1413 , maybe, result, list, array, dict, tuple
1514 , platform, cmd, sub
@@ -104,13 +103,6 @@ data Canonical =
104103 { _package :: !Pkg.Name
105104 , _module :: !Name.Name
106105 }
107− deriving (Ord)
108−
109−
110−canonicalIsKernel :: Canonical -> Bool
111−canonicalIsKernel (Canonical _ name) =
112− Name.isKernel name
113−
114106
115107
116108-- INSTANCES
@@ -121,6 +113,14 @@ instance Eq Canonical where
121113 name1 == name2 && pkg1 == pkg2
122114
123115
116+instance Ord Canonical where
117+ compare (Canonical pkg1 name1) (Canonical pkg2 name2) =
118+ case compare name1 name2 of
119+ LT -> LT
120+ EQ -> compare pkg1 pkg2
121+ GT -> GT
122+
123+
124124instance Binary Canonical where
125125 put (Canonical a b) = put a >> put b
126126 get = liftM2 Canonical get get
compiler/src/Elm/Package.hs+24−15
@@ -44,7 +44,6 @@ data Name =
4444 { _author :: !Name.Name
4545 , _project :: !Name.Name
4646 }
47− deriving (Eq, Ord)
4847
4948
5049data Canonical =
@@ -52,7 +51,7 @@ data Canonical =
5251 { _name :: !Name
5352 , _version :: !V.Version
5453 }
55− deriving (Eq, Ord)
54+ deriving (Ord)
5655
5756
5857
@@ -202,25 +201,35 @@ suggestions =
202201
203202
204203
205−-- BINARY
204+-- INSTANCES
206205
207206
208−instance Binary Name where
209− get =
210− liftM2 Name get get
207+instance Eq Name where
208+ (==) (Name author1 project1) (Name author2 project2) =
209+ project1 == project2 && author1 == author2
211210
212− put (Name author project) =
213− do put author
214− put project
215211
212+instance Eq Canonical where
213+ (==) (Canonical package1 version1) (Canonical package2 version2) =
214+ version1 == version2 && package1 == package2
215+
216+
217+instance Ord Name where
218+ compare (Name author1 project1) (Name author2 project2) =
219+ case compare project1 project2 of
220+ LT -> LT
221+ EQ -> compare author1 author2
222+ GT -> GT
216223
217−instance Binary Canonical where
218− get =
219− liftM2 Canonical get get
220224
221− put (Canonical name version) =
222− do put name
223− put version
225+instance Binary Name where
226+ get = liftM2 Name get get
227+ put (Name a b) = put a >> put b
228+
229+
230+instance Binary Canonical where
231+ get = liftM2 Canonical get get
232+ put (Canonical a b) = put a >> put b
224233
225234
226235