From 9d9f8332f1981d8e7f5436bd4138eaf3b44a0f5a Mon Sep 17 00:00:00 2001 From: meooow25 Date: Sat, 17 Aug 2024 18:46:51 +0530 Subject: [PATCH 1/2] Update Eq IntSet and Eq (IntMap a) * 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. --- containers-tests/benchmarks/IntMap.hs | 1 + containers-tests/benchmarks/IntSet.hs | 1 + containers/src/Data/IntMap/Internal.hs | 12 ++---------- containers/src/Data/IntSet/Internal.hs | 11 +---------- 4 files changed, 5 insertions(+), 20 deletions(-) diff --git a/containers-tests/benchmarks/IntMap.hs b/containers-tests/benchmarks/IntMap.hs index d29b3f77c..b48768e0d 100644 --- a/containers-tests/benchmarks/IntMap.hs +++ b/containers-tests/benchmarks/IntMap.hs @@ -53,6 +53,7 @@ main = do , bench "spanAntitone" $ whnf (M.spanAntitone ( m' == m') m ] where elems = elems_hits diff --git a/containers-tests/benchmarks/IntSet.hs b/containers-tests/benchmarks/IntSet.hs index 47f751c88..a9a6ea747 100644 --- a/containers-tests/benchmarks/IntSet.hs +++ b/containers-tests/benchmarks/IntSet.hs @@ -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 diff --git a/containers/src/Data/IntMap/Internal.hs b/containers/src/Data/IntMap/Internal.hs index ed9ce5e34..de1b070a8 100644 --- a/containers/src/Data/IntMap/Internal.hs +++ b/containers/src/Data/IntMap/Internal.hs @@ -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) @@ -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 diff --git a/containers/src/Data/IntSet/Internal.hs b/containers/src/Data/IntSet/Internal.hs index 6d739b8de..18510cfb8 100644 --- a/containers/src/Data/IntSet/Internal.hs +++ b/containers/src/Data/IntSet/Internal.hs @@ -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) @@ -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 --------------------------------------------------------------------} From e4f2b5194adf6cec65bd76a039974d777e5d6bfd Mon Sep 17 00:00:00 2001 From: meooow25 Date: Sat, 17 Aug 2024 19:29:49 +0530 Subject: [PATCH 2/2] Note that the benchmark is on the worst case --- containers-tests/benchmarks/IntMap.hs | 2 +- containers-tests/benchmarks/IntSet.hs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/containers-tests/benchmarks/IntMap.hs b/containers-tests/benchmarks/IntMap.hs index b48768e0d..9716d117a 100644 --- a/containers-tests/benchmarks/IntMap.hs +++ b/containers-tests/benchmarks/IntMap.hs @@ -53,7 +53,7 @@ main = do , bench "spanAntitone" $ whnf (M.spanAntitone ( m' == m') m + , bench "eq" $ whnf (\m' -> m' == m') m -- worst case, compares everything ] where elems = elems_hits diff --git a/containers-tests/benchmarks/IntSet.hs b/containers-tests/benchmarks/IntSet.hs index a9a6ea747..79041b76c 100644 --- a/containers-tests/benchmarks/IntSet.hs +++ b/containers-tests/benchmarks/IntSet.hs @@ -56,7 +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 + , bench "eq" $ whnf (\s' -> s' == s') s -- worst case, compares everything ] where bound = 2^12