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超哥2 小时前
Rust 内部可变性模式:突破借用规则的受控机制
开发语言·后端·rust·rust内部可变性·借用规则·受控机制
LYFlied5 小时前
Rust代码打包为WebAssembly二进制文件详解
开发语言·前端·性能优化·rust·wasm·跨端
i建模5 小时前
C++和Rust的性能对比
开发语言·c++·rust
盒马盒马5 小时前
Rust:智能指针 Box & Rc & Cow
开发语言·算法·rust
xuejianxinokok7 小时前
如何在 Rust 中以惯用方式使用全局变量
后端·rust
Pomelo_刘金8 小时前
Rust : Safe and Unsafe
rust
围炉聊科技9 小时前
Vibe Kanban:Rust构建的AI编程代理编排平台
开发语言·rust·ai编程
半夏知半秋11 小时前
rust学习-循环
开发语言·笔记·后端·学习·rust
梦想画家12 小时前
Rust模块化开发从入门到精通:用mod搭建高可维护项目架构
架构·rust·模块化开发
木木木一12 小时前
Rust学习记录--C4 Rust所有权
开发语言·学习·rust