vector-hashtables 0.1.2.1 → 0.1.2.2
raw patch · 6 files changed
+179/−22 lines, 6 filesdep +deepseqdep −quickcheck-instancesdep −unordered-containersPVP: minor bump suggested
API additions: PVP suggests at least a minor version bump
Dependencies added: deepseq
Dependencies removed: quickcheck-instances, unordered-containers
API changes (from Hackage documentation)
+ Data.Vector.Hashtables.Internal: atWithPrevOrElse :: forall (ks :: Type -> Type -> Type) k (vs :: Type -> Type -> Type) v m a. (MVector ks k, MVector vs v, PrimMonad m, Hashable k, Eq k) => Dictionary (PrimState m) ks k vs v -> k -> (Dictionary (PrimState m) ks k vs v -> Int -> Int -> m a) -> (Dictionary (PrimState m) ks k vs v -> m a) -> m a
Files
- README.md +2/−0
- bench/Main.hs +78/−1
- changelog.md +5/−0
- src/Data/Vector/Hashtables/Internal.hs +33/−8
- test/Data/Vector/HashTablesSpec.hs +46/−1
- vector-hashtables.cabal +15/−12
README.md view
@@ -20,6 +20,8 @@ ``` (You will need the `bench-show` tool, which is available from Hackage.) +You can also see the plots produced from the same numbers in [./charts/readme.md](./charts/readme.md).+ | Benchmark | v.0.1.1.4 (ns) | current (ns) | | ----------------------------------------------------------------- | ------------ | ------------ |
bench/Main.hs view
@@ -13,6 +13,11 @@ import Criterion (Benchmark) import qualified Data.Vector.Hashtables.Internal as VH+import qualified Data.Vector as BVec+import Control.DeepSeq (NFData (..))+import Criterion.Main (perRunEnv)+import Data.Bits (shiftR, xor)+import Data.Hashable (Hashable (..)) vh :: Int -> IO (VH.Dictionary (PrimState IO) VM.MVector Int VM.MVector Int) vh n = do@@ -224,7 +229,8 @@ utilitiesBench <- mapM utilities inputs defaultMain [ bgroup "Comparison" comparisonBench- , bgroup "Utilities" utilitiesBench ]+ , bgroup "Utilities" utilitiesBench+ , bgroup "AlterDelete" alterDeleteBench ] -- ** Utilities benchmark @@ -348,3 +354,74 @@ return () bhutoList ht = VH.toList ht >> return ()++-- ** Deleting through alter/alterM, against delete+--+-- Only the deletes are timed: every run gets a freshly filled table+-- (perRunEnv), and deletes every key in insertion order.++-- | Every key in one bucket chain.+newtype Collide = Collide Int deriving (Eq, Show)++instance Hashable Collide where+ hashWithSalt _ _ = 0+ hash _ = 0++-- | Lets perRunEnv hold a mutable table.+newtype Env a = Env a++instance NFData (Env a) where+ rnf (Env a) = a `seq` ()++type IntTable = VH.Dictionary (PrimState IO) VM.MVector Int VM.MVector Int+type CollideTable = VH.Dictionary (PrimState IO) BV.MVector Collide VM.MVector Int++-- | Distinct pseudo-random keys (SplitMix's finaliser is a bijection), so+-- buckets collide the way real keys do; sequential Int keys never collide.+randomKeys :: Int -> [Int]+randomKeys n = map mix [1 .. n + 1]+ where+ mix z0 = let z1 = (z0 `xor` (z0 `shiftR` 30)) * 0xbf58476d1ce4e5b9+ z2 = (z1 `xor` (z1 `shiftR` 27)) * 0x94d049bb133111eb+ in z2 `xor` (z2 `shiftR` 31)++fillInt :: BVec.Vector Int -> IO (Env IntTable)+fillInt ks = do+ ht <- VH.initialize (BVec.length ks)+ BVec.mapM_ (\k -> VH.insert ht k k) ks+ return (Env ht)++fillCollide :: BVec.Vector Collide -> IO (Env CollideTable)+fillCollide ks = do+ ht <- VH.initialize (BVec.length ks)+ BVec.mapM_ (\k@(Collide i) -> VH.insert ht k i) ks+ return (Env ht)++deleteGroup+ :: (BVec.Vector k -> IO (Env (VH.Dictionary (PrimState IO) ks k vs Int)))+ -> (VH.Dictionary (PrimState IO) ks k vs Int -> k -> IO ())+ -> (VH.Dictionary (PrimState IO) ks k vs Int -> k -> IO ())+ -> (VH.Dictionary (PrimState IO) ks k vs Int -> k -> IO ())+ -> String -> BVec.Vector k -> Benchmark+deleteGroup fill del alt altM name ks = bgroup name+ [ bench "delete" $ perRunEnv (fill ks) (\(Env ht) -> BVec.mapM_ (del ht) ks)+ , bench "alter" $ perRunEnv (fill ks) (\(Env ht) -> BVec.mapM_ (alt ht) ks)+ , bench "alterM" $ perRunEnv (fill ks) (\(Env ht) -> BVec.mapM_ (altM ht) ks) ]+{-# INLINE deleteGroup #-}++intDeletes :: String -> BVec.Vector Int -> Benchmark+intDeletes = deleteGroup fillInt VH.delete+ (\ht -> VH.alter ht (const Nothing)) (\ht -> VH.alterM ht (const (return Nothing)))++collideDeletes :: String -> BVec.Vector Collide -> Benchmark+collideDeletes = deleteGroup fillCollide VH.delete+ (\ht -> VH.alter ht (const Nothing)) (\ht -> VH.alterM ht (const (return Nothing)))++alterDeleteBench :: [Benchmark]+alterDeleteBench =+ [ bgroup (show n)+ [ intDeletes "sequential" (BVec.fromList [0 .. n])+ , intDeletes "random" (BVec.fromList (randomKeys n)) ]+ | n <- [10000, 1000000] ] +++ [ bgroup (show n) [ collideDeletes "one chain" (BVec.fromList (map Collide [0 .. n])) ]+ | n <- [1000] ]
changelog.md view
@@ -1,3 +1,8 @@+# 0.1.2.2 (2026-11-28)++* Add separate benchmark for `alter` vs `alterM` vs `delete` (see [#45](https://github.com/klapaucius/vector-hashtables/pull/45)).+* Fix `alter` and `alterM` losing other keys of the same bucket when deleting (see [#43](https://github.com/klapaucius/vector-hashtables/issues/43)).+ # 0.1.2.1 (2025-06-17) * Bump `QuickCheck` version (see [#34](https://github.com/klapaucius/vector-hashtables/pull/34)).
src/Data/Vector/Hashtables/Internal.hs view
@@ -261,6 +261,31 @@ else onNothing d {-# INLINE atWithOrElse #-} +-- | Like 'atWithOrElse', but @onFound@ also gets the found entry's+-- predecessor in its bucket chain (@-1@ if the entry is the head).+atWithPrevOrElse :: (MVector ks k, MVector vs v, PrimMonad m, Hashable k, Eq k)+ => Dictionary (PrimState m) ks k vs v+ -> k+ -> (Dictionary (PrimState m) ks k vs v -> Int -> Int -> m a)+ -> (Dictionary (PrimState m) ks k vs v -> m a)+ -> m a+atWithPrevOrElse d key' onFound onNothing = do+ Dictionary{..} <- readMutVar . getDRef $ d+ let hashCode' = hash key' .&. mask+ go !prev !i+ | i >= 0 = do+ hc <- hashCode ! i+ if hc == hashCode'+ then do+ k <- key !~ i+ if k == key'+ then onFound d prev i+ else go i =<< next ! i+ else go i =<< next ! i+ | otherwise = onNothing d+ go (-1) =<< buckets ! (hashCode' `fastRem` remSize)+{-# INLINE atWithPrevOrElse #-}+ -- | /O(1)/ in the best case, /O(n)/ in the worst case. -- Find dictionary entry by given key. If entry not found @-1@ returned. findEntry :: (MVector ks k, MVector vs v, PrimMonad m, Hashable k, Eq k)@@ -687,13 +712,13 @@ !targetBucket = hashCode' `fastRem` remSize onFound' value' dict i = insertWithIndex targetBucket hashCode' k value' (getDRef ht) dict i- onNothing' dict i = deleteWithIndex targetBucket hashCode' d k (-1) i+ onNothing' prev i = deleteWithIndex targetBucket hashCode' d k prev i - onFound dict i = do+ onFound dict prev i = do d'@Dictionary{..} <- readMutVar . getDRef $ dict v <- value !~ i case f (Just v) of- Nothing -> onNothing' d' i+ Nothing -> onNothing' prev i Just v' -> onFound' v' d' i onNothing dict = do@@ -702,7 +727,7 @@ Nothing -> return () Just v' -> onFound' v' d' (-1) - void $ atWithOrElse ht k onFound onNothing+ void $ atWithPrevOrElse ht k onFound onNothing {-# INLINE alter #-} @@ -721,14 +746,14 @@ !targetBucket = hashCode' `fastRem` remSize onFound' value' dict i = insertWithIndex targetBucket hashCode' k value' (getDRef ht) dict i- onNothing' dict i = deleteWithIndex targetBucket hashCode' d k (-1) i+ onNothing' prev i = deleteWithIndex targetBucket hashCode' d k prev i - onFound dict i = do+ onFound dict prev i = do d'@Dictionary{..} <- readMutVar . getDRef $ dict v <- value !~ i res <- f (Just v) case res of- Nothing -> onNothing' d' i+ Nothing -> onNothing' prev i Just v' -> onFound' v' d' i onNothing dict = do@@ -738,7 +763,7 @@ Nothing -> return () Just v' -> onFound' v' d' (-1) - void $ atWithOrElse ht k onFound onNothing+ void $ atWithPrevOrElse ht k onFound onNothing {-# INLINE alterM #-}
test/Data/Vector/HashTablesSpec.hs view
@@ -32,7 +32,7 @@ Positive (..), Property, choose, elements, forAll, generate, property, shuffle,- vector)+ vector, (==>)) import Test.Hspec (Spec, describe, errorCall, it, shouldBe, shouldThrow)@@ -104,6 +104,17 @@ testAtCollide :: (VH.Dictionary (PrimState IO) ks AlwaysCollide vs Int) -> AlwaysCollide -> IO Int + testAlterCollide+ :: (VH.Dictionary (PrimState IO) ks AlwaysCollide vs Int)+ -> (Maybe Int -> Maybe Int) -> AlwaysCollide -> IO ()++ testAlterMCollide+ :: (VH.Dictionary (PrimState IO) ks AlwaysCollide vs Int)+ -> (Maybe Int -> IO (Maybe Int)) -> AlwaysCollide -> IO ()++ testLookupCollide+ :: (VH.Dictionary (PrimState IO) ks AlwaysCollide vs Int) -> AlwaysCollide -> IO (Maybe Int)+ testFromList :: Proxy ks -> Proxy vs -> [(Int, Int)] -> IO (VH.Dictionary (PrimState IO) ks Int vs Int) @@ -180,6 +191,12 @@ it "when altering is just a result - key updated with result" $ property prop_alterUpdate + it "deleting via alter keeps the other keys of the same bucket" $+ property (prop_alterDeleteCollisions False)++ it "deleting via alterM keeps the other keys of the same bucket" $+ property (prop_alterDeleteCollisions True)+ it "when upserting a new key - key is set to value" $ property prop_upsertInsert it "when upserting an existing key - key updated with result" $ property prop_upsertUpdate@@ -301,6 +318,22 @@ v <- testMember ht x v `shouldBe` False + -- All keys share one bucket chain; deleting one must not unlink the+ -- entries before it in the chain (#43).+ prop_alterDeleteCollisions :: Bool -> [Int] -> NonNegative Int -> Property+ prop_alterDeleteCollisions useM ks (NonNegative i) =+ not (null keys) ==> do+ ht <- testInitCollide (Proxy @ks) (Proxy @vs) 10+ mapM_ (\k -> testInsertCollide ht (AC k) k) keys+ let gone = keys !! (i `mod` length keys)+ if useM+ then testAlterMCollide ht (const (pure Nothing)) (AC gone)+ else testAlterCollide ht (const Nothing) (AC gone)+ found <- mapM (testLookupCollide ht . AC) keys+ found `shouldBe` [if k == gone then Nothing else Just k | k <- keys]+ where+ keys = Set.toList (Set.fromList ks)+ prop_alterUpdate :: HashTableTest ks vs => (Int, Int) -> IO () prop_alterUpdate (x, y) = do ht <- testInit (Proxy @ks) (Proxy @vs) 10@@ -353,6 +386,9 @@ testDelete = VH.delete testInsertCollide = VH.insert testAtCollide = VH.at+ testAlterCollide = VH.alter+ testAlterMCollide = VH.alterM+ testLookupCollide = VH.lookup testLength = VH.length testFromList _ _ = VH.fromList testToList = VH.toList@@ -378,6 +414,9 @@ testDelete = VH.delete testInsertCollide = VH.insert testAtCollide = VH.at+ testAlterCollide = VH.alter+ testAlterMCollide = VH.alterM+ testLookupCollide = VH.lookup testLength = VH.length testFromList _ _ = VH.fromList testToList = VH.toList@@ -403,6 +442,9 @@ testDelete = VH.delete testInsertCollide = VH.insert testAtCollide = VH.at+ testAlterCollide = VH.alter+ testAlterMCollide = VH.alterM+ testLookupCollide = VH.lookup testLength = VH.length testFromList _ _ = VH.fromList testToList = VH.toList@@ -428,6 +470,9 @@ testDelete = VH.delete testInsertCollide = VH.insert testAtCollide = VH.at+ testAlterCollide = VH.alter+ testAlterMCollide = VH.alterM+ testLookupCollide = VH.lookup testLength = VH.length testFromList _ _ = VH.fromList testToList = VH.toList
vector-hashtables.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.0 name: vector-hashtables-version: 0.1.2.1+version: 0.1.2.2 synopsis: Efficient vector-based mutable hashtables implementation. description: This package provides efficient vector-based hashtable implementation similar to .NET Generic Dictionary implementation (at the time of 2015).@@ -18,14 +18,17 @@ changelog.md extra-source-files: gen/GenPrimes.hs tested-with:- GHC == 9.8.1- GHC == 9.6.3- GHC == 9.4.7- GHC == 9.2.8- GHC == 9.0.2- GHC == 8.10.7- GHC == 8.8.4- GHC == 8.6.5+ GHC == 9.14.1+ GHC == 9.12.2+ GHC == 9.10.3+ GHC == 9.8.4+ GHC == 9.6.7+ GHC == 9.4.8+ GHC == 9.2.8+ GHC == 9.0.2+ GHC == 8.10.7+ GHC == 8.8.4+ GHC == 8.6.5 library hs-source-dirs: src@@ -50,8 +53,9 @@ , vector , primitive , criterion+ , deepseq+ , hashable , hashtables- , unordered-containers default-language: Haskell2010 test-suite vector-hashtables-test@@ -71,8 +75,7 @@ -- Additional dependencies build-depends: hspec >= 2.6.0 && < 2.12- , QuickCheck >= 2.12.6.1 && < 2.17- , quickcheck-instances >= 0.3.19 && < 0.4+ , QuickCheck >= 2.12.6.1 && < 2.18 build-tool-depends: hspec-discover:hspec-discover >= 2.6.0 && < 2.12