#map #lock-free #read-write #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

#602 in Concurrency

Download history 1350/week @ 2024-07-21 1438/week @ 2024-07-28 2875/week @ 2024-08-04 1597/week @ 2024-08-11 1472/week @ 2024-08-18 1621/week @ 2024-08-25 1587/week @ 2024-09-01 1547/week @ 2024-09-08 1177/week @ 2024-09-15 2074/week @ 2024-09-22 1010/week @ 2024-09-29 1931/week @ 2024-10-06 1679/week @ 2024-10-13 1510/week @ 2024-10-20 1177/week @ 2024-10-27 1731/week @ 2024-11-03

6,214 downloads per month
Used in 22 crates (16 directly)

MIT/Apache

89KB
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

~0.2–27MB
~344K SLoC