lvish-2.0: Parallel scheduler, LVar data structures, and infrastructure to build more.

Safe HaskellTrustworthy

Data.LVar.SLMap

Contents

Description

This module provides finite maps that only grow. It is based on a concurrent skip list implementation of maps.

This module is usually a more efficient alternative to PureMap, and provides almost the same interface. However, it's always good to test multiple data structures if you have a performance-critical use case.

Synopsis

The type and its basic operations

data IMap k s v

The map datatype itself. Like all other LVars, it has an s parameter (think STRef) in addition to the a parameter that describes the type of elements in the set.

Performance note: this data structure reduces contention between parallel computations inserting into the map, but all blocking computations are not as scalable. All continuations waiting for not-yet-present elements will currently share a single queue [2013.09.26].

Instances

LVarData1 (IMap k)

An IMap can be treated as a generic container LVar. However, the polymorphic operations are less useful than the monomorphic ones exposed by this module.

OrderedLVarData1 (IMap k)

The IMaps in this module also have the special property that they support an O(1) freeze operation which immediately yields a Foldable container (snapFreeze).

Foldable (IMap k Trvrsbl) 
Foldable (IMap k Frzn) 
Eq (IMap k s v)

Equality is physical equality, as with IORefs.

(Show k, Show a) => Show (IMap k Trvrsbl a)

For convenience only; the user could define this.

(Show k, Show a) => Show (IMap k Frzn a) 
Generator (IMap k Frzn a) 
Show k => ParFoldable (IMap k Frzn a) 
DeepFrz a => DeepFrz (IMap k s a) 

newEmptyMap :: Ord k => Par e s (IMap k s v)

Create a fresh map with nothing in it.

newMap :: Ord k => Map k v -> Par e s (IMap k s v)

Create a new map populated with initial elements.

newFromList :: (Ord k, Eq v) => [(k, v)] -> Par e s (IMap k s v)

Create a new map drawing initial elements from an existing list.

insert :: (Ord k, Eq v, HasPut e) => k -> v -> IMap k s v -> Par e s ()

Put a single entry into the map. (WHNF) Strict in the key and value.

getKey :: (HasGet e, Ord k) => k -> IMap k s v -> Par e s v

Wait for the map to contain a specified key, and return the associated value.

waitSize :: HasGet e => Int -> IMap k s v -> Par e s ()

Wait on the SIZE of the map, not its contents.

waitValue :: (HasGet e, Ord k, Eq v) => v -> IMap k s v -> Par e s ()

Wait until the map contains a certain value (on any key).

modify

Arguments

:: forall f a b e s key . (Ord key, LVarData1 f, Show key, Ord a, HasPut e) 
=> IMap key s (f s a) 
-> key

The key to lookup.

-> Par e s (f s a)

Create a new "bottom" element whenever an entry is not present.

-> (f s a -> Par e s b)

The computation to apply on the right-hand side of the keyed entry.

-> Par e s b 

IMaps containing other LVars have some additional capabilities compared to those containing regular Haskell data. In particular, it is possible to modify existing entries (monotonically). Further, this modify function implicitly inserts a "bottom" element if there is no existing entry for the key.

Generic routines and convenient aliases

gmodify

Arguments

:: forall f a b e s key . (Ord key, LVarData1 f, LVarWBottom f, LVContents f a, Show key, Ord a, HasPut e) 
=> IMap key s (f s a) 
-> key

The key to lookup.

-> (f s a -> Par e s b)

The computation to apply on the right-hand side of the keyed entry.

-> Par e s b 

A generic version of modify that does not require a newBottom argument, rather, it uses the generic version of that function.

getOrInit :: forall f a b e s key. (Ord key, LVarData1 f, LVarWBottom f, LVContents f a, Show key, Ord a, HasPut e) => key -> IMap key s (f s a) -> Par e s (f s a)

Return the preexisting value for a key if it exists, and otherwise return

This is a convenience routine that can easily be defined in terms of gmodify

Quasi-deterministic operations

freezeMap :: (HasFreeze e, Ord k) => IMap k s v -> Par e s (IMap k Frzn v)

Get the exact contents of the map. As with any quasi-deterministic operation, using freezeMap may cause your program to exhibit a limited form of nondeterminism: it will never return the wrong answer, but it may include synchronization bugs that can (nondeterministically) cause exceptions.

This is an O(1) operation that doesn't copy the in-memory representation of the IMap.

fromIMap :: IMap k Frzn a -> Map k a

O(N): Convert from an IMap to a plain Map. This is only permitted when the IMap has already been frozen. This is useful for processing the result of runParThenFreeze, using standard library functions.

traverseFrzn_ :: Ord k => (k -> a -> Par e s ()) -> IMap k Frzn a -> Par e s ()

Traverse a frozen map for side effect. This is useful (in comparison with more generic operations) because the function passed in may see the key as well as the value.

Iteration and callbacks

forEach :: IMap k s v -> (k -> v -> Par e s ()) -> Par e s ()

Add an (asynchronous) callback that listens for all new new key/value pairs added to the map.

forEachHP

Arguments

:: Maybe HandlerPool

optional pool to enroll in

-> IMap k s v

Map to listen to

-> (k -> v -> Par e s ())

callback

-> Par e s () 

Add an (asynchronous) callback that listens for all new key/value pairs added to the map, optionally tied to a handler pool.

withCallbacksThenFreeze :: forall k v b s e. (HasPut e, HasGet e, HasFreeze e, Eq b) => IMap k s v -> (k -> v -> Par e s ()) -> Par e s b -> Par e s b

Register a per-element callback, then run an action in this context, and freeze when all (recursive) invocations of the callback are complete. Returns the final value of the provided action.

Higher-level derived operations

copy :: (Ord k, Eq v, HasPut e) => IMap k s v -> Par e s (IMap k s v)

Return a fresh map which will contain strictly more elements than the input. That is, things put in the former go in the latter, but not vice versa.

traverseMap :: (Ord k, Eq b, HasPut e) => (k -> a -> Par e s b) -> IMap k s a -> Par e s (IMap k s b)

Establish a monotonic map between the input and output map Produce a new result based on each element, while leaving the keys the same.

traverseMap_ :: (Ord k, Eq b, HasPut e) => (k -> a -> Par e s b) -> IMap k s a -> IMap k s b -> Par e s ()

An imperative-style, in-place version of traverseMap that takes the output map as an argument.

Alternate versions of derived ops that expose HandlerPools they create

traverseMapHP :: (Ord k, Eq b, HasPut e) => Maybe HandlerPool -> (k -> a -> Par e s b) -> IMap k s a -> Par e s (IMap k s b)

Variant of traverseMap that optionally ties the handlers to a pool.

traverseMapHP_ :: (Ord k, Eq b, HasPut e) => Maybe HandlerPool -> (k -> a -> Par e s b) -> IMap k s a -> IMap k s b -> Par e s ()

Variant of traverseMap_ that optionally ties the handlers to a pool.

unionHP :: (Ord k, Eq a, HasPut e) => Maybe HandlerPool -> IMap k s a -> IMap k s a -> Par e s (IMap k s a)

Return a new map which will (ultimately) contain everything in either input map. Conflicting entries will result in a multiple put exception. Optionally ties the handlers to a pool.

Debugging Helpers

levelCounts :: IMap k s a -> IO [Int]