Ratomic:提供可变数据结构,用于 Ruby 的 Ractors。
Ratomic: Provides Mutable data structures for use with Ruby's Ractors

原始链接: https://github.com/mperham/ratomic

Ratomic 是一个新的 Ruby 项目,它提供可变的、Ractor 安全的数据结构,使 Ruby 代码能够突破全局虚拟机锁 (GVL) 的限制。目前处于预发布阶段,Ratomic 提供了 `Counter`、`Pool`、`Map`(哈希表)和 `Queue` 等数据结构,每个结构都设计用于在 Ractor 中并发访问。为了性能和安全,它们的 API 故意被限制得很简洁。 `Counter` 提供原子递增/递减操作。`Pool` 管理一个用于重用的对象池。`Map` 提供一个 Ractor 安全的哈希表,具有 `fetch_and_modify` 等方法。`Queue` 实现了一个多生产者、多消费者的队列。 这个项目建立在 Ilya Bylich 关于无锁数据结构和 Ractor 的研究之上。欢迎贡献,特别是那些有 Rust 和/或 Ruby C 扩展经验的人。这将是一个学习和贡献的机会,可以帮助推进 Ruby 的并发能力。

Mike Perham 的 "Ratomic" 库,为 Ruby 的 Ractor 提供了可变数据结构,正在 Hacker News 上引起关注。一位评论者重点提到了一篇 Jean撰写的相关文章(byroot.github.io),该文章讨论了共享数据结构(如队列和并发映射)对于使 Ractor 能够用于完整应用程序的必要性,并提到了 C 扩展能够使其类型可移动的重要性。Atomics 包含 Ractor 安全版本的队列和映射。另一位评论者表达了兴奋之情,并指出他们刚刚阅读了激发该库灵感的文章,并希望有这样的资源,强调了 Atomics 的及时性和价值。这些讨论表明,Ruby 社区对 Ractor 的兴趣和开发正在日益增长。

原文

Ratomic provides mutable data structures for use with Ruby's Ractors. This allows Ruby code to scale beyond the infamous GVL.

If you know Rust and Ruby C-extensions, we need your help! This project is brand new and could use your knowledge! If you don't know Rust or C, consider this a challenge to learn and solve. Read through the issues to find work that sounds interesting to you.

Install the gem and add to the application's Gemfile by executing:

TODO: We have not released a gem yet.

Ratomic provides several useful Ractor-safe structures. Note the APIs available are frequently very limited compared to Ruby's broad API.

These structures are designed for use as class-level constants so they can be shared by numerous Ractors.

c = Ratomic::Counter.new
c.read # => 0
c.inc
c.inc(5)
c.dec(1)
c.dec
c.read # => 4

A Ractor-safe object pool:

POOL = Ratomic::Pool.new(5, 1.0) { Object.new }
POOL.with do |obj|
  # do something with obj
end

A Ractor-safe map/hash structure:

HASH = Ratomic::Map.new
HASH["mike"] = 123
HASH["mike"] # => 123
HASH.fetch_and_modify(key) {|value| v + 1 }
HASH.clear

A multi-producer, multi-consumer queue.

q = Ratomic::Queue.new
q.push(Object.new)
q.pop # => <Object>

Ilya Bylich wrote and documented his original research at Ruby, Ractors, and Lock-free Data Structures/. Thank you for your impressive work, Ilya!

This repo is further research into the usability and limitations of Ractor-friendly structures in Ruby code and gems.

MIT License.

联系我们 contact @ memedata.com