concurrent-hashtable-0.1.8: Thread-safe hash tables for multi-cores!

Copyright(c) Peter Robinson
LicenseBSD3 (see the file LICENSE)
MaintainerPeter Robinson <pwr@lowerbound.io>
Stabilityprovisional
Portabilitynon-portable (requires concurrency, stm)
Safe HaskellNone
LanguageHaskell2010

Data.HashTable

Contents

Description

You can find benchmarks and more information about the internals of this package here: https://lowerbound.io/blog/2019-10-24_concurrent_hash_table_performance.html

Usage Example:

> ht <- newWithDefaults 4     -- creates hash table of initial size 4
> insert ht 1 "hello"         -- adds key-value pair (1,"hello")
> insert ht 2 "world"         -- adds key-value pair (2,"world")
> atomically $ readAssocs ht  -- convert to a key-value list
 [(1,"hello"),(2,"world")]
> readSizeIO ht               -- returns 4
> insert ht 3 "!"             -- adds key-value pair (3,"!") and triggers a resize since load/size is ≥ 0.75
> readSizeIO ht               -- returns 8
> atomically $ readAssocs ht  -- convert to a key-value list
 [(1,"hello"),(3,"!"),(2,"world")]
Synopsis

Data Type

data HashTable k v Source #

A thread-safe hash table that supports dynamic resizing.

data Chain k v Source #

Used for chain-hashing.

Instances
Eq (Chain k v) Source # 
Instance details

Defined in Data.HashTable.Internal

Methods

(==) :: Chain k v -> Chain k v -> Bool #

(/=) :: Chain k v -> Chain k v -> Bool #

Construction

new Source #

Arguments

:: Eq k 
=> Int

Initial size of the hash table

-> Config k 
-> IO (HashTable k v) 

Creates a new hash table with an initial size. See newWithDefaults for more details.

newWithDefaults Source #

Arguments

:: (Eq k, Hashable k) 
=> Int

Initial size of the hash table

-> IO (HashTable k v) 

Creates a new hash table with the given initial vector size, scale factor 2.0, a resizing load threshold of 0.75, and we use as many threads for resizing as we have cores available. This will use a hash function with a (single) random salt. For security sensitive applications, you MUST supply your own hash function. (To be replaced by universal hashing in future versions.)

mkDefaultConfig :: Hashable k => IO (Config k) Source #

Default configuration: scale factor = 2.0; resizing threshold = 0.75; number of worker threads for resizing = getNumCapabilities; hash function = use hashWithSalt with a random salt.

data Config k Source #

Configuration options that may affect the performance of the hash table

Constructors

Config 

Fields

Instances
Show (Config k) Source # 
Instance details

Defined in Data.HashTable.Internal

Methods

showsPrec :: Int -> Config k -> ShowS #

show :: Config k -> String #

showList :: [Config k] -> ShowS #

Atomic Read-Operations

lookup Source #

Arguments

:: Eq k 
=> HashTable k v 
-> k

key k

-> IO (Maybe v)

value for key k if it exists

Lookup the value for the given key in the hash table.

readAssocs :: Eq k => HashTable k v -> STM [(k, v)] Source #

Returns an atomic snapshot of the hash table as a list of key-value pairs. If there is a lot of contention going on, this may be very inefficient.

Non-Atomic Read-Operations

readAssocsIO :: Eq k => HashTable k v -> IO [(k, v)] Source #

Returns the content of the hash table as a list of key-value pairs. This is *not* an atomic operation! If you need atomicity, use readAssoc instead.

Atomic Write-Operations

insert Source #

Arguments

:: Eq k 
=> HashTable k v 
-> k

key k

-> v

value v

-> IO Bool

returns True if and only if the size of the hash table has changed

Inserts a key-value pair k,v into the hash table. Uses chain hashing to resolve collisions. If you want to update the entry only if it already exists, use update. If you want to update the entry only if it does *not* exist, use add.

add Source #

Arguments

:: Eq k 
=> HashTable k v 
-> k

key

-> v

value

-> IO Bool

returns True if and only if the key was not yet in the table

Adds a key and value pair into the hash table only if the key does not exist yet. Returns True if the insertion was successful, i.e., the hash table does not yet contain this key. To get the same behaviour as insert, use insert. If you want to update an already-existing item, use update.

update Source #

Arguments

:: Eq k 
=> HashTable k v 
-> k

key

-> v

value

-> IO Bool

returns True if and only if the key was found

Updates the value for key k. If k is not in the hash table, it skips the update and returns False.

modify Source #

Arguments

:: Eq k 
=> HashTable k v 
-> k

key k

-> (v -> v)

update-function

-> IO (Maybe v)

returns the old value for key k if it existed

Applies an update-function to the value for key k. Returns the old value if it exists. If k is not in the hash table, it returns Nothing.

delete Source #

Arguments

:: Eq k 
=> HashTable k v 
-> k

key of entry that will be deleted

-> IO Bool

returns True if and only if the size of the hash table has changed, i.e., an item was deleted

Deletes the entry for the given key from the hash table. Returns True if and only if an entry was deleted from the table.

swapValues Source #

Arguments

:: Eq k 
=> HashTable k v 
-> k

key k

-> v

new value

-> IO v

old value

Atomically replaces the value for the given key k in the hash table with the new value. Returns the old value. Throws AssertionFailed if k is not in the hash table.

Utilities

readSizeIO :: HashTable k v -> IO Int Source #

Returns the size of the vector representing the hash table.

readSize :: HashTable k v -> STM Int Source #

Returns the size of the vector representing the hash table.

readLoad :: HashTable k v -> IO Int Source #

The load (i.e. number of stored items) in the table. Note that this is not synchronized for performance reasons and hence might be somewhat out of date if a lot of contention is happening.

resize :: Eq k => HashTable k v -> IO () Source #

Increases the size of the hash table by scaling the current size it according to the _scaleFactor in the configuration.