[][src]Crate sharded

Note: This crate is still in early development and undergoing API changes. Contributions, feature requests, and constructive feedback are warmly welcomed.

sharded   Build Crate

Sharded provides safe, fast, and obvious concurrent collections in Rust. This crate splits the underlying collection into N shards each with its own lock. Calling read(key) or write(key) returns a guard for a single shard.

Features

  • Zero unsafe code. This library uses #![forbid(unsafe_code)]. There are some limitations with the raw locking API that could cause you to write a bug, but it should be hard to so!

  • Zero dependencies. By default, the library only uses std. If you'd like to pull in some community crates such as parking_lot, just use the 3rd-party feature.

  • Tiny footprint. The core logic is ~100 lines of code. This may build up over time as utilities and ergonomics are added.

  • Extremely fast. This implementation may be a more performant choice for your workload than some of the most popular concurrent hashmaps out there. ??

  • Flexible API.. Bring your own lock or collection types. sharded::Map is just a type alias for Shard<Lock<Collection<_>>>. There's support for Sets and Trees, too!

See Also

  • flurry - A port of Java's java.util.concurrent.ConcurrentHashMap to Rust. (Also part of a live stream series)
  • dashmap - Blazing fast concurrent HashMap for Rust.
  • countrie - A concurrent hash-trie map & set.

Quick Start

[dependencies]

# Optionally use `parking_lot`, `hashbrown`, and `ahash`
# by specifing the feature "3rd-party"
sharded = { version = "0.0.1", features = ["3rd-party"] }

Examples

Use a concurrent HashMap

This example is not tested
use sharded::Map;
let concurrent = Map::new()

// or use an existing HashMap,

let users = Shard::from(users);

let guard = users.write(32);
guard.insert(32, user);

Acknowledgements

Many thanks to

  • Reddit community for a few pointers and some motivation to take this project further.

  • Jon Gjengset for the live streams and utility crates involved

  • and countless OSS contributors that made this work possible

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in sharded by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Structs

Shard

The sharded lock collection. This is the main data type in the crate. See also the type aliases Map, Set, and so on.

Traits

Collection

Basic methods needing implemented for shard construction

ExtractShardKey

Teases out the sharding key for example from an IntoIterator value.

Lock

Generic locking implementation.

ShardLock

Type Definitions

Map

Sharded lock-based concurrent map using the crate default lock and map implementations.

RandomState
RwLock
Set

Sharded lock-based concurrent set using the crate default lock and set implementations.