Script 'mail_helper' called by obssrc
Hello community,
here is the log from the commit of package ghc-unordered-containers for
openSUSE:Factory checked in at 2026-12-02 17:02:58
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Comparing /work/SRC/openSUSE:Factory/ghc-unordered-containers (Old)
and /work/SRC/openSUSE:Factory/.ghc-unordered-containers.new.1340718 (New)
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Package is "ghc-unordered-containers"
Wed Dec 2 17:02:58 2026 rev:39 rq:1402042 version:0.2.21.1
Changes:
--------
---
/work/SRC/openSUSE:Factory/ghc-unordered-containers/ghc-unordered-containers.changes
2026-11-24 12:43:40.991307669 +0100
+++
/work/SRC/openSUSE:Factory/.ghc-unordered-containers.new.1340718/ghc-unordered-containers.changes
2026-12-02 17:04:51.259205290 +0100
@@ -1,0 +2,44 @@
+Mon Nov 30 10:13:29 UTC 2026 - Peter Simons <[email protected]>
+
+- Update unordered-containers to version 0.2.21.1.
+ ## [0.2.21.1] - November 2026
+
+ * Bug fixes:
+ * [Fix `fromList` and `fromListWith[Key]` leaking elements between
calls](https://github.com/haskell-unordered-containers/unordered-containers/pull/602).
+ A function like `f xs = HashSet.fromList ([a, b] ++ xs)` could return a
set that also
+ contained elements passed to other calls of `f`. With
`fromListWith[Key]`, values passed
+ to other calls could also end up combined into the result.
+
+ Affected functions:
+ * `Data.HashSet.fromList`
+ * `Data.HashMap.Lazy.fromList`, `fromListWith` and `fromListWithKey`
+ * `Data.HashMap.Strict.fromList`, `fromListWith` and `fromListWithKey`
+
+ These functions inserted the list elements into the map under
construction in-place, in
+ a pure fold. GHC could therefore build the map for the statically known
prefix `[a, b]`
+ once and share it between all calls of `f`. Each call then inserted its
`xs` into that
+ shared map. Depending on the hashes of the elements, some of these
insertions modified
+ the shared map, so the inserted elements also showed up in the results
of other calls.
+
+ Now each of these functions builds its map entirely in `ST`, so the
in-place inserts of
+ one call can only affect that call's own map.
+ See
[#601](https://github.com/haskell-unordered-containers/unordered-containers/issues/601)
for details.
+ * [`Data.HashMap.Strict.alterF`: Don't store thunks via the
"alterFinsertWith"
rule](https://github.com/haskell-unordered-containers/unordered-containers/pull/600)
+ * [`Data.HashMap.Strict.insertWith`: Don't store thunks in hash collision
nodes](https://github.com/haskell-unordered-containers/unordered-containers/pull/600)
+
+ * Performance improvements:
+ * [`fromListWith[Key]`: Specialize to the combining
function](https://github.com/haskell-unordered-containers/unordered-containers/pull/602)
+ * [`Data.HashMap.Strict.insertWith`: Return the original map if the
combined value is pointer-equal to the old
value](https://github.com/haskell-unordered-containers/unordered-containers/pull/600)
+ * [`insertWith`: Save a closure allocation per
call](https://github.com/haskell-unordered-containers/unordered-containers/pull/600)
+
+ * Documentation changes:
+ * [Document that `HashMap`s have a canonical
form](https://github.com/haskell-unordered-containers/unordered-containers/pull/593)
+ * [Document that `Foldable.elem` takes O(n) time for
`HashSet`](https://github.com/haskell-unordered-containers/unordered-containers/pull/592)
+
+ * Other changes:
+ * [Use `Array` and `MArray`
unqualified](https://github.com/haskell-unordered-containers/unordered-containers/pull/576)
+ * [Remove redundant length check in
`sameArray1`](https://github.com/haskell-unordered-containers/unordered-containers/pull/591)
+
+ [0.2.21.1]:
https://github.com/haskell-unordered-containers/unordered-containers/compare/v0.2.21...v0.2.21.1
+
+-------------------------------------------------------------------
Old:
----
unordered-containers-0.2.21.tar.gz
unordered-containers.cabal
New:
----
unordered-containers-0.2.21.1.tar.gz
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Other differences:
------------------
++++++ ghc-unordered-containers.spec ++++++
--- /var/tmp/diff_new_pack.SSlEkw/_old 2026-12-02 17:04:52.474256835 +0100
+++ /var/tmp/diff_new_pack.SSlEkw/_new 2026-12-02 17:04:52.476256919 +0100
@@ -20,14 +20,13 @@
%bcond_with ghc_prof
%bcond_with tests
Name: ghc-%pkg_name
-Version: 0.2.21
+Version: 0.2.21.1
Release: 0
%global pkgver %pkg_name-%{version}
Summary: Efficient hashing-based container types
License: BSD-3-Clause
URL: https://hackage.haskell.org/package/%pkg_name
Source0:
https://hackage.haskell.org/package/%pkg_name-%{version}/%pkg_name-%{version}.tar.gz
-Source1:
https://hackage.haskell.org/package/%pkg_name-%{version}/revision/2.cabal#/%{pkg_name}.cabal
BuildRequires: ghc-Cabal-devel
BuildRequires: ghc-base-devel
BuildRequires: ghc-deepseq-devel
@@ -110,9 +109,9 @@
%prep
%autosetup -p1 -n %pkg_name-%{version}
-cp -p %{SOURCE1} %pkg_name.cabal
%build
+%define cabal_configure_options -f-debug
%ghc_lib_build
%install
++++++ unordered-containers-0.2.21.tar.gz ->
unordered-containers-0.2.21.1.tar.gz ++++++
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn'
'--exclude=.svnignore' old/unordered-containers-0.2.21/CHANGES.md
new/unordered-containers-0.2.21.1/CHANGES.md
--- old/unordered-containers-0.2.21/CHANGES.md 2001-09-09 03:46:40.000000000
+0200
+++ new/unordered-containers-0.2.21.1/CHANGES.md 2001-09-09
03:46:40.000000000 +0200
@@ -1,3 +1,43 @@
+## [0.2.21.1] - November 2026
+
+* Bug fixes:
+ * [Fix `fromList` and `fromListWith[Key]` leaking elements between
calls](https://github.com/haskell-unordered-containers/unordered-containers/pull/602).
+ A function like `f xs = HashSet.fromList ([a, b] ++ xs)` could return a
set that also
+ contained elements passed to other calls of `f`. With `fromListWith[Key]`,
values passed
+ to other calls could also end up combined into the result.
+
+ Affected functions:
+ * `Data.HashSet.fromList`
+ * `Data.HashMap.Lazy.fromList`, `fromListWith` and `fromListWithKey`
+ * `Data.HashMap.Strict.fromList`, `fromListWith` and `fromListWithKey`
+
+ These functions inserted the list elements into the map under construction
in-place, in
+ a pure fold. GHC could therefore build the map for the statically known
prefix `[a, b]`
+ once and share it between all calls of `f`. Each call then inserted its
`xs` into that
+ shared map. Depending on the hashes of the elements, some of these
insertions modified
+ the shared map, so the inserted elements also showed up in the results of
other calls.
+
+ Now each of these functions builds its map entirely in `ST`, so the
in-place inserts of
+ one call can only affect that call's own map.
+ See
[#601](https://github.com/haskell-unordered-containers/unordered-containers/issues/601)
for details.
+ * [`Data.HashMap.Strict.alterF`: Don't store thunks via the
"alterFinsertWith"
rule](https://github.com/haskell-unordered-containers/unordered-containers/pull/600)
+ * [`Data.HashMap.Strict.insertWith`: Don't store thunks in hash collision
nodes](https://github.com/haskell-unordered-containers/unordered-containers/pull/600)
+
+* Performance improvements:
+ * [`fromListWith[Key]`: Specialize to the combining
function](https://github.com/haskell-unordered-containers/unordered-containers/pull/602)
+ * [`Data.HashMap.Strict.insertWith`: Return the original map if the combined
value is pointer-equal to the old
value](https://github.com/haskell-unordered-containers/unordered-containers/pull/600)
+ * [`insertWith`: Save a closure allocation per
call](https://github.com/haskell-unordered-containers/unordered-containers/pull/600)
+
+* Documentation changes:
+ * [Document that `HashMap`s have a canonical
form](https://github.com/haskell-unordered-containers/unordered-containers/pull/593)
+ * [Document that `Foldable.elem` takes O(n) time for
`HashSet`](https://github.com/haskell-unordered-containers/unordered-containers/pull/592)
+
+* Other changes:
+ * [Use `Array` and `MArray`
unqualified](https://github.com/haskell-unordered-containers/unordered-containers/pull/576)
+ * [Remove redundant length check in
`sameArray1`](https://github.com/haskell-unordered-containers/unordered-containers/pull/591)
+
+[0.2.21.1]:
https://github.com/haskell-unordered-containers/unordered-containers/compare/v0.2.21...v0.2.21.1
+
## [0.2.21] - December 2025
* API enhancements:
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn'
'--exclude=.svnignore'
old/unordered-containers-0.2.21/Data/HashMap/Internal/Array.hs
new/unordered-containers-0.2.21.1/Data/HashMap/Internal/Array.hs
--- old/unordered-containers-0.2.21/Data/HashMap/Internal/Array.hs
2001-09-09 03:46:40.000000000 +0200
+++ new/unordered-containers-0.2.21.1/Data/HashMap/Internal/Array.hs
2001-09-09 03:46:40.000000000 +0200
@@ -141,10 +141,9 @@
unsafeSameArray (Array xs) (Array ys) =
tagToEnum# (unsafeCoerce# reallyUnsafePtrEquality# xs ys)
+-- | Precondition: the two arrays have the same length. This is not checked.
sameArray1 :: (a -> b -> Bool) -> Array a -> Array b -> Bool
-sameArray1 eq !xs0 !ys0
- | lenxs /= lenys = False
- | otherwise = go 0 xs0 ys0
+sameArray1 eq !xs0 !ys0 = go 0 xs0 ys0
where
go !k !xs !ys
| k == lenxs = True
@@ -153,7 +152,6 @@
= eq x y && go (k + 1) xs ys
!lenxs = length xs0
- !lenys = length ys0
length :: Array a -> Int
length ary = I# (sizeofSmallArray# (unArray ary))
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn'
'--exclude=.svnignore'
old/unordered-containers-0.2.21/Data/HashMap/Internal/ST.hs
new/unordered-containers-0.2.21.1/Data/HashMap/Internal/ST.hs
--- old/unordered-containers-0.2.21/Data/HashMap/Internal/ST.hs 1970-01-01
01:00:00.000000000 +0100
+++ new/unordered-containers-0.2.21.1/Data/HashMap/Internal/ST.hs
2001-09-09 03:46:40.000000000 +0200
@@ -0,0 +1,46 @@
+{-# LANGUAGE BangPatterns #-}
+{-# LANGUAGE CPP #-}
+{-# OPTIONS_HADDOCK not-home #-}
+
+-- | = WARNING
+--
+-- This module is considered __internal__.
+--
+-- The Package Versioning Policy __does not apply__.
+--
+-- The contents of this module may change __in any way whatsoever__
+-- and __without any warning__ between minor versions of this package.
+--
+-- Authors importing this module are expected to track development
+-- closely.
+--
+-- = Description
+--
+-- Utilities for working in 'ST'.
+module Data.HashMap.Internal.ST
+ ( foldlST'
+ ) where
+
+import Control.Monad.ST
+-- for MicroHS
+import Data.Foldable (Foldable (foldr))
+#ifdef __GLASGOW_HASKELL__
+import GHC.Exts (oneShot)
+#endif
+import Prelude hiding (foldr)
+
+-- | A strict version of 'Data.Foldable.foldlM', specialized for 'ST'. The
problem
+-- with just using @foldlM@ is that GHC doesn't always get the arity right,
which
+-- can lead to excessive allocation. See
+--
https://github.com/haskell-unordered-containers/unordered-containers/pull/602#issuecomment-6775859682
+-- and https://gitlab.haskell.org/ghc/ghc/-/issues/16357.
+-- We don't actually need the strictness for @fromList@ and such, but we
include
+-- it to reduce the number of things we have to check.
+foldlST' :: Foldable f => (b -> a -> ST s b) -> b -> f a -> ST s b
+foldlST' f z0 xs = foldr (\x k -> oneShot (\ !z -> f z x >>= k)) pure xs z0
+{-# INLINE foldlST' #-}
+
+#ifndef __GLASGOW_HASKELL__
+oneShot :: a -> a
+oneShot f = f
+#endif
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn'
'--exclude=.svnignore'
old/unordered-containers-0.2.21/Data/HashMap/Internal/Strict.hs
new/unordered-containers-0.2.21.1/Data/HashMap/Internal/Strict.hs
--- old/unordered-containers-0.2.21/Data/HashMap/Internal/Strict.hs
2001-09-09 03:46:40.000000000 +0200
+++ new/unordered-containers-0.2.21.1/Data/HashMap/Internal/Strict.hs
2001-09-09 03:46:40.000000000 +0200
@@ -131,17 +131,18 @@
import Data.Bits ((.&.), (.|.))
import Data.Coerce (coerce)
import Data.Functor.Identity (Identity (..))
--- See Note [Imports from Data.HashMap.Internal]
import Data.Hashable (Hashable)
-import Data.HashMap.Internal (Hash, HashMap (..), Leaf (..), LookupRes (..),
- Shift, fullBitmap, hash, index, mask, nextShift,
- ptrEq, sparseIndex)
-import Prelude hiding (lookup, map)
+-- See Note [Imports from Data.HashMap.Internal]
+import Data.HashMap.Internal (Hash, HashMap (..), Leaf (..),
+ LookupRes (..), Shift, fullBitmap, hash,
+ index, mask, nextShift, ptrEq, sparseIndex)
+import Data.HashMap.Internal.Array (Array)
+import Data.HashMap.Internal.ST (foldlST')
+import Prelude hiding (lookup, map)
-- See Note [Imports from Data.HashMap.Internal]
import qualified Data.HashMap.Internal as HM
import qualified Data.HashMap.Internal.Array as A
-import qualified Data.List as List
import qualified GHC.Exts as Exts
{-
@@ -192,51 +193,63 @@
-- > where f new old = new + old
insertWith :: Hashable k => (v -> v -> v) -> k -> v -> HashMap k v
-> HashMap k v
-insertWith f k0 v0 m0 = go h0 k0 v0 0 m0
+insertWith f0 k0 v0 m0 = go f0 h0 k0 v0 0 m0
where
h0 = hash k0
- go !h !k x !_ Empty = leaf h k x
- go h k x s t@(Leaf hy l@(L ky y))
+ go _ !h !k x !_ Empty = leaf h k x
+ go f h k x s t@(Leaf hy l@(L ky y))
| hy == h = if ky == k
- then leaf h k (f x y)
+ then case f x y of
+ !v' | ptrEq y v' -> t
+ | otherwise -> Leaf h (L k v')
else x `seq` HM.collision h l (L k x)
| otherwise = x `seq` runST (HM.two s h k x hy t)
- go h k x s (BitmapIndexed b ary)
+ go f h k x s t@(BitmapIndexed b ary)
| b .&. m == 0 =
let ary' = A.insert ary i $! leaf h k x
in HM.bitmapIndexedOrFull (b .|. m) ary'
| otherwise =
case A.index# ary i of
- (# st #) ->
- let !st' = go h k x (nextShift s) st
+ (# !st #) ->
+ let !st' = go f h k x (nextShift s) st
ary' = A.update ary i st'
- in BitmapIndexed b ary'
+ in if ptrEq st st'
+ then t
+ else BitmapIndexed b ary'
where m = mask h s
i = sparseIndex b m
- go h k x s (Full ary) =
+ go f h k x s t@(Full ary) =
case A.index# ary i of
- (# st #) ->
- let !st' = go h k x (nextShift s) st
+ (# !st #) ->
+ let !st' = go f h k x (nextShift s) st
ary' = HM.updateFullArray ary i st'
- in Full ary'
+ in if ptrEq st st'
+ then t
+ else Full ary'
where i = index h s
- go h k x s t@(Collision hy v)
- | h == hy = Collision h (updateOrSnocWith f k x v)
- | otherwise = go h k x s $ BitmapIndexed (mask hy s) (A.singleton t)
+ go f h k x s t@(Collision hy v)
+ | h == hy =
+ let !v' = insertWithArr f k x v
+ in if A.unsafeSameArray v v'
+ then t
+ else Collision h v'
+ | otherwise = go f h k x s $ BitmapIndexed (mask hy s) (A.singleton t)
{-# INLINABLE insertWith #-}
--- | In-place update version of insertWith
+-- | In-place update version of 'insertWith'. See 'HM.unsafeInsert' for when
+-- this is safe.
unsafeInsertWith :: Hashable k => (v -> v -> v) -> k -> v -> HashMap k v
- -> HashMap k v
+ -> ST s (HashMap k v)
unsafeInsertWith f k0 v0 m0 = unsafeInsertWithKey (const f) k0 v0 m0
-{-# INLINABLE unsafeInsertWith #-}
+{-# INLINE unsafeInsertWith #-}
-unsafeInsertWithKey :: forall k v. Hashable k => (k -> v -> v -> v) -> k -> v
-> HashMap k v
- -> HashMap k v
-unsafeInsertWithKey f k0 v0 m0 = runST (go h0 k0 v0 0 m0)
+-- | Like 'unsafeInsertWith', but the combining function also takes the key.
+unsafeInsertWithKey :: forall k v s. Hashable k => (k -> v -> v -> v) -> k ->
v -> HashMap k v
+ -> ST s (HashMap k v)
+unsafeInsertWithKey f k0 v0 m0 = go h0 k0 v0 0 m0
where
h0 = hash k0
- go :: forall s. Hash -> k -> v -> Shift -> HashMap k v -> ST s (HashMap k
v)
+ go :: Hash -> k -> v -> Shift -> HashMap k v -> ST s (HashMap k v)
go !h !k x !_ Empty = return $! leaf h k x
go h k x s t@(Leaf hy l@(L ky y))
| hy == h = if ky == k
@@ -264,8 +277,9 @@
where i = index h s
go h k x s t@(Collision hy v)
| h == hy = return $! Collision h (updateOrSnocWithKey f k x v)
- | otherwise = go h k x s $ BitmapIndexed (mask hy s) (A.singleton t)
-{-# INLINABLE unsafeInsertWithKey #-}
+ | otherwise = x `seq` HM.two s h k x hy t
+-- INLINE, so that 'go' is specialised to the combining function.
+{-# INLINE unsafeInsertWithKey #-}
-- | \(O(\log n)\) Adjust the value tied to a given key in this map only
-- if it is present. Otherwise, leave the map alone.
@@ -362,11 +376,8 @@
test_bottom :: a
test_bottom = error "Data.HashMap.alterF internal error: hit test_bottom"
-bogus# :: (# #) -> (# a #)
-bogus# _ = error "Data.HashMap.alterF internal error: hit bogus#"
-
-impossibleAdjust :: a
-impossibleAdjust = error "Data.HashMap.alterF internal error: impossible
adjust"
+impossibleAlterF :: a
+impossibleAlterF = error "Data.HashMap.alterF internal error: impossible
result"
{-# RULES
@@ -381,9 +392,12 @@
"alterFinsertWith" [1] forall (f :: Maybe a -> Identity (Maybe a)) x y.
alterFWeird (coerce (Just x)) (coerce (Just y)) f =
- coerce (HM.insertModifying x (\mold -> case runIdentity (f (Just mold)) of
- Nothing -> bogus# (# #)
- Just !new -> (# new #)))
+ \ !k !m -> Identity $
+ insertWith
+ (\_ old -> case runIdentity (f (Just old)) of
+ Nothing -> impossibleAlterF
+ Just new -> new)
+ k x m
-- This rule is written a bit differently than the one for lazy
-- maps because the adjust here is strict. We could write it the
@@ -392,7 +406,7 @@
alterFWeird (coerce Nothing) (coerce (Just x)) f =
coerce (adjust (\a -> case runIdentity (f (Just a)) of
Just a' -> a'
- Nothing -> impossibleAdjust))
+ Nothing -> impossibleAlterF))
"alterFlookup" forall _ign1 _ign2 (f :: Maybe a -> Const r (Maybe a)) .
alterFWeird _ign1 _ign2 f = \ !k !m -> Const (getConst (f (HM.lookup k m)))
@@ -664,8 +678,9 @@
-- list contains duplicate mappings, the later mappings take
-- precedence.
fromList :: Hashable k => [(k, v)] -> HashMap k v
-fromList = List.foldl' (\ m (k, !v) -> HM.unsafeInsert k v m) HM.empty
-{-# INLINABLE fromList #-}
+-- See Note [fromList arity] in Data.HashMap.Internal
+fromList = \ xs -> runST $ foldlST' (\ m (k, !v) -> HM.unsafeInsert k v m)
HM.empty xs
+{-# INLINE fromList #-}
-- | \(O(n \log n)\) Construct a map from a list of elements. Uses
-- the provided function @f@ to merge duplicate entries with
@@ -698,7 +713,8 @@
-- > fromListWith f [(k, a), (k, b), (k, c), (k, d)]
-- > = fromList [(k, f d (f c (f b a)))]
fromListWith :: Hashable k => (v -> v -> v) -> [(k, v)] -> HashMap k v
-fromListWith f = List.foldl' (\ m (k, v) -> unsafeInsertWith f k v m) HM.empty
+-- See Note [fromList arity] in Data.HashMap.Internal
+fromListWith f = \ xs -> runST $ foldlST' (\ m (k, v) -> unsafeInsertWith f k
v m) HM.empty xs
{-# INLINE fromListWith #-}
-- | \(O(n \log n)\) Construct a map from a list of elements. Uses
@@ -728,13 +744,14 @@
--
-- @since 0.2.11
fromListWithKey :: Hashable k => (k -> v -> v -> v) -> [(k, v)] -> HashMap k v
-fromListWithKey f = List.foldl' (\ m (k, v) -> unsafeInsertWithKey f k v m)
HM.empty
+-- See Note [fromList arity] in Data.HashMap.Internal
+fromListWithKey f = \ xs -> runST $ foldlST' (\ m (k, v) ->
unsafeInsertWithKey f k v m) HM.empty xs
{-# INLINE fromListWithKey #-}
------------------------------------------------------------------------
-- Array operations
-updateWith :: Eq k => (v -> v) -> k -> A.Array (Leaf k v) -> A.Array (Leaf k v)
+updateWith :: Eq k => (v -> v) -> k -> Array (Leaf k v) -> Array (Leaf k v)
updateWith f k0 ary0 = go k0 ary0 0 (A.length ary0)
where
go !k !ary !i !n
@@ -749,18 +766,34 @@
-- the given function to the new and old value (in that order). The
-- value is always evaluated to WHNF before being inserted into the
-- array.
-updateOrSnocWith :: Eq k => (v -> v -> v) -> k -> v -> A.Array (Leaf k v)
- -> A.Array (Leaf k v)
-updateOrSnocWith f = updateOrSnocWithKey (const f)
-{-# INLINABLE updateOrSnocWith #-}
+--
+-- Returns the original array if the combined value is pointer-equal to
+-- the old one.
+insertWithArr :: Eq k => (v -> v -> v) -> k -> v -> Array (Leaf k v)
+ -> Array (Leaf k v)
+insertWithArr f !k v !ary = go 0 (A.length ary)
+ where
+ -- k is free in go rather than an argument: otherwise GHC stores the
+ -- updated leaf as a thunk. See
+ -- https://gitlab.haskell.org/ghc/ghc/-/issues/27507.
+ go !i !n
+ -- Not found, append to the end.
+ | i >= n = A.snoc ary $! L k $! v
+ | otherwise = case A.index# ary i of
+ (# L kx y #)
+ | k == kx -> case f v y of
+ !v' | ptrEq y v' -> ary
+ | otherwise -> A.update ary i (L k v')
+ | otherwise -> go (i+1) n
+{-# INLINABLE insertWithArr #-}
-- | Append the given key and value to the array. If the key is
-- already present, instead update the value of the key by applying
-- the given function to the new and old value (in that order). The
-- value is always evaluated to WHNF before being inserted into the
-- array.
-updateOrSnocWithKey :: Eq k => (k -> v -> v -> v) -> k -> v -> A.Array (Leaf k
v)
- -> A.Array (Leaf k v)
+updateOrSnocWithKey :: Eq k => (k -> v -> v -> v) -> k -> v -> Array (Leaf k v)
+ -> Array (Leaf k v)
updateOrSnocWithKey f k0 v0 ary0 = go k0 v0 ary0 0 (A.length ary0)
where
go !k v !ary !i !n
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn'
'--exclude=.svnignore' old/unordered-containers-0.2.21/Data/HashMap/Internal.hs
new/unordered-containers-0.2.21.1/Data/HashMap/Internal.hs
--- old/unordered-containers-0.2.21/Data/HashMap/Internal.hs 2001-09-09
03:46:40.000000000 +0200
+++ new/unordered-containers-0.2.21.1/Data/HashMap/Internal.hs 2001-09-09
03:46:40.000000000 +0200
@@ -151,31 +151,34 @@
, adjust#
) where
-import Data.Traversable -- MicroHs needs this since its Prelude does
not have Foldable&Traversable.
- -- It's harmless for GHC, and putting it
first avoid a warning.
-
-import Control.Applicative (Const (..))
-import Control.DeepSeq (NFData (..), NFData1 (..), NFData2 (..))
-import Control.Monad.ST (ST, runST)
-import Data.Bifoldable (Bifoldable (..))
-import Data.Bits (complement, countTrailingZeros, popCount,
- shiftL, unsafeShiftL, unsafeShiftR, (.&.),
- (.|.))
-import Data.Coerce (coerce)
-import Data.Data (Constr, Data (..), DataType)
-import Data.Functor.Classes (Eq1 (..), Eq2 (..), Ord1 (..), Ord2 (..),
- Read1 (..), Show1 (..), Show2 (..))
-import Data.Functor.Identity (Identity (..))
-import Data.Hashable (Hashable)
-import Data.Hashable.Lifted (Hashable1, Hashable2)
-import Data.HashMap.Internal.List (isPermutationBy, unorderedCompare)
-import Data.Maybe (isNothing)
-import Data.Semigroup (Semigroup (..), stimesIdempotentMonoid)
-import GHC.Exts (Int (..), Int#, TYPE, (==#))
-import GHC.Stack (HasCallStack)
-import Prelude hiding (Foldable (..), filter, lookup, map,
- pred)
-import Text.Read hiding (step)
+-- MicroHs needs this import since its Prelude does not have
Foldable&Traversable.
+-- It's harmless for GHC, and putting it first avoids a warning.
+import Data.Traversable
+
+import Control.Applicative (Const (..))
+import Control.DeepSeq (NFData (..), NFData1 (..), NFData2 (..))
+import Control.Monad.ST (ST, runST)
+import Data.Bifoldable (Bifoldable (..))
+import Data.Bits (complement, countTrailingZeros, popCount,
+ shiftL, unsafeShiftL, unsafeShiftR, (.&.),
+ (.|.))
+import Data.Coerce (coerce)
+import Data.Data (Constr, Data (..), DataType)
+import Data.Functor.Classes (Eq1 (..), Eq2 (..), Ord1 (..), Ord2 (..),
+ Read1 (..), Show1 (..), Show2 (..))
+import Data.Functor.Identity (Identity (..))
+import Data.Hashable (Hashable)
+import Data.Hashable.Lifted (Hashable1, Hashable2)
+import Data.HashMap.Internal.Array (Array, MArray)
+import Data.HashMap.Internal.List (isPermutationBy, unorderedCompare)
+import Data.HashMap.Internal.ST (foldlST')
+import Data.Maybe (isNothing)
+import Data.Semigroup (Semigroup (..), stimesIdempotentMonoid)
+import GHC.Exts (Int (..), Int#, TYPE, (==#))
+import GHC.Stack (HasCallStack)
+import Prelude hiding (Foldable (..), filter, lookup, map,
+ pred)
+import Text.Read hiding (step)
import qualified Data.Data as Data
import qualified Data.Foldable as Foldable
@@ -218,7 +221,7 @@
-- ^ Invariants:
--
-- * 'Empty' is not a valid sub-node. It can only appear at the root.
(INV1)
- | BitmapIndexed !Bitmap !(A.Array (HashMap k v))
+ | BitmapIndexed !Bitmap !(Array (HashMap k v))
-- ^ Invariants:
--
-- * Only the lower @maxChildren@ bits of the 'Bitmap' may be set. The
@@ -236,11 +239,11 @@
-- compatible with its 'Hash'. (INV6)
-- (TODO: Document this properly (#425))
-- * The 'Hash' of a 'Leaf' node must be the 'hash' of its key. (INV7)
- | Full !(A.Array (HashMap k v))
+ | Full !(Array (HashMap k v))
-- ^ Invariants:
--
-- * The array of a 'Full' node stores exactly 'maxChildren' sub-nodes.
(INV8)
- | Collision !Hash !(A.Array (Leaf k v))
+ | Collision !Hash !(Array (Leaf k v))
-- ^ Invariants:
--
-- * The location of a 'Leaf' or 'Collision' node in the tree must be
@@ -252,6 +255,38 @@
-- * No two keys stored in a 'Collision' can be equal according to their
-- 'Eq' instance. (INV10)
+{-
+Note [Canonical form]
+~~~~~~~~~~~~~~~~~~~~~
+
+The invariants above imply that HashMaps have a canonical form: two
+HashMaps that contain the same key-value pairs have the same tree
+structure, modulo the order of keys within a Collision node -- regardless
+of the order in which they were constructed. This is because each key's
+hash fully determines the path to its leaf, while the remaining
+invariants, in particular INV1, INV3, INV5, INV8 and INV9, rule out
+alternative encodings of the same sub-tree (e.g. a redundant
+BitmapIndexed node wrapping a single Leaf, a BitmapIndexed node that
+could be a Full node, or a single-entry Collision node that could be a
+Leaf).
+
+Several functions rely on this, in two ways:
+
+* equal1 and equalKeys compare two trees structurally, node by node.
+ Canonical form makes this complete: maps with equal contents have
+ identical structure, except within Collision nodes, which these
+ functions compare as unordered collections.
+
+* equal2, cmp, equalKeys1 and the Hashable instances flatten a tree with
+ leavesAndCollisions. Canonical form guarantees that maps with equal
+ contents produce the same sequence of leaves, again modulo the order
+ within Collision nodes, which these functions handle
+ order-insensitively.
+
+Without a canonical form these functions could give inconsistent results
+for maps with equal contents.
+-}
+
type role HashMap nominal representational
-- | @since 0.2.17.0
@@ -546,11 +581,11 @@
-- hashLeafWithSalt :: Int -> Leaf k v -> Int
hashLeafWithSalt s (L k v) = (s `hk` k) `hv` v
- -- hashCollisionWithSalt :: Int -> A.Array (Leaf k v) -> Int
+ -- hashCollisionWithSalt :: Int -> Array (Leaf k v) -> Int
hashCollisionWithSalt s
= List.foldl' H.hashWithSalt s . arrayHashesSorted s
- -- arrayHashesSorted :: Int -> A.Array (Leaf k v) -> [Int]
+ -- arrayHashesSorted :: Int -> Array (Leaf k v) -> [Int]
arrayHashesSorted s = List.sort . List.map (hashLeafWithSalt s) .
A.toList
instance (Hashable k) => Hashable1 (HashMap k) where
@@ -573,11 +608,11 @@
hashLeafWithSalt :: Int -> Leaf k v -> Int
hashLeafWithSalt s (L k v) = s `H.hashWithSalt` k `H.hashWithSalt` v
- hashCollisionWithSalt :: Int -> A.Array (Leaf k v) -> Int
+ hashCollisionWithSalt :: Int -> Array (Leaf k v) -> Int
hashCollisionWithSalt s
= List.foldl' H.hashWithSalt s . arrayHashesSorted s
- arrayHashesSorted :: Int -> A.Array (Leaf k v) -> [Int]
+ arrayHashesSorted :: Int -> Array (Leaf k v) -> [Int]
arrayHashesSorted s = List.sort . List.map (hashLeafWithSalt s) .
A.toList
-- | Helper to get 'Leaf's and 'Collision's as a list.
@@ -832,7 +867,7 @@
{-# INLINE collision #-}
-- | Create a 'BitmapIndexed' or 'Full' node.
-bitmapIndexedOrFull :: Bitmap -> A.Array (HashMap k v) -> HashMap k v
+bitmapIndexedOrFull :: Bitmap -> Array (HashMap k v) -> HashMap k v
-- The strictness in @ary@ helps achieve a nice code size reduction in
-- @unionWith[Key]@ with GHC 9.2.2. See the Core diffs in
--
https://github.com/haskell-unordered-containers/unordered-containers/pull/376.
@@ -956,17 +991,22 @@
-- | Replace the ith Leaf with Leaf k v.
--
-- This does not check that @i@ is within bounds of the array.
-setAtPosition :: Int -> k -> v -> A.Array (Leaf k v) -> A.Array (Leaf k v)
+setAtPosition :: Int -> k -> v -> Array (Leaf k v) -> Array (Leaf k v)
setAtPosition i k x ary = A.update ary i (L k x)
{-# INLINE setAtPosition #-}
--- | In-place update version of insert
-unsafeInsert :: forall k v. Hashable k => k -> v -> HashMap k v -> HashMap k v
-unsafeInsert k0 v0 m0 = runST (go h0 k0 v0 0 m0)
+-- | In-place update version of 'insert'.
+--
+-- The arrays of the 'BitmapIndexed' and 'Full' nodes of the argument are
+-- overwritten. This is only safe for a map that was built from 'empty' by
+-- @unsafeInsert*@ calls in the same 'ST' computation, each result being
+-- passed to the next call and not used otherwise.
+unsafeInsert :: forall k v s. Hashable k => k -> v -> HashMap k v -> ST s
(HashMap k v)
+unsafeInsert k0 v0 m0 = go h0 k0 v0 0 m0
where
h0 = hash k0
- go :: forall s. Hash -> k -> v -> Shift -> HashMap k v -> ST s (HashMap k
v)
+ go :: Hash -> k -> v -> Shift -> HashMap k v -> ST s (HashMap k v)
go !h !k x !_ Empty = return $! Leaf h (L k x)
go h k x s t@(Leaf hy l@(L ky y))
| hy == h = if ky == k
@@ -994,30 +1034,29 @@
where i = index h s
go h k x s t@(Collision hy v)
| h == hy = return $! Collision h (updateOrSnocWith (\a _ -> (# a
#)) k x v)
- | otherwise = go h k x s $ BitmapIndexed (mask hy s) (A.singleton t)
+ | otherwise = two s h k x hy t
{-# INLINABLE unsafeInsert #-}
--- | Create a map from two key-value pairs which hashes don't collide. To
--- enhance sharing, the second key-value pair is represented by the hash of its
--- key and a singleton HashMap pairing its key with its value.
+-- | Create a map from a key-value pair and an existing 'Leaf' or 'Collision'
+-- node. The two hashes must differ. To enhance sharing, the node is passed
+-- together with its hash and reused as is.
--
-- Note: to avoid silly thunks, this function must be strict in the
--- key. See issue #232. We don't need to force the HashMap argument
--- because it's already in WHNF (having just been matched) and we
--- just put it directly in an array.
+-- key. See issue #232. The node is forced too, so that no thunk can
+-- end up in the array.
two :: Shift -> Hash -> k -> v -> Hash -> HashMap k v -> ST s (HashMap k v)
two = go
where
- go s h1 k1 v1 h2 t2
+ go s h1 k1 v1 h2 !t2
| bp1 == bp2 = do
st <- go (nextShift s) h1 k1 v1 h2 t2
ary <- A.singletonM st
- return $ BitmapIndexed bp1 ary
+ return $! BitmapIndexed bp1 ary
| otherwise = do
mary <- A.new 2 $! Leaf h1 (L k1 v1)
A.write mary idx2 t2
ary <- A.unsafeFreeze mary
- return $ BitmapIndexed (bp1 .|. bp2) ary
+ return $! BitmapIndexed (bp1 .|. bp2) ary
where
bp1 = mask h1 s
bp2 = mask h2 s
@@ -1053,52 +1092,52 @@
-- thunks in the tree.
insertModifying :: Hashable k => v -> (v -> (# v #)) -> k -> HashMap k v
-> HashMap k v
-insertModifying x f k0 m0 = go h0 k0 0 m0
+insertModifying x0 f0 k0 m0 = go x0 f0 h0 k0 0 m0
where
!h0 = hash k0
- go !h !k !_ Empty = Leaf h (L k x)
- go h k s t@(Leaf hy l@(L ky y))
+ go x _ !h !k !_ Empty = Leaf h (L k x)
+ go x f h k s t@(Leaf hy l@(L ky y))
| hy == h = if ky == k
then case f y of
(# v' #) | ptrEq y v' -> t
| otherwise -> Leaf h (L k v')
else collision h l (L k x)
| otherwise = runST (two s h k x hy t)
- go h k s t@(BitmapIndexed b ary)
+ go x f h k s t@(BitmapIndexed b ary)
| b .&. m == 0 =
let ary' = A.insert ary i $! Leaf h (L k x)
in bitmapIndexedOrFull (b .|. m) ary'
| otherwise =
case A.index# ary i of
(# !st #) ->
- let !st' = go h k (nextShift s) st
+ let !st' = go x f h k (nextShift s) st
ary' = A.update ary i st'
in if ptrEq st st'
then t
else BitmapIndexed b ary'
where m = mask h s
i = sparseIndex b m
- go h k s t@(Full ary) =
+ go x f h k s t@(Full ary) =
case A.index# ary i of
(# !st #) ->
- let !st' = go h k (nextShift s) st
+ let !st' = go x f h k (nextShift s) st
ary' = updateFullArray ary i st'
in if ptrEq st st'
then t
else Full ary'
where i = index h s
- go h k s t@(Collision hy v)
+ go x f h k s t@(Collision hy v)
| h == hy =
let !v' = insertModifyingArr x f k v
in if A.unsafeSameArray v v'
then t
else Collision h v'
- | otherwise = go h k s $ BitmapIndexed (mask hy s) (A.singleton t)
+ | otherwise = go x f h k s $ BitmapIndexed (mask hy s) (A.singleton t)
{-# INLINABLE insertModifying #-}
-- | Like insertModifying for arrays; used to implement insertModifying
-insertModifyingArr :: Eq k => v -> (v -> (# v #)) -> k -> A.Array (Leaf k v)
- -> A.Array (Leaf k v)
+insertModifyingArr :: Eq k => v -> (v -> (# v #)) -> k -> Array (Leaf k v)
+ -> Array (Leaf k v)
insertModifyingArr x f k0 ary0 = go k0 ary0 0 (A.length ary0)
where
go !k !ary !i !n
@@ -1114,17 +1153,19 @@
| otherwise -> go k ary (i+1) n
{-# INLINE insertModifyingArr #-}
--- | In-place update version of insertWith
-unsafeInsertWith :: forall k v. Hashable k
+-- | In-place update version of 'insertWith'. See 'unsafeInsert' for when
+-- this is safe.
+unsafeInsertWith :: forall k v s. Hashable k
=> (v -> v -> v) -> k -> v -> HashMap k v
- -> HashMap k v
+ -> ST s (HashMap k v)
unsafeInsertWith f k0 v0 m0 = unsafeInsertWithKey (\_ a b -> (# f a b #)) k0
v0 m0
-{-# INLINABLE unsafeInsertWith #-}
+{-# INLINE unsafeInsertWith #-}
-unsafeInsertWithKey :: forall k v. Hashable k
+-- | Like 'unsafeInsertWith', but the combining function also takes the key.
+unsafeInsertWithKey :: forall k v s. Hashable k
=> (k -> v -> v -> (# v #)) -> k -> v -> HashMap k v
- -> HashMap k v
-unsafeInsertWithKey f k0 v0 m0 = runST (go h0 k0 v0 0 m0)
+ -> ST s (HashMap k v)
+unsafeInsertWithKey f k0 v0 m0 = go h0 k0 v0 0 m0
where
h0 = hash k0
go :: Hash -> k -> v -> Shift -> HashMap k v -> ST s (HashMap k v)
@@ -1154,8 +1195,9 @@
where i = index h s
go h k x s t@(Collision hy v)
| h == hy = return $! Collision h (updateOrSnocWithKey f k x v)
- | otherwise = go h k x s $ BitmapIndexed (mask hy s) (A.singleton t)
-{-# INLINABLE unsafeInsertWithKey #-}
+ | otherwise = two s h k x hy t
+-- INLINE, so that 'go' is specialised to the combining function.
+{-# INLINE unsafeInsertWithKey #-}
-- | \(O(\log n)\) Remove the mapping for the specified key from this map
-- if present.
@@ -1574,7 +1616,7 @@
{-# INLINABLE isSubmapOfBy #-}
-- | \(O(\min n m))\) Checks if a bitmap indexed node is a submap of another.
-submapBitmapIndexed :: (HashMap k v1 -> HashMap k v2 -> Bool) -> Bitmap ->
A.Array (HashMap k v1) -> Bitmap -> A.Array (HashMap k v2) -> Bool
+submapBitmapIndexed :: (HashMap k v1 -> HashMap k v2 -> Bool) -> Bitmap ->
Array (HashMap k v1) -> Bitmap -> Array (HashMap k v2) -> Bool
submapBitmapIndexed comp !b1 !ary1 !b2 !ary2 = subsetBitmaps && go 0 0 (b1Orb2
.&. negate b1Orb2)
where
go :: Int -> Int -> Bitmap -> Bool
@@ -1712,8 +1754,8 @@
{-# INLINE unionWithKey #-}
-- | Strict in the result of @f@.
-unionArrayBy :: (a -> a -> a) -> Bitmap -> Bitmap -> A.Array a -> A.Array a
- -> A.Array a
+unionArrayBy :: (a -> a -> a) -> Bitmap -> Bitmap -> Array a -> Array a
+ -> Array a
-- The manual forcing of @b1@, @b2@, @ary1@ and @ary2@ results in handsome
-- Core size reductions with GHC 9.2.2. See the Core diffs in
--
https://github.com/haskell-unordered-containers/unordered-containers/pull/376.
@@ -1947,7 +1989,7 @@
-- TODO: This could be faster if we would keep track of which elements of ary2
-- we've already matched. Those could be skipped when we check the following
-- elements of ary1.
-differenceCollisions :: Eq k => Hash -> A.Array (Leaf k v1) -> HashMap k v1 ->
Hash -> A.Array (Leaf k v2) -> HashMap k v1
+differenceCollisions :: Eq k => Hash -> Array (Leaf k v1) -> HashMap k v1 ->
Hash -> Array (Leaf k v2) -> HashMap k v1
differenceCollisions !h1 !ary1 t1 !h2 !ary2
| h1 == h2 =
if A.unsafeSameArray ary1 ary2
@@ -2112,7 +2154,7 @@
=> (v -> Maybe v)
-> Hash
-> k
- -> A.Array (Leaf k v)
+ -> Array (Leaf k v)
-> HashMap k v
-- ^ The original Collision node which will be re-used if the array is
unchanged.
-> HashMap k v
@@ -2133,7 +2175,7 @@
-- we've already matched. Those could be skipped when we check the following
-- elements of ary1.
-- TODO: Return tA when the array is unchanged.
-differenceWithKey_Collisions :: Eq k => (k -> v -> w -> Maybe v) -> Word ->
A.Array (Leaf k v) -> HashMap k v -> Word -> A.Array (Leaf k w) -> HashMap k v
+differenceWithKey_Collisions :: Eq k => (k -> v -> w -> Maybe v) -> Word ->
Array (Leaf k v) -> HashMap k v -> Word -> Array (Leaf k w) -> HashMap k v
differenceWithKey_Collisions f !hA !aryA !tA !hB !aryB
| hA == hB =
let f' l@(L kA vA) =
@@ -2234,8 +2276,8 @@
) ->
Bitmap ->
Bitmap ->
- A.Array (HashMap k v1) ->
- A.Array (HashMap k v2) ->
+ Array (HashMap k v1) ->
+ Array (HashMap k v2) ->
HashMap k v3
intersectionArrayBy f !b1 !b2 !ary1 !ary2
| b1 .&. b2 == 0 = Empty
@@ -2272,7 +2314,7 @@
bIntersect = b1 .&. b2
{-# INLINE intersectionArrayBy #-}
-intersectionCollisions :: Eq k => (k -> v1 -> v2 -> (# v3 #)) -> Hash -> Hash
-> A.Array (Leaf k v1) -> A.Array (Leaf k v2) -> HashMap k v3
+intersectionCollisions :: Eq k => (k -> v1 -> v2 -> (# v3 #)) -> Hash -> Hash
-> Array (Leaf k v1) -> Array (Leaf k v2) -> HashMap k v3
intersectionCollisions f h1 h2 ary1 ary2
| h1 == h2 = runST $ do
let !n2 = A.length ary2
@@ -2306,7 +2348,7 @@
-- undefined 2 1 4
-- @
-- We don't actually need to write undefined, we just have to make sure that
the next search starts 1 after the current one.
-searchSwap :: Eq k => A.MArray s (Leaf k v) -> Int -> k -> Int -> ST s (Maybe
(Leaf k v))
+searchSwap :: Eq k => MArray s (Leaf k v) -> Int -> k -> Int -> ST s (Maybe
(Leaf k v))
searchSwap mary n toFind start = go start toFind start
where
go i0 k i
@@ -2382,7 +2424,7 @@
disjointSubtrees s a b@Collision{} = disjointSubtrees s b a
{-# INLINABLE disjointSubtrees #-}
-disjointArrays :: Eq k => Shift -> Bitmap -> A.Array (HashMap k a) -> Bitmap
-> A.Array (HashMap k b) -> Bool
+disjointArrays :: Eq k => Shift -> Bitmap -> Array (HashMap k a) -> Bitmap ->
Array (HashMap k b) -> Bool
disjointArrays !s !bmA !aryA !bmB !aryB = go (bmA .&. bmB)
where
go 0 = True
@@ -2400,7 +2442,7 @@
-- TODO: GHC 9.12.2 inlines disjointCollisions into `disjoint @Int`.
-- How do you prevent this while preserving specialization?
--
https://stackoverflow.com/questions/79838305/ensuring-specialization-while-preventing-inlining
-disjointCollisions :: Eq k => Hash -> A.Array (Leaf k a) -> Hash -> A.Array
(Leaf k b) -> Bool
+disjointCollisions :: Eq k => Hash -> Array (Leaf k a) -> Hash -> Array (Leaf
k b) -> Bool
disjointCollisions !hA !aryA !hB !aryB
| hA == hB = A.all predicate aryA
| otherwise = True
@@ -2566,7 +2608,7 @@
mary <- A.new_ n
step ary0 mary b0 0 0 1 n
where
- step :: A.Array (HashMap k v1) -> A.MArray s (HashMap k v2)
+ step :: Array (HashMap k v1) -> MArray s (HashMap k v2)
-> Bitmap -> Int -> Int -> Bitmap -> Int
-> ST s (HashMap k v2)
step !ary !mary !b i !j !bi n
@@ -2598,7 +2640,7 @@
mary <- A.new_ n
step ary0 mary 0 0 n
where
- step :: A.Array (Leaf k v1) -> A.MArray s (Leaf k v2)
+ step :: Array (Leaf k v1) -> MArray s (Leaf k v2)
-> Int -> Int -> Int
-> ST s (HashMap k v2)
step !ary !mary i !j n
@@ -2653,8 +2695,24 @@
-- | \(O(n \log n)\) Construct a map with the supplied mappings. If the list
-- contains duplicate mappings, the later mappings take precedence.
fromList :: Hashable k => [(k, v)] -> HashMap k v
-fromList = List.foldl' (\ m (k, v) -> unsafeInsert k v m) empty
-{-# INLINABLE fromList #-}
+-- See Note [fromList arity]
+fromList = \ xs -> runST $ foldlST' (\ m (k, v) -> unsafeInsert k v m) empty xs
+{-# INLINE fromList #-}
+
+{-
+Note [fromList arity]
+~~~~~~~~~~~~~~~~~~~~~
+
+The fromList* functions are INLINE, so that the fold can fuse with a good
+producer of the list. They bind the list with a lambda:
+
+ fromListWith f = \ xs -> ...
+
+GHC unfolds an INLINE function only when it is applied to all the arguments
+left of the '='. With the lambda, a partial application like @fromListWith f@
+still unfolds. (GHC 9.0 to 10.0 don't count the Hashable dictionary and unfold
+one argument early: https://gitlab.haskell.org/ghc/ghc/-/issues/27589.)
+-}
-- | \(O(n \log n)\) Construct a map from a list of elements. Uses
-- the provided function @f@ to merge duplicate entries with
@@ -2687,7 +2745,8 @@
-- > fromListWith f [(k, a), (k, b), (k, c), (k, d)]
-- > = fromList [(k, f d (f c (f b a)))]
fromListWith :: Hashable k => (v -> v -> v) -> [(k, v)] -> HashMap k v
-fromListWith f = List.foldl' (\ m (k, v) -> unsafeInsertWith f k v m) empty
+-- See Note [fromList arity]
+fromListWith f = \ xs -> runST $ foldlST' (\ m (k, v) -> unsafeInsertWith f k
v m) empty xs
{-# INLINE fromListWith #-}
-- | \(O(n \log n)\) Construct a map from a list of elements. Uses
@@ -2717,7 +2776,8 @@
--
-- @since 0.2.11
fromListWithKey :: Hashable k => (k -> v -> v -> v) -> [(k, v)] -> HashMap k v
-fromListWithKey f = List.foldl' (\ m (k, v) -> unsafeInsertWithKey (\k' a b ->
(# f k' a b #)) k v m) empty
+-- See Note [fromList arity]
+fromListWithKey f = \ xs -> runST $ foldlST' (\ m (k, v) ->
unsafeInsertWithKey (\k' a b -> (# f k' a b #)) k v m) empty xs
{-# INLINE fromListWithKey #-}
------------------------------------------------------------------------
@@ -2731,11 +2791,11 @@
#else
forall r k v.
#endif
- Eq k => ((# #) -> r) -> (v -> Int -> r) -> k -> A.Array (Leaf k v) -> r
+ Eq k => ((# #) -> r) -> (v -> Int -> r) -> k -> Array (Leaf k v) -> r
lookupInArrayCont absent present k0 ary0 =
lookupInArrayCont_ k0 ary0 0 (A.length ary0)
where
- lookupInArrayCont_ :: Eq k => k -> A.Array (Leaf k v) -> Int -> Int -> r
+ lookupInArrayCont_ :: Eq k => k -> Array (Leaf k v) -> Int -> Int -> r
lookupInArrayCont_ !k !ary !i !n
| i >= n = absent (# #)
| otherwise = case A.index# ary i of
@@ -2746,7 +2806,7 @@
-- | \(O(n)\) Lookup the value associated with the given key in this
-- array. Returns 'Nothing' if the key wasn't found.
-indexOf :: Eq k => k -> A.Array (Leaf k v) -> Maybe Int
+indexOf :: Eq k => k -> Array (Leaf k v) -> Maybe Int
indexOf k0 ary0 = go k0 ary0 0 (A.length ary0)
where
go !k !ary !i !n
@@ -2757,7 +2817,7 @@
| otherwise -> go k ary (i+1) n
{-# INLINABLE indexOf #-}
-updateWith# :: Eq k => (v -> (# v #)) -> k -> A.Array (Leaf k v) -> A.Array
(Leaf k v)
+updateWith# :: Eq k => (v -> (# v #)) -> k -> Array (Leaf k v) -> Array (Leaf
k v)
updateWith# f k0 ary0 = go k0 ary0 0 (A.length ary0)
where
go !k !ary !i !n
@@ -2770,13 +2830,13 @@
| otherwise -> go k ary (i+1) n
{-# INLINABLE updateWith# #-}
-updateOrSnocWith :: Eq k => (v -> v -> (# v #)) -> k -> v -> A.Array (Leaf k v)
- -> A.Array (Leaf k v)
+updateOrSnocWith :: Eq k => (v -> v -> (# v #)) -> k -> v -> Array (Leaf k v)
+ -> Array (Leaf k v)
updateOrSnocWith f = updateOrSnocWithKey (const f)
{-# INLINABLE updateOrSnocWith #-}
-updateOrSnocWithKey :: Eq k => (k -> v -> v -> (# v #)) -> k -> v -> A.Array
(Leaf k v)
- -> A.Array (Leaf k v)
+updateOrSnocWithKey :: Eq k => (k -> v -> v -> (# v #)) -> k -> v -> Array
(Leaf k v)
+ -> Array (Leaf k v)
updateOrSnocWithKey f k0 v0 ary0 = go k0 v0 ary0 0 (A.length ary0)
where
go !k v !ary !i !n
@@ -2789,7 +2849,7 @@
| otherwise -> go k v ary (i+1) n
{-# INLINABLE updateOrSnocWithKey #-}
-updateOrConcatWithKey :: Eq k => (k -> v -> v -> (# v #)) -> A.Array (Leaf k
v) -> A.Array (Leaf k v) -> A.Array (Leaf k v)
+updateOrConcatWithKey :: Eq k => (k -> v -> v -> (# v #)) -> Array (Leaf k v)
-> Array (Leaf k v) -> Array (Leaf k v)
updateOrConcatWithKey f ary1 ary2 = A.run $ do
-- TODO: instead of mapping and then folding, should we traverse?
-- We'll have to be careful to avoid allocating pairs or similar.
@@ -2822,7 +2882,7 @@
{-# INLINABLE updateOrConcatWithKey #-}
-- | \(O(n*m)\) Check if the first array is a subset of the second array.
-subsetArray :: Eq k => (v1 -> v2 -> Bool) -> A.Array (Leaf k v1) -> A.Array
(Leaf k v2) -> Bool
+subsetArray :: Eq k => (v1 -> v2 -> Bool) -> Array (Leaf k v1) -> Array (Leaf
k v2) -> Bool
subsetArray cmpV ary1 ary2 = A.length ary1 <= A.length ary2 && A.all inAry2
ary1
where
inAry2 (L k1 v1) = lookupInArrayCont (\_ -> False) (\v2 _ -> cmpV v1 v2)
k1 ary2
@@ -2832,12 +2892,12 @@
-- Manually unrolled loops
-- | \(O(n)\) Update the element at the given position in this array.
-updateFullArray :: A.Array e -> Int -> e -> A.Array e
+updateFullArray :: Array e -> Int -> e -> Array e
updateFullArray ary idx b = runST (updateFullArrayM ary idx b)
{-# INLINE updateFullArray #-}
-- | \(O(n)\) Update the element at the given position in this array.
-updateFullArrayM :: A.Array e -> Int -> e -> ST s (A.Array e)
+updateFullArrayM :: Array e -> Int -> e -> ST s (Array e)
updateFullArrayM ary idx b = do
mary <- clone ary
A.write mary idx b
@@ -2845,7 +2905,7 @@
{-# INLINE updateFullArrayM #-}
-- | \(O(n)\) Update the element at the given position in this array, by
applying a function to it.
-updateFullArrayWith' :: A.Array e -> Int -> (e -> e) -> A.Array e
+updateFullArrayWith' :: Array e -> Int -> (e -> e) -> Array e
updateFullArrayWith' ary idx f =
case A.index# ary idx of
(# x #) -> updateFullArray ary idx $! f x
@@ -2853,7 +2913,7 @@
-- | Unsafely clone an array of (2^bitsPerSubkey) elements. The length of the
input
-- array is not checked.
-clone :: A.Array e -> ST s (A.MArray s e)
+clone :: Array e -> ST s (MArray s e)
clone ary =
A.thaw ary 0 (2^bitsPerSubkey)
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn'
'--exclude=.svnignore' old/unordered-containers-0.2.21/Data/HashSet/Internal.hs
new/unordered-containers-0.2.21.1/Data/HashSet/Internal.hs
--- old/unordered-containers-0.2.21/Data/HashSet/Internal.hs 2001-09-09
03:46:40.000000000 +0200
+++ new/unordered-containers-0.2.21.1/Data/HashSet/Internal.hs 2001-09-09
03:46:40.000000000 +0200
@@ -92,15 +92,17 @@
, keysSet
) where
-import Control.DeepSeq (NFData (..), NFData1 (..), liftRnf2)
-import Data.Data (Constr, Data (..), DataType)
+import Control.DeepSeq (NFData (..), NFData1 (..), liftRnf2)
+import Control.Monad.ST (runST)
+import Data.Data (Constr, Data (..), DataType)
import Data.Functor.Classes
-import Data.Hashable (Hashable (hashWithSalt))
-import Data.Hashable.Lifted (Hashable1 (..), Hashable2 (..))
-import Data.HashMap.Internal (HashMap, equalKeys, equalKeys1, foldMapWithKey,
- foldlWithKey, foldrWithKey)
-import Data.Semigroup (Semigroup (..), stimesIdempotentMonoid)
-import Prelude hiding (Foldable(..), filter, map)
+import Data.Hashable (Hashable (hashWithSalt))
+import Data.Hashable.Lifted (Hashable1 (..), Hashable2 (..))
+import Data.HashMap.Internal (HashMap, equalKeys, equalKeys1,
foldMapWithKey,
+ foldlWithKey, foldrWithKey)
+import Data.HashMap.Internal.ST (foldlST')
+import Data.Semigroup (Semigroup (..), stimesIdempotentMonoid)
+import Prelude hiding (Foldable (..), filter, map)
import Text.Read
import qualified Data.Data as Data
@@ -160,6 +162,8 @@
instance Ord1 HashSet where
liftCompare c (HashSet a) (HashSet b) = liftCompare2 c compare a b
+-- | Note that 'Foldable.elem' is \(O(n)\). For a 'Hashable' element
+-- type, use 'member', which is \(O(\log n)\).
instance Foldable.Foldable HashSet where
foldMap f = foldMapWithKey (\a _ -> f a) . asMap
foldr = foldr
@@ -471,7 +475,8 @@
-- | \(O(n \log n)\) Construct a set from a list of elements.
fromList :: Hashable a => [a] -> HashSet a
-fromList = HashSet . List.foldl' (\ m k -> H.unsafeInsert k () m) H.empty
+-- See Note [fromList arity] in Data.HashMap.Internal
+fromList = \ xs -> HashSet $ runST $ foldlST' (\ m k -> H.unsafeInsert k () m)
H.empty xs
{-# INLINE fromList #-}
#if defined(__GLASGOW_HASKELL__)
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn'
'--exclude=.svnignore'
old/unordered-containers-0.2.21/benchmarks/FineGrained.hs
new/unordered-containers-0.2.21.1/benchmarks/FineGrained.hs
--- old/unordered-containers-0.2.21/benchmarks/FineGrained.hs 2001-09-09
03:46:40.000000000 +0200
+++ new/unordered-containers-0.2.21.1/benchmarks/FineGrained.hs 2001-09-09
03:46:40.000000000 +0200
@@ -26,9 +26,11 @@
defaultMain
[ bgroup
"HashMap.Strict"
- [ bFromList,
+ [ bSize,
+ bFromList,
bLookup,
bInsert,
+ bInsertWith,
bUpdate,
bAlter,
bDelete,
@@ -42,7 +44,7 @@
]
defaultSizes :: [Int]
-defaultSizes = [0, 1, 10, 100, 1000, 10_000, 100_000]
+defaultSizes = [0, 1, 5, 10, 50, 100, 500, 1_000, 5_000, 10_000, 50_000,
100_000, 500_000]
-- | Length of a 'Bytes' key in bytes.
--
@@ -56,6 +58,11 @@
defaultGen :: StdGen
defaultGen = mkStdGen 42
+bSize :: Benchmark
+bSize = bgroup "size" [bgroup' "Int" genIntMap b]
+ where
+ b s = bench (show s) . whnf (\m -> HM.size m)
+
bFromList :: Benchmark
bFromList =
bgroup
@@ -175,6 +182,75 @@
setupBytes size gen = do
m <- genBytesMap size gen
ks <- genNBytes 2000 bytesLength gen
+ let kvs = take 1000 $ Data.List.cycle $ map (,1) $ filter (not . flip
HM.member m) ks
+ return (m, kvs)
+ setupInts size gen = do
+ m <- genIntMap size gen
+ ks <- genInts 2000 gen
+ let kvs = take 1000 $ Data.List.cycle $ map (,1) $ filter (not . flip
HM.member m) ks
+ return (m, kvs)
+
+-- 1000 insertions each, so we get more precise timings
+bInsertWith :: Benchmark
+bInsertWith =
+ bgroup
+ "insertWith (1000x)"
+ [ bgroup
+ "presentKey"
+ [ bgroup "sameValue" bInsertWithPresentKeySameValue,
+ bgroup "differentValue" bInsertWithPresentKeyDifferentValue
+ ],
+ bgroup "absentKey" bInsertWithAbsentKey
+ ]
+
+bInsertWithPresentKeySameValue :: [Benchmark]
+bInsertWithPresentKeySameValue =
+ [ bgroup'WithSizes sizes "Bytes" setupBytes b,
+ bgroup'WithSizes sizes "Int" setupInts b
+ ]
+ where
+ sizes = filter (/= 0) defaultSizes
+ b s =
+ bench (show s)
+ . whnf (\(m, kvs) -> foldl' (\() (k, v) -> HM.insertWith (\_ old ->
old) k v m `seq` ()) () kvs)
+ toKVs = take 1000 . Data.List.cycle . HM.toList
+ setupBytes size gen = do
+ m <- genBytesMap size gen
+ return (m, toKVs m)
+ setupInts size gen = do
+ m <- genIntMap size gen
+ return (m, toKVs m)
+
+bInsertWithPresentKeyDifferentValue :: [Benchmark]
+bInsertWithPresentKeyDifferentValue =
+ [ bgroup'WithSizes sizes "Bytes" setupBytes b,
+ bgroup'WithSizes sizes "Int" setupInts b
+ ]
+ where
+ sizes = filter (/= 0) defaultSizes
+ b s =
+ bench (show s)
+ . whnf (\(m, kvs) -> foldl' (\() (k, v) -> HM.insertWith (+) k v m
`seq` ()) () kvs)
+ toKVs = take 1000 . Data.List.cycle . HM.toList
+ setupBytes size gen = do
+ m <- genBytesMap size gen
+ return (m, toKVs m)
+ setupInts size gen = do
+ m <- genIntMap size gen
+ return (m, toKVs m)
+
+bInsertWithAbsentKey :: [Benchmark]
+bInsertWithAbsentKey =
+ [ bgroup' "Bytes" setupBytes b,
+ bgroup' "Int" setupInts b
+ ]
+ where
+ b s =
+ bench (show s)
+ . whnf (\(m, kvs) -> foldl' (\() (k, v) -> HM.insertWith (+) k v m
`seq` ()) () kvs)
+ setupBytes size gen = do
+ m <- genBytesMap size gen
+ ks <- genNBytes 2000 bytesLength gen
let kvs = take 1000 $ Data.List.cycle $ map (,1) $ filter (not . flip
HM.member m) ks
return (m, kvs)
setupInts size gen = do
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn'
'--exclude=.svnignore' old/unordered-containers-0.2.21/tests/Regressions.hs
new/unordered-containers-0.2.21.1/tests/Regressions.hs
--- old/unordered-containers-0.2.21/tests/Regressions.hs 2001-09-09
03:46:40.000000000 +0200
+++ new/unordered-containers-0.2.21.1/tests/Regressions.hs 2001-09-09
03:46:40.000000000 +0200
@@ -10,8 +10,11 @@
import Control.Exception (evaluate)
import Control.Monad (replicateM)
import Data.Bits (shiftL)
+import Data.Char (digitToInt)
+import Data.Coerce (coerce)
+import Data.Functor.Identity (Identity (..))
import Data.Hashable (Hashable (..))
-import Data.List (delete)
+import Data.List (delete, sort)
import Data.Maybe (isJust, isNothing)
import GHC.Exts (touch#)
import GHC.IO (IO (..))
@@ -19,19 +22,22 @@
import System.Mem (performGC)
import System.Mem.Weak (deRefWeak, mkWeakPtr)
import System.Random (randomIO)
-import Test.HUnit (Assertion, assert)
+import Test.HUnit (Assertion, assert, assertFailure, (@?=))
import Test.QuickCheck
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit (testCase)
import Test.Tasty.QuickCheck (testProperty)
+import Util.Key (Key, collisionAtHash, incKey)
-import qualified Data.HashMap.Lazy as HML
-import qualified Data.HashMap.Strict as HMS
-import qualified Data.HashSet as HS
-import qualified Test.Tasty as Tasty
+import qualified Data.HashMap.Internal as HMI
+import qualified Data.HashMap.Internal.Array as A
+import qualified Data.HashMap.Lazy as HML
+import qualified Data.HashMap.Strict as HMS
+import qualified Data.HashSet as HS
+import qualified Test.Tasty as Tasty
import qualified Data.Foldable as Foldable
-import NoThunks.Class (noThunksInValues)
+import NoThunks.Class (OnlyCheckWhnf (..), noThunksInValues)
issue32 :: Assertion
issue32 = assert $ isJust $ HMS.lookup 7 m'
@@ -262,6 +268,93 @@
where m = HS.toMap . HS.fromList @Int
------------------------------------------------------------------------
+-- Issue #599
+
+issue599 :: TestTree
+issue599 = testGroup "issue599"
+ [ testCase "Empty" $ noThunksAfterAlterF []
+ , testCase "Leaf, different hash" $ noThunksAfterAlterF [k1]
+ , testCase "Leaf, same hash" $ noThunksAfterAlterF [kB]
+ , testCase "BitmapIndexed" $ noThunksAfterAlterF [k1, k2]
+ , testCase "Collision" $ noThunksAfterAlterF [kB, kC]
+ , testCase "Leaf, present key" $ noThunksAfterAlterF [kA]
+ , testCase "Collision, present key" $ noThunksAfterAlterF [kA, kB]
+ , testCase "present key, unused default" $ do
+ let f = Identity . Just . maybe (error "Should not be evaluated")
opaqueSucc
+ m <- evaluate $ runIdentity $ HMS.alterF f kA (HMS.singleton kA 1)
+ assert $ m == HMS.singleton kA 2
+ ]
+ where
+ (kA, kB, kC, _) = collisionAtHash 0
+ k1 = incKey kA
+ k2 = incKey k1
+
+ noThunksAfterAlterF ks = do
+ v <- randomIO
+ let m = issue599AlterF v kA (HMS.fromList [(k, 0) | k <- ks])
+ mThunkInfo <- noThunksInValues mempty (Foldable.toList m)
+ assert $ isNothing mThunkInfo
+
+-- The shape of the function makes the "alterFinsertWith" rule fire.
+issue599AlterF :: Int -> Key -> HMS.HashMap Key Int -> HMS.HashMap Key Int
+issue599AlterF v k m =
+ runIdentity $ HMS.alterF (Identity . Just . maybe (opaqueSucc v) opaqueSucc)
k m
+
+opaqueSucc :: Int -> Int
+opaqueSucc = succ
+{-# NOINLINE opaqueSucc #-}
+
+------------------------------------------------------------------------
+-- Issue #601
+
+-- | A key that hashes to the number it spells: @hash (Numeral "33") == 33@.
+newtype Numeral = Numeral String
+ deriving Eq
+
+instance Hashable Numeral where
+ -- Whether 'issue601' triggers the bug is sensitive to how this is written.
+ hash (Numeral s) = Foldable.foldl' (\h c -> 10 * h + digitToInt c) 0 s
+ hashWithSalt salt k = hashWithSalt salt (hash k)
+
+-- 'HS.fromList' used to build the set with in-place inserts in a pure fold.
+-- GHC could therefore share the set for the known prefix between the calls
+-- of @fromListWithPrefix@, and the second call below returned [1, 2, 33, 65].
+issue601 :: Assertion
+issue601 = do
+ -- 33 and 65 go to the root slot that 1 occupies.
+ hashes (fromListWithPrefix [Numeral "33"]) @?= [1, 2, 33]
+ hashes (fromListWithPrefix [Numeral "65"]) @?= [1, 2, 65]
+ where
+ fromListWithPrefix :: [Numeral] -> HS.HashSet Numeral
+ fromListWithPrefix ks = HS.fromList ([Numeral "1", Numeral "2"] ++ ks)
+ {-# NOINLINE fromListWithPrefix #-}
+
+ hashes = sort . map hash . HS.toList
+
+------------------------------------------------------------------------
+-- GHC #27507
+
+-- Strict 'HMS.insertWith' used to store the updated leaf of a collision as a
+-- thunk: https://gitlab.haskell.org/ghc/ghc/-/issues/27507
+insertWithCollisionLeaf :: Assertion
+insertWithCollisionLeaf = do
+ v <- randomIO
+ m <- evaluate $ insertWithPlus kA v (HMS.fromList [(kA, 0), (kB, 0)])
+ case m of
+ HMI.Collision _ ary -> do
+ let leaves = coerce (A.toList ary) :: [OnlyCheckWhnf (HMI.Leaf Key Int)]
+ mThunkInfo <- noThunksInValues mempty leaves
+ assert $ isNothing mThunkInfo
+ _ -> assertFailure "Expected a Collision"
+ where
+ (kA, kB, _, _) = collisionAtHash 0
+
+-- NOINLINE prevents specialization to 'Key', which hides the thunk.
+insertWithPlus :: Hashable k => k -> Int -> HMS.HashMap k Int -> HMS.HashMap k
Int
+insertWithPlus = HMS.insertWith (+)
+{-# NOINLINE insertWithPlus #-}
+
+------------------------------------------------------------------------
-- * Test list
tests :: TestTree
@@ -286,4 +379,7 @@
, testCase "issue383" issue383
, testCase "issue420" issue420
, issue491
+ , issue599
+ , testCase "issue601" issue601
+ , testCase "Strict.insertWith: collision leaves are not thunks"
insertWithCollisionLeaf
]
diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn'
'--exclude=.svnignore'
old/unordered-containers-0.2.21/unordered-containers.cabal
new/unordered-containers-0.2.21.1/unordered-containers.cabal
--- old/unordered-containers-0.2.21/unordered-containers.cabal 2001-09-09
03:46:40.000000000 +0200
+++ new/unordered-containers-0.2.21.1/unordered-containers.cabal
2001-09-09 03:46:40.000000000 +0200
@@ -1,5 +1,5 @@
name: unordered-containers
-version: 0.2.21
+version: 0.2.21.1
synopsis: Efficient hashing-based container types
description:
Efficient hashing-based container types. The containers have been
@@ -29,8 +29,9 @@
extra-source-files: CHANGES.md
tested-with:
- GHC ==9.12.2
- || ==9.10.2
+ GHC ==9.14.1
+ || ==9.12.2
+ || ==9.10.3
|| ==9.8.4
|| ==9.6.7
|| ==9.4.8
@@ -48,6 +49,7 @@
Data.HashMap.Internal.Array
Data.HashMap.Internal.Debug
Data.HashMap.Internal.List
+ Data.HashMap.Internal.ST
Data.HashMap.Internal.Strict
Data.HashMap.Lazy
Data.HashMap.Strict
@@ -60,7 +62,7 @@
hashable >= 1.4 && < 1.6
if impl(ghc)
build-depends:
- template-haskell >= 2.16 && < 2.24
+ template-haskell >= 2.16 && < 2.25
default-language: Haskell2010
@@ -146,8 +148,8 @@
bytestring >= 0.11.3,
deepseq,
hashable,
- random,
- tasty-bench,
+ random >= 1.3.0,
+ tasty-bench >= 0.4.1,
unordered-containers
default-language: Haskell2010