syncmap is a fast, concurrent cache library built with a focus on performance and correctness.

Related tags

Utilities syncmap
Overview

syncmap

syncmap

syncmap is a fast, concurrent cache library

syncmap is a fast, concurrent cache library built with a focus on performance and correctness.

The motivation to build syncmap comes from the sync.Map in Golang.

Summary

Map is like a Hashmap but is safe for concurrent use by multiple thread without additional locking or coordination. Loads, stores, and deletes run in amortized constant time.

The Map type is specialized. Most code should use a plain Rust HashMap instead, with separate locking or coordination, for better type safety and to make it easier to maintain other invariants along with the map content.

The Map type is optimized for two common use cases: (1) when the entry for a given key is only ever written once but read many times, as in caches that only grow, or (2) when multiple thread read, write, and overwrite entries for disjoint sets of keys. In these two cases, use of a Map may significantly reduce lock contention compared to a Rust HashMap paired with a separate Mutex or RWMutex.

The zero Map is empty and ready for use. A Map must not be copied after first use.

In the terminology of the Go memory model, Map arranges that a write operation “synchronizes before” any read operation that observes the effect of the write, where read and write operations are defined as follows. Load, LoadAndDelete, LoadOrStore are read operations; Delete, LoadAndDelete, and Store are write operations; and LoadOrStore is a write operation when it returns loaded set to false.

Status

syncmap is usable but still under active development. We expect it to be production ready in the near future.

Usage

Example

// concurrent exmaple
 use syncmap::map::Map;
fn main() {
 let map = Arc::new(Map::<usize, usize>::new());

 let map1 = map.clone();
 let t1 = std::thread::spawn(move || {
  for i in 0..5000 {
   map1.insert(i, 0, &map1.guard());
  }
 });
 let map2 = map.clone();
 let t2 = std::thread::spawn(move || {
  for i in 0..5000 {
   map2.insert(i, 1, &map2.guard());
  }
 });

 t1.join().unwrap();
 t2.join().unwrap();

 thread::sleep(Duration::from_micros(1000));
 let mut missed = 0;
 let guard = map.guard();
 for i in 0..5000 {
  let v = map.get(&i, &guard);
  if v.is_some() {
   assert!(v == Some(&0) || v == Some(&1));
  } else {
   missed += 1;
  }

  // let kv = map.get_key_value(&i, &guard).unwrap();
  // assert!(kv == (&i, &0) || kv == (&i, &1));
 }
 println!("missed {}", missed)
}
 use syncmap::map::Map;
fn main() {
  let cache = Map::new();

  let guard = cache.guard();
  cache.insert("key", "value1",  &guard);
 
  thread::sleep(Duration::from_millis(50));

  assert_eq!(cache.get(&"key", &guard), Some("value1"));

}
You might also like...
High-performance, Reliable ChatGLM SDK natural language processing in Rust-Lang

RustGLM for ChatGLM Rust SDK - 中文文档 High-performance, high-quality Experience and Reliable ChatGLM SDK natural language processing in Rust-Language 1.

Let Tauri's transparent background rendering window be stacked on Bevy's rendering window in real time to run the game with native-level performance!

Native Bevy with Tauri HUD DEMO 将 Tauri 的透明背景渲染窗口实时叠在 Bevy 的渲染窗口上,以使用原生级别性能运行游戏! Let Tauri's transparent background rendering window be stacked on Bev

Repo for Monaco, a DCA engine for Solana. Built on Solend and lending protocols (Jet, Solend, Port, etc...)
Repo for Monaco, a DCA engine for Solana. Built on Solend and lending protocols (Jet, Solend, Port, etc...)

Monaco Monaco is a DCA protocol for solana built on top of Serum and compatible with any program that implements or extends the instruction interface

Keyscope is a key and secret workflow (validation, invalidation, etc.) tool built in Rust
Keyscope is a key and secret workflow (validation, invalidation, etc.) tool built in Rust

