Skip to content

Commit f795586

Browse files
authored
Speed up difference* and intersection* with unsafeInsert (#372)
…for speedups around 20% in the `HashMap.{difference,intersection}` benchmarks. Context: #225, #364
1 parent 2d537c1 commit f795586

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

Data/HashMap/Internal.hs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,7 +1748,7 @@ difference :: (Eq k, Hashable k) => HashMap k v -> HashMap k w -> HashMap k v
17481748
difference a b = foldlWithKey' go empty a
17491749
where
17501750
go m k v = case lookup k b of
1751-
Nothing -> insert k v m
1751+
Nothing -> unsafeInsert k v m
17521752
_ -> m
17531753
{-# INLINABLE difference #-}
17541754

@@ -1760,8 +1760,8 @@ differenceWith :: (Eq k, Hashable k) => (v -> w -> Maybe v) -> HashMap k v -> Ha
17601760
differenceWith f a b = foldlWithKey' go empty a
17611761
where
17621762
go m k v = case lookup k b of
1763-
Nothing -> insert k v m
1764-
Just w -> maybe m (\y -> insert k y m) (f v w)
1763+
Nothing -> unsafeInsert k v m
1764+
Just w -> maybe m (\y -> unsafeInsert k y m) (f v w)
17651765
{-# INLINABLE differenceWith #-}
17661766

17671767
-- | /O(n*log m)/ Intersection of two maps. Return elements of the first
@@ -1770,7 +1770,7 @@ intersection :: (Eq k, Hashable k) => HashMap k v -> HashMap k w -> HashMap k v
17701770
intersection a b = foldlWithKey' go empty a
17711771
where
17721772
go m k v = case lookup k b of
1773-
Just _ -> insert k v m
1773+
Just _ -> unsafeInsert k v m
17741774
_ -> m
17751775
{-# INLINABLE intersection #-}
17761776

@@ -1782,7 +1782,7 @@ intersectionWith :: (Eq k, Hashable k) => (v1 -> v2 -> v3) -> HashMap k v1
17821782
intersectionWith f a b = foldlWithKey' go empty a
17831783
where
17841784
go m k v = case lookup k b of
1785-
Just w -> insert k (f v w) m
1785+
Just w -> unsafeInsert k (f v w) m
17861786
_ -> m
17871787
{-# INLINABLE intersectionWith #-}
17881788

@@ -1794,7 +1794,7 @@ intersectionWithKey :: (Eq k, Hashable k) => (k -> v1 -> v2 -> v3)
17941794
intersectionWithKey f a b = foldlWithKey' go empty a
17951795
where
17961796
go m k v = case lookup k b of
1797-
Just w -> insert k (f k v w) m
1797+
Just w -> unsafeInsert k (f k v w) m
17981798
_ -> m
17991799
{-# INLINABLE intersectionWithKey #-}
18001800

Data/HashMap/Internal/Strict.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -595,8 +595,8 @@ differenceWith :: (Eq k, Hashable k) => (v -> w -> Maybe v) -> HashMap k v -> Ha
595595
differenceWith f a b = foldlWithKey' go empty a
596596
where
597597
go m k v = case HM.lookup k b of
598-
Nothing -> insert k v m
599-
Just w -> maybe m (\y -> insert k y m) (f v w)
598+
Nothing -> v `seq` unsafeInsert k v m
599+
Just w -> maybe m (\ !y -> unsafeInsert k y m) (f v w)
600600
{-# INLINABLE differenceWith #-}
601601

602602
-- | /O(n+m)/ Intersection of two maps. If a key occurs in both maps
@@ -607,7 +607,7 @@ intersectionWith :: (Eq k, Hashable k) => (v1 -> v2 -> v3) -> HashMap k v1
607607
intersectionWith f a b = foldlWithKey' go empty a
608608
where
609609
go m k v = case HM.lookup k b of
610-
Just w -> insert k (f v w) m
610+
Just w -> let !x = f v w in unsafeInsert k x m
611611
_ -> m
612612
{-# INLINABLE intersectionWith #-}
613613

@@ -619,7 +619,7 @@ intersectionWithKey :: (Eq k, Hashable k) => (k -> v1 -> v2 -> v3)
619619
intersectionWithKey f a b = foldlWithKey' go empty a
620620
where
621621
go m k v = case HM.lookup k b of
622-
Just w -> insert k (f k v w) m
622+
Just w -> let !x = f k v w in unsafeInsert k x m
623623
_ -> m
624624
{-# INLINABLE intersectionWithKey #-}
625625

0 commit comments

Comments
 (0)