mvcc_cell code review

mvcc_cell code review

(Jin Qing's Column, Aug., 2024)

mvcc_cell: Software-transactional memory for Rust

Downloads: 1,475

Transactions are fully isolated and serialized:

  • Each transaction sees a fixed snapshot
  • Any concurrent commits will be prevented

Example:

rust 复制代码
let mvcc = Mvcc::new();

// Create a transactional slot
let slot = MvccCell::new(&mvcc, Box::new(0));

// Start concurrent transactions
let mut t1 = mvcc.begin();
let mut t2 = mvcc.begin();

// Uncommitted values are not visible outside the transaction
t1[&slot] = 42;
assert_eq!(t2[&slot], 0);

// First committer wins, regardless of actual modification order
t2[&slot] = 7;
assert!(t2.try_commit().is_ok());
assert!(t1.try_commit().is_err());

// Transactions always read the values that were current when
// begin() was called.
assert_eq!(mvcc.begin()[&slot], 7);

Features

  • The minimal implementation of MVCC
  • Uses 2 transaction IDs: begin ID and commit ID
    • 2 transactions can have the same begin ID
    • Commit will increase the global transaction ID
      • TxnId only increased on commit
    • Any overlap of 2 transactions is regarded as confliction
  • MvccCell stores a value with its history and pending writings
  • Snapshot isolation: reading and writing are within the snapshot
  • Non-blocking: no lock on reading and writing
  • Transaction
    • The ID of a transaction is private
    • Transaction is not Send nor Sync
  • Not two-phase commit: try_commit() will commit
  • Writing implies reading
    • Low Performance: any concurrent read or write to the same slot will fail
  • Consistency: Serializable
  • Vacuum can cleanup the history
相关推荐
superman超哥24 分钟前
Rust 日志级别与结构化日志:生产级可观测性实践
开发语言·后端·rust·可观测性·rust日志级别·rust结构化日志
superman超哥1 小时前
Rust 减少内存分配策略:性能优化的内存管理艺术
开发语言·后端·性能优化·rust·内存管理·内存分配策略
superman超哥2 小时前
Rust 并发性能调优:线程、异步与无锁的深度优化
开发语言·后端·rust·线程·异步·无锁·rust并发性能
superman超哥2 小时前
Rust Trait 对象与动态分发权衡:性能与灵活性的深度权衡
开发语言·后端·rust·rust trait·对象与动态发布·性能与灵活性
ftpeak2 小时前
Burn:纯 Rust 小 AI 引擎的嵌入式物体识别之旅(一步不踩坑)
开发语言·人工智能·rust
superman超哥2 小时前
Rust 异步性能最佳实践:高并发场景的极致优化
开发语言·后端·rust·最佳实践·异步性能·高并发场景
怪我冷i4 小时前
Zed编辑器快速入门
vscode·rust·编辑器·zed
wadesir14 小时前
掌握Rust并发数据结构(从零开始构建线程安全的多线程应用)
数据结构·安全·rust
superman超哥1 天前
Rust 发布 Crate 到 Crates.io:从本地到生态的完整旅程
开发语言·后端·rust·crate·crates.io
浪客川1 天前
【百例RUST - 002】流程控制 基础语法练习题
开发语言·rust