A library for lock-free shared 64-bit dictionaries
Project description
Atomic-Dict: A library for lock-free shared 64-bit dictionaries
Atomic-Dict provides a shared Map[Int, Int] for 64-bit integer values. Create an AtomicDict(), fork, then update keyed values using atomic operations. Supported operations include compare-and-swap, exchange, increment, etc.
Limitations
This is not a general purpose dictionary implementation. It is designed to be used as a synchronization primitive. Key limitations include:
- The key must be a non-zero 64-bit value.
- The value for freshly allocated keys are always initialized as 0.
- Keys allocated into the dictionary can never be removed.
- The maximum size of the dictionary must be specified upfront.
To support more complex types, build shared a list[XYZ] before fork(), then use indexes into those lists as the keys and values of the AtomicDict.
Performance
While bare bones, AtomicDict is fast. Operations never lock, never wait, and leverage cache locality. It is hard to imagine a faster shared dictionary implementation.
Example use
from atomic_dict import AtomicDict
import multiprocessing
if __name__ == "__main__":
dict = AtomicDict(1024*1024)
context = multiprocessing.get_context("fork")
def worker(id: int) -> None:
for _ in range(32*1024):
idx = dict[1].add(1) # also: sub, bor, bxor, band, swap, cas(expected, replacement)
dict[100 + idx] = id
with context.Pool(8) as p:
p.map(worker, range(16))
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for atomic_dict-0.5.0-cp310-cp310-manylinux_2_28_x86_64.whl
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 | 3190da3aeb4e689729f05256b7eb21b69186a462e9af7dd5a1e1e9fbaf2264dd |
|
| MD5 | 74ecbadb93b4ed2ef2e76cecb9642bc4 |
|
| BLAKE2b-256 | 358d4491515cc120c48d8999e9a83bf65800479cf9a23a7561dc95b65d4170ba |