Struct lockfree::map::Map

source ·
pub struct Map<K, V, H = RandomState> { /* private fields */ }
Expand description

A lock-free map. Implemented using multi-level hash-tables (in a tree fashion) with ordered buckets.

Design

In order to implement this map, we shall fix a constant named BITS, which should be smaller than the number of bits in the hash (and not 0). We chose 8 for it. Now, we define a table structure: an array of nodes with length 1 << BITS (256 in this case).

For inserting, we take the first BITS bits of the hash. Now, we verify the node. If it is empty, insert a new bucket with our entry (a leaf of the tree), and assign our hash to the bucket. If there is a branch (i.e. a sub-table), we shift the hash BITS bits to the left, but we also keep the original hash for consultation. Then we try again in the sub-table. If there is another leaf, and if the hash of the leaf’s bucket is equal to ours, we insert our entry into the bucket. If the hashes are not equal, we create a sub-table, insert the old leaf into the new sub-table, and insert our pair after.

Entries in a bucket are a single linked list ordered by key. The ordering of the list is because of possible race conditions if e.g. new nodes were always inserted at end. And if a bucket is detected to be empty, the table will be requested to delete the bucket.

For searching, in a similar way, the hash is shifted and sub-tables are entered until either a node is empty or a leaf is found. If the hash of the leaf’s bucket is equal to our hash, we search for the entry into the bucket. Because the bucket is ordered, we may know the entry is not present with ease.

Because of limitation of sharing in concurrent contexts, we do return plain references to the entries, neither allow the user to move out removed values, as they must be deinitialized correctly. Instead, we return guarded references to the entries and wrappers over removed entries.

Implementations

Creates a new Map with the default hasher builder.

Creates the Map using the given shared incinerator.

Creates an iterator over guarded references to the key-value entries.

Creates an iterator over the key-value entries, with a mutable reference to the value.

Tries to optimize space by removing unnecessary tables without removing any entry. This method might also clear delayed resource destruction. This method cannot be performed in a shared context.

Removes all entries. This method might also clear delayed resource destruction. This method cannot be performed in a shared context.

Creates the Map using the given hasher builder.

Creates the Map using the given hasher builder and shared incinerator.

The shared incinerator used by this Map.

The hasher buider used by this Map.

Searches for the entry identified by the given key. The returned value is a guarded reference. Guarded to ensure no thread deallocates the allocation for the entry while it is being used. The method accepts a type resulted from borrowing the stored key. This method will only work correctly if Hash and Ord are implemented in the same way for the borrowed type and the stored type. If the entry was not found, None is returned.

Inserts unconditionally the given key and value. If there was a previously stored value, it is returned.

Inserts interactively the given key. A closure is passed to generate the value part of the entry and validate it with the found value. Even though the closure may have already accepted some condition, it might get recalled many times due to concurrent modifications of the Map.

The first argument passed to the closure is the key passed in first place. The second argument is an optional mutable reference to a previously generated value. Obviously, if no value was ever generated, it is None. The third argument is a reference to the found stored entry. Obviously, if no stored entry was found, it is None. The return value of the closure is a specification of “what to do with the insertion now”.

Reinserts a previously removed entry. The entry must have been either:

  1. Removed from any Map using the same SharedIncin as this Map. 2. Removed from an already dead Map with dead SharedIncin. 3. Removed from a Map whose SharedIncin has no sensitive reads active.

If the removed entry does not fit any category, the insertion will fail. Otherwise, insertion cannot fail.

Reinserts interactively a previously removed entry. A closure will be passed to validate if the conditions are correct for the reinsertion. The first argument passed to the closure is a reference to the removed entry, the second argument is a reference to the stored found entry. Obviously, if no stored entry was found, the argument is None. The returned value is a boolean indicating if the reinsertion should go on. Even though the closure may have already accepted some condition, it might get recalled many times due to concurrent modifications of the Map.

The entry must have been either:

  1. Removed from any Map using the same SharedIncin as this Map. 2. Removed from an already dead Map with dead SharedIncin. 3. Removed from a Map whose SharedIncin has no sensitive reads active.

If the removed entry does not fit any category, the insertion will fail. Otherwise, insertion cannot fail.

Removes unconditionally the entry identified by the given key. If no entry was found, None is returned. This method will only work correctly if Hash and Ord are implemented in the same way for the borrowed type and the stored type. If the entry was not found, None is returned.

Removes interactively the entry identified by the given key. A closure is passed to validate the removal. The only argument passed to the closure is a reference to the found entry. The closure returns if the removal should go on. If no entry was found, None is returned. This method will only work correctly if Hash and Ord are implemented in the same way for the borrowed type and the stored type. If the entry was not found, None is returned.

Acts just like Extend::extend but does not require mutability.

Trait Implementations

Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Executes the destructor for this type. Read more
Extends a collection with the contents of an iterator. Read more
🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Creates a value from an iterator. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.