#map #lock-free #multi-value

evmap

A lock-free, eventually consistent, concurrent multi-value map

56 releases (34 stable)

11.0.0-alpha.7 Dec 21, 2020
11.0.0-alpha.2 Nov 29, 2020
11.0.0-alpha.1 Jun 3, 2020
10.0.2 May 11, 2020
0.4.0 Mar 31, 2017

#47 in Concurrency

Download history 3126/week @ 2021-02-26 3467/week @ 2021-03-05 3055/week @ 2021-03-12 3669/week @ 2021-03-19 3516/week @ 2021-03-26 2207/week @ 2021-04-02 2032/week @ 2021-04-09 1740/week @ 2021-04-16 1453/week @ 2021-04-23 1870/week @ 2021-04-30 2181/week @ 2021-05-07 1524/week @ 2021-05-14 1668/week @ 2021-05-21 1202/week @ 2021-05-28 1075/week @ 2021-06-04 1183/week @ 2021-06-11

10,979 downloads per month
Used in 17 crates (12 directly)

MIT/Apache

87KB
1.5K SLoC

Build Status Codecov Crates.io Documentation

A lock-free, eventually consistent, concurrent multi-value map.

This map implementation allows reads and writes to execute entirely in parallel, with no implicit synchronization overhead. Reads never take locks on their critical path, and neither do writes assuming there is a single writer (multi-writer is possible using a Mutex), which significantly improves performance under contention. It is backed by the concurrency primitive left-right.

The trade-off exposed by this module is one of eventual consistency: writes are not visible to readers except following explicit synchronization. Specifically, readers only see the operations that preceeded the last call to WriteHandle::flush by a writer. This lets writers decide how stale they are willing to let reads get. They can refresh the map after every write to emulate a regular concurrent HashMap, or they can refresh only occasionally to reduce the synchronization overhead at the cost of stale reads.

For read-heavy workloads, the scheme used by this module is particularly useful. Writers can afford to refresh after every write, which provides up-to-date reads, and readers remain fast as they do not need to ever take locks.

The map is multi-value, meaning that every key maps to a collection of values. This introduces some memory cost by adding a layer of indirection through a Vec for each value, but enables more advanced use. This choice was made as it would not be possible to emulate such functionality on top of the semantics of this map (think about it -- what would the operational log contain?).

To facilitate more advanced use-cases, each of the two maps also carry some customizeable meta-information. The writers may update this at will, and when a refresh happens, the current meta will also be made visible to readers. This could be useful, for example, to indicate what time the refresh happened.

Performance

These benchmarks are outdated at this point, but communicate the right point. Hopefully I'll have a chance to update them again some time soon.

I've run some benchmarks of evmap against a standard Rust HashMap protected by a reader-writer lock, as well as against chashmap — a crate which provides "concurrent hash maps, based on bucket-level multi-reader locks". The benchmarks were run using the binary in benchmark/ on a 40-core machine with Intel(R) Xeon(R) CPU E5-2660 v3 @ 2.60GHz CPUs.

The benchmark runs a number of reader and writer threads in tight loops, each of which does a read or write to a random key in the map respectively. Results for both uniform and skewed distributions are provided below. The benchmark measures the average number of reads and writes per second as the number of readers and writers increases.

Preliminary results show that evmap performs well under contention, especially on the read side. This benchmark represents the worst-case usage of evmap in which every write also does a refresh. If the map is refreshed less often, performance increases (see bottom plot).

Read throughput Write throughput Write throughput

Dependencies

~135–280KB