Skip to content

Commit

Permalink
Rearrange eq checks
Browse files Browse the repository at this point in the history
  • Loading branch information
natefaubion committed Aug 11, 2023
1 parent c5642c5 commit a229c3d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
25 changes: 25 additions & 0 deletions bench/Bench/Data/Map.purs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ benchMap = do

log ""

log "eq"
log "------------"
benchEq

log ""

log "fromFoldable"
log "------------"
benchFromFoldable
Expand Down Expand Up @@ -171,3 +177,22 @@ benchMap = do

log $ "M.difference: big map (" <> show (M.size bigMap') <> ")"
benchWith 10 \_ -> M.difference bigMap' midMap'

benchEq = do
log $ "Mapf149d5.eq: small map (" <> show (Mapf149d5.size smallMap) <> ")"
bench \_ -> smallMap == smallMap

log $ "M.eq: small map (" <> show (M.size smallMap') <> ")"
bench \_ -> smallMap' == smallMap'

log $ "Mapf149d5.eq: midsize map (" <> show (Mapf149d5.size midMap) <> ")"
benchWith 100 \_ -> midMap == midMap

log $ "M.eq: midsize map (" <> show (M.size midMap') <> ")"
benchWith 100 \_ -> midMap' == midMap'

log $ "Mapf149d5.eq: big map (" <> show (Mapf149d5.size bigMap) <> ")"
benchWith 10 \_ -> bigMap == bigMap

log $ "M.eq: big map (" <> show (M.size bigMap') <> ")"
benchWith 10 \_ -> bigMap' == bigMap'
16 changes: 8 additions & 8 deletions src/Data/Map/Internal.purs
Original file line number Diff line number Diff line change
Expand Up @@ -835,26 +835,20 @@ instance (Eq k, Eq v) => Eq (MapIter k v) where
eq = go
where
go a b = case stepAsc a of
IterDone ->
true
IterNext k1 v1 a' ->
case stepAsc b of
IterNext k2 v2 b'
| k1 == k2 && v1 == v2 ->
go a' b'
_ ->
false
IterDone ->
true

instance (Ord k, Ord v) => Ord (MapIter k v) where
compare = go
where
go a b = case stepAsc a, stepAsc b of
IterDone, IterDone ->
EQ
IterDone, _ ->
LT
_, IterDone ->
GT
IterNext k1 v1 a', IterNext k2 v2 b' ->
case compare k1 k2 of
EQ ->
Expand All @@ -865,6 +859,12 @@ instance (Ord k, Ord v) => Ord (MapIter k v) where
other
other ->
other
IterDone, _ ->
LT
_, IterDone ->
GT
IterDone, IterDone ->
EQ

-- | Converts a Map to a MapIter for iteration using a MapStepper.
toMapIter :: forall k v. Map k v -> MapIter k v
Expand Down

0 comments on commit a229c3d

Please sign in to comment.