✅ Automate your key and secret validation workflows 🤠 Over 30 different providers 🤖 Export to JSON, audit via CSV 🔑 Keyscope Keyscope is a key and

SimpleX Chat GUI built with Rust, Tauri and Yew
SimpleX Chat GUI built with Rust, Tauri and Yew

simplex-desktop A desktop application for simplex-chat. WIP, contributions are welcome. Architecture For the back end we rust with tauri and frontend

🏪 Modern emoji picker popup for desktop, based on Emoji Mart, built with Tauri and Svelte
🏪 Modern emoji picker popup for desktop, based on Emoji Mart, built with Tauri and Svelte

Emoji Mart desktop popup Modern emoji picker popup app for desktop, based on the amazing Emoji Mart web component. 🍾 Built as a popup: quick invocati

Catify, but built in rust
Catify, but built in rust

A simple project to prettify commit messages NOW IN RUST! Commit messages are good. They provide information about tons of things. But far too many co

Hydrogen is the desktop application for Geplauder, built with tauri studio.

Hydrogen Hydrogen is the desktop application for Geplauder, built with tauri studio. For more information on Geplauder, click here. Usage To configure

Cakecutter - a utility tool that quickly sets up a project from a pre-built template
Cakecutter - a utility tool that quickly sets up a project from a pre-built template

Cakecutter Create projects from pre-built cakes (templates)! Supports files, packages, content, running commands and more! Cakecutter is a utility too

Releases(v0.1.3)
Owner
Behrouz R.Farsi
Rust & Golang Developer
Behrouz R.Farsi
Port of the fantastic Iconoir Icon Pack to Rust embedded devices, with a focus on speed, usability, and completeness.

embedded-iconoir - Icons for every device, ever. What is embedded-iconor? embedded-iconoir is a library that allows you to use Iconoir on embedded dev

null 9 Mar 25, 2023
This plugin provides an interface for storing unencrypted values on the application cache folder.

Tauri Plugin Store This plugin provides an interface for storing unencrypted values on the application cache folder. Architecture This repo shape migh

Tauri 128 Jan 1, 2023
Nix binary cache implemented in rust using libnix-store

harmonia Build Whole application nix-shell --run cargo b C Library Wrapper around libnixstore nix-shell --run make Note: The makefile is only to pro

Helsinki Systems 84 Dec 24, 2022
A Diablo II library for core and simple client functionality, written in Rust for performance, safety and re-usability

A Diablo II library for core and simple client functionality, written in Rust for performance, safety and re-usability

null 4 Nov 30, 2022
Pure Rust library for Apache ZooKeeper built on tokio

zookeeper-async Async Zookeeper client written 100% in Rust, based on tokio. This library is intended to be equivalent with the official (low-level) Z

Kamil Rojewski 16 Dec 16, 2022
High-performance QEMU memory and instruction tracing

Cannoli Cannoli is a high-performance tracing engine for qemu-user. It can record a trace of both PCs executed, as well as memory operations. It consi

Margin Research 412 Oct 18, 2023
A high-performance SPSC bounded circular buffer of bytes

Cueue A high performance, single-producer, single-consumer, bounded circular buffer of contiguous elements, that supports lock-free atomic batch opera

Thaler Benedek 38 Dec 28, 2022
Monorep for fnRPC (high performance serverless rpc framework)

fnrpc Monorep for fnRPC (high performance serverless rpc framework) cli Cli tool help build and manage functions Create RPC functions Create & Manage

Faasly 3 Dec 21, 2022
High-performance BitTorrent tracker compatible with UNIT3D tracker software

UNIT3D-Announce High-performance backend BitTorrent tracker compatible with UNIT3D tracker software. Usage # Clone this repository $ git clone https:/

HDInnovations 4 Feb 6, 2023
A high-performance Lambda authorizer for API Gateway that can validate OIDC tokens

oidc-authorizer A high-performance token-based API Gateway authorizer Lambda that can validate OIDC-issued JWT tokens. ?? Use case This project provid

Luciano Mammino 4 Oct 30, 2023