Skip to content

Commit

Permalink
Update Eq IntSet and Eq (IntMap a)
Browse files Browse the repository at this point in the history
* Add benchmarks
* Mark the function INLINABLE for IntMap. On benchmarks for IntMap Int
  this reduces the run time by ~22%.
* Remove definitions for (/=). There is no good reason to implement this
  manually.
  • Loading branch information
meooow25 committed Aug 17, 2024
1 parent fb2aa39 commit 9d9f833
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 20 deletions.
1 change: 1 addition & 0 deletions containers-tests/benchmarks/IntMap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ main = do
, bench "spanAntitone" $ whnf (M.spanAntitone (<key_mid)) m
, bench "split" $ whnf (M.split key_mid) m
, bench "splitLookup" $ whnf (M.splitLookup key_mid) m
, bench "eq" $ whnf (\m' -> m' == m') m
]
where
elems = elems_hits
Expand Down
1 change: 1 addition & 0 deletions containers-tests/benchmarks/IntSet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ main = do
, bench "split:sparse" $ whnf (IS.split elem_sparse_mid) s_sparse
, bench "splitMember:dense" $ whnf (IS.splitMember elem_mid) s
, bench "splitMember:sparse" $ whnf (IS.splitMember elem_sparse_mid) s_sparse
, bench "eq" $ whnf (\s' -> s' == s') s
]
where
bound = 2^12
Expand Down
12 changes: 2 additions & 10 deletions containers/src/Data/IntMap/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3415,8 +3415,7 @@ data Distinct = Distinct | Nondistinct
Eq
--------------------------------------------------------------------}
instance Eq a => Eq (IntMap a) where
t1 == t2 = equal t1 t2
t1 /= t2 = nequal t1 t2
(==) = equal

equal :: Eq a => IntMap a -> IntMap a -> Bool
equal (Bin p1 l1 r1) (Bin p2 l2 r2)
Expand All @@ -3425,14 +3424,7 @@ equal (Tip kx x) (Tip ky y)
= (kx == ky) && (x==y)
equal Nil Nil = True
equal _ _ = False

nequal :: Eq a => IntMap a -> IntMap a -> Bool
nequal (Bin p1 l1 r1) (Bin p2 l2 r2)
= (p1 /= p2) || (nequal l1 l2) || (nequal r1 r2)
nequal (Tip kx x) (Tip ky y)
= (kx /= ky) || (x/=y)
nequal Nil Nil = False
nequal _ _ = True
{-# INLINABLE equal #-}

-- | @since 0.5.9
instance Eq1 IntMap where
Expand Down
11 changes: 1 addition & 10 deletions containers/src/Data/IntSet/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1388,8 +1388,7 @@ data Inserted = Inserted !IntSet ![Key]
Eq
--------------------------------------------------------------------}
instance Eq IntSet where
t1 == t2 = equal t1 t2
t1 /= t2 = nequal t1 t2
(==) = equal

equal :: IntSet -> IntSet -> Bool
equal (Bin p1 l1 r1) (Bin p2 l2 r2)
Expand All @@ -1399,14 +1398,6 @@ equal (Tip kx1 bm1) (Tip kx2 bm2)
equal Nil Nil = True
equal _ _ = False

nequal :: IntSet -> IntSet -> Bool
nequal (Bin p1 l1 r1) (Bin p2 l2 r2)
= (p1 /= p2) || (nequal l1 l2) || (nequal r1 r2)
nequal (Tip kx1 bm1) (Tip kx2 bm2)
= kx1 /= kx2 || bm1 /= bm2
nequal Nil Nil = False
nequal _ _ = True

{--------------------------------------------------------------------
Ord
--------------------------------------------------------------------}
Expand Down

0 comments on commit 9d9f833

Please sign in to comment.