farce 0.2.0-x86-linux-gnu → 0.4.0-x86-linux-gnu

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +91 -0
  3. data/README.md +117 -1451
  4. data/docs/compatibility.md +74 -0
  5. data/docs/concurrency.md +151 -0
  6. data/docs/data-structures.md +313 -0
  7. data/docs/installation.md +63 -0
  8. data/docs/integrations.md +124 -0
  9. data/docs/introduction.md +266 -0
  10. data/docs/modes.md +8 -5
  11. data/docs/scheduling.md +176 -0
  12. data/docs/scopes.md +3 -1
  13. data/docs/sharing.md +211 -0
  14. data/docs/shims.md +144 -0
  15. data/docs/utilities.md +93 -0
  16. data/docs/variants-and-scopes.md +92 -0
  17. data/docs/variants.md +18 -0
  18. data/lib/farce/abstract/executor.rb +130 -0
  19. data/lib/farce/abstract/flag.rb +6 -0
  20. data/lib/farce/abstract/pool.rb +334 -0
  21. data/lib/farce/abstract/scheduler.rb +539 -93
  22. data/lib/farce/backports.rb +274 -0
  23. data/lib/farce/engine/jruby/fiber_scheduler.rb +3 -0
  24. data/lib/farce/engine/jruby/scheduler_synchronization.rb +87 -0
  25. data/lib/farce/engine/jruby/signal.rb +7 -4
  26. data/lib/farce/engine/ruby/3.4/null_port.rb +21 -0
  27. data/lib/farce/engine/ruby/3.4/patch.rb +67 -0
  28. data/lib/farce/engine/ruby/3.4/port.rb +13 -12
  29. data/lib/farce/engine/ruby/3.4/ractor_methods.rb +0 -1
  30. data/lib/farce/engine/ruby/4.0/null_port.rb +36 -0
  31. data/lib/farce/engine/ruby/4.0/port.rb +1 -2
  32. data/lib/farce/engine/ruby/4.1/null_port.rb +5 -0
  33. data/lib/farce/engine/ruby.rb +7 -0
  34. data/lib/farce/engine/shared/main_scheduler.rb +2 -2
  35. data/lib/farce/engine/shared/null_port.rb +18 -0
  36. data/lib/farce/engine/shared/parallel_scheduler.rb +1 -1
  37. data/lib/farce/engine/shared/port.rb +41 -3
  38. data/lib/farce/engine/shared/proxy_owner.rb +1 -1
  39. data/lib/farce/engine/shared/queue.rb +4 -4
  40. data/lib/farce/engine/shared/ractor_methods.rb +2 -2
  41. data/lib/farce/engine/shared/ractor_selector.rb +73 -0
  42. data/lib/farce/engine/shared/unshared_atom.rb +9 -8
  43. data/lib/farce/engine/shared.rb +9 -0
  44. data/lib/farce/extension.rb +150 -0
  45. data/lib/farce/integrations/ractor_tmvar.rb +4 -1
  46. data/lib/farce/internal/fake_ractor.rb +10 -2
  47. data/lib/farce/internal/lease_initialization.rb +1 -1
  48. data/lib/farce/internal/local_executor_methods.rb +33 -0
  49. data/lib/farce/internal/local_scheduled_task.rb +27 -0
  50. data/lib/farce/internal/scheduled_task.rb +1 -1
  51. data/lib/farce/internal/scheduler_lifecycle.rb +7 -4
  52. data/lib/farce/internal/thread_pool_supervisor.rb +45 -0
  53. data/lib/farce/internal/unshared_pool_worker.rb +14 -0
  54. data/lib/farce/internal.rb +14 -0
  55. data/lib/farce/local/pool.rb +111 -0
  56. data/lib/farce/local/scheduler.rb +219 -0
  57. data/lib/farce/null_port.rb +12 -0
  58. data/lib/farce/pool.rb +2 -290
  59. data/lib/farce/proxy.rb +2 -2
  60. data/lib/farce/scheduler.rb +1 -502
  61. data/lib/farce/system.rb +10 -2
  62. data/lib/farce/task.rb +285 -0
  63. data/lib/farce/{thread_scheduler.rb → thread_executor.rb} +7 -7
  64. data/lib/farce/unshared/pool.rb +44 -0
  65. data/lib/farce/unshared/scheduler.rb +89 -0
  66. data/lib/farce/version.rb +1 -1
  67. data/lib/farce.rb +10 -18
  68. metadata +34 -2
data/README.md CHANGED
@@ -1,1581 +1,247 @@
1
1
  # Farce: Fiber and Ractor Compatibility Enabler
2
2
 
3
- Farce provides tools and data structures to write code that works well with both **Fiber schedulers** and **Ractors**, as well as the classic **Threads**. Its main purpose is to be used by other gems to provide better compatibility with these concurrency primitives, but it can also be used directly in applications.
3
+ Farce provides mutable data structures and coordination tools for Ruby code that uses **threads, Ractors, and Fiber schedulers**. Use it to share state between workers, connect background computation to an event loop, or make a gem compatible with the caller's concurrency model.
4
4
 
5
- **Here's a taste:**
5
+ **Here's a taste:** four workers updating the same map, without losing increments or managing a lock.
6
6
 
7
7
  ```ruby
8
- map = Farce::Map.new
9
- output = Farce::Mutable.new "Hello from Farce! The map has 0 entries, with a total sum of 0."
10
-
11
- # Mutate output in another Ractor - this would not work with a String.
12
- # Using Farce::Ractor here instead of Ractor so it also works on JRuby and TruffleRuby.
13
- ractor = Farce::Ractor.new(output) do |output|
14
- sleep rand # I laugh in the face of race conditions. Ha, ha, ha, ha!
15
- output.gsub!("Farce", "✨ Farce ✨")
16
- end
8
+ require "farce"
17
9
 
18
- # No idea if the above ractor is done modifying output, but we don't need to worry!
19
- # We'll just pick a random key one hundred times and increase its counter by one.
20
- # Oh, and update the output string.
21
- 100.times.map do
22
-
23
- # And of course we need to do it all in parallel for maximum performance!
24
- # Let's ignore the fact that this would be much faster if we didn't.
25
- # Starting 100 ractors is the main performance issue here.
26
- # But that wouldn't be an interesting example, right?
27
- Farce::Ractor.new(output, map) do |output, map|
28
- key = %i[foo bar baz].sample
29
-
30
- # Wrap the modifications in a transaction, so map and output never disagree
31
- Farce.transaction(output, map) do |_, output, map|
32
-
33
- # Increase the value we have for the given key by one!
34
- map[key] ||= 0
35
- map[key] += 1
36
-
37
- # Could also replace the lines above with:
38
- #
39
- # map.upsert(key, 1) { it + 1 }
40
- #
41
- # … which would be atomic.
42
- # But we're in a transaction, so everything is atomic! ☢️
43
-
44
- output.sub!(/\d+ entries/, "#{map.size} entries")
45
- output.sub!(/sum of \d+/, "sum of #{map.values.sum}")
10
+ counts = Farce::Map.new
11
+ workers = 4.times.map do
12
+ Farce.in_parallel(counts) do |counts|
13
+ 100.times do
14
+ counts.upsert(:completed, 1) { |count| count + 1 }
46
15
  end
47
16
  end
48
-
49
- end.each(&:join)
50
-
51
- # Let's be sure that first ractor has completed its modification
52
- ractor.join
53
-
54
- # Hello from ✨ Farce ✨! The map has 3 entries with a total sum of 100.
55
- puts output
56
- ```
57
-
58
- 🤨 **You don't see why you'd want this?**<br>
59
- → Start with the [Introduction](#introduction).
60
-
61
- 📖 **Now you want to know what else Farce can do?**<br>
62
- → Check out the [Features](#features)!
63
-
64
- 🔎 **Returning user and you need the details?**<br>
65
- → See the [API reference](https://rkh.github.io/farce/).
66
-
67
- 🛠️ **Want to contribute?**<br>
68
- → Read the [contribution guide](CONTRIBUTING.md), [code of conduct](CODE_OF_CONDUCT.md), and [security policy](SECURITY.md).
69
-
70
-
71
- ## Table of Contents
72
-
73
- - [Farce: Fiber and Ractor Compatibility Enabler](#farce-fiber-and-ractor-compatibility-enabler)
74
- - [Table of Contents](#table-of-contents)
75
- - [Introduction](#introduction)
76
- - [The Problem](#the-problem)
77
- - [The Solution](#the-solution)
78
- - [Features](#features)
79
- - [Data Structures](#data-structures)
80
- - [Maps](#maps)
81
- - [Vectors](#vectors)
82
- - [Sets](#sets)
83
- - [Atoms](#atoms)
84
- - [Counters](#counters)
85
- - [Flags](#flags)
86
- - [References](#references)
87
- - [Molecules](#molecules)
88
- - [Queues](#queues)
89
- - [Priority Queues](#priority-queues)
90
- - [Timer Queues](#timer-queues)
91
- - [Queue capacity](#queue-capacity)
92
- - [Shims, Polyfills, and Extensions](#shims-polyfills-and-extensions)
93
- - [Ractor](#ractor)
94
- - [Port](#port)
95
- - [WeakRef](#weakref)
96
- - [Resolv](#resolv)
97
- - [Sharing Unshareable Data](#sharing-unshareable-data)
98
- - [Sharing Modes](#sharing-modes)
99
- - [Envelopes](#envelopes)
100
- - [Mutables](#mutables)
101
- - [Proxies](#proxies)
102
- - [Mode Managers](#mode-managers)
103
- - [Leases](#leases)
104
- - [Variants and Scopes](#variants-and-scopes)
105
- - [Available Variants](#available-variants)
106
- - [Provided scopes](#provided-scopes)
107
- - [Concurrency](#concurrency)
108
- - [Locks](#locks)
109
- - [Atomic Operations](#atomic-operations)
110
- - [Observability and Signaling](#observability-and-signaling)
111
- - [Transactions](#transactions)
112
- - [Maps and Sets](#maps-and-sets)
113
- - [TVars](#tvars)
114
- - [Scheduling Code](#scheduling-code)
115
- - [`Farce.on_main`](#farceon_main)
116
- - [`Farce.in_parallel`](#farcein_parallel)
117
- - [`Farce.schedule`](#farceschedule)
118
- - [Schedulers](#schedulers)
119
- - [Ractor Pools](#ractor-pools)
120
- - [Third-Party Fiber Schedulers](#third-party-fiber-schedulers)
121
- - [Integrations](#integrations)
122
- - [Active Support](#active-support)
123
- - [Dry Types](#dry-types)
124
- - [JSON, YAML, etc](#json-yaml-etc)
125
- - [Concurrent Ruby](#concurrent-ruby)
126
- - [Ractor Sharing](#ractor-sharing)
127
- - [Additional Integrations](#additional-integrations)
128
- - [Disable Automatic loading](#disable-automatic-loading)
129
- - [Miscellaneous](#miscellaneous)
130
- - [Top Level Methods](#top-level-methods)
131
- - [Additional Classes](#additional-classes)
132
- - [Shareability Mixins](#shareability-mixins)
133
- - [Constants](#constants)
134
- - [Compatibility and Dependencies](#compatibility-and-dependencies)
135
- - [Ruby](#ruby)
136
- - [Optional Dependencies](#optional-dependencies)
137
- - [Similar Projects](#similar-projects)
138
- - [Installation](#installation)
139
- - [Globally](#globally)
140
- - [As a project dependency](#as-a-project-dependency)
141
- - [As a library dependency](#as-a-library-dependency)
142
- - [Local setup](#local-setup)
143
- - [Loading Farce](#loading-farce)
144
- - [Known Issues and Limitations](#known-issues-and-limitations)
145
- - [Possible discrepancy regarding frozen state in Ruby and C](#possible-discrepancy-regarding-frozen-state-in-ruby-and-c)
146
- - [Housekeeping](#housekeeping)
147
-
148
-
149
- ## Introduction
150
-
151
- Ruby 3.0 introduced two powerful concurrency primitives: **Fiber schedulers** and **Ractors**, besides the already existing **Threads**:
152
-
153
- * **Threads** are the classic concurrency primitive in Ruby, and mostly map to native operating system threads. This way any system calls, IO, or other blocking operations will block a thread, allowing another one to run. However, on the official Ruby implementation (often referred to as CRuby or MRI), Threads cannot run in parallel due to the Global VM Lock (GVL). This means that even if you have multiple threads, only one of them can execute Ruby code at a time.
154
- * **Ractors** provide parallelism, even on CRuby. They make it very hard to introduce concurrency issues between them, but put severe limitations on state sharing and cross-ractor communication. Ractors are only supported by CRuby, but that is generally not an issue, as other implementations, such as JRuby and TruffleRuby, support true parallelism with Threads. As of Ruby 4.0, Ractors are still considered experimental.
155
- * **Fiber schedulers** allow you to run multiple Fibers concurrently on a single Thread, without having to explicitly pass control between them. This is a very efficient way to run concurrent code, especially for IO-bound workloads. As of Ruby 4.0, Fiber schedulers with IO support are still considered experimental.
156
-
157
- All three of these need some form of **concurrency coordination** for shared state. These are usually the most complicated for Threads, as state is shared between them freely, and an interrupt can happen at any time. Fibers on the other hand have explicit control transfer (only on blocking operations or when giving up control), so their behavior is much more predictable. Ractors disallow sharing mutable state between them (or have built-in locking for the few cases where they allow it).
158
-
159
- ### The Problem
160
-
161
- > [!NOTE]
162
- > Please keep in mind that many of these libraries may gain better Ractor support in the future, and that the table might already be outdated. Feel free to open an issue if you find any inaccuracies. This is also in no way meant as a criticism of any of the libraries mentioned. They all have their own scopes, goals and priorities, and largely rely on volunteer contributions.
163
-
164
- Both the Ruby core library, and other libraries, like the very popular [concurrent-ruby](https://github.com/ruby-concurrency/concurrent-ruby) gem, provide plenty of primitives to solve this. However, most of them are not compatible with Ractors. And those that are, are then in turn usually not compatible with Fiber schedulers. Due to this, most of the popular frameworks and libraries out there do not support Ractors at all:
165
-
166
- <table>
167
- <thead>
168
- <tr>
169
- <th>Library</th>
170
- <th>Example classes</th>
171
- <th>Multithreading</th>
172
- <th>Non-blocking Fibers</th>
173
- <th>Cross-Ractor Usage</th>
174
- </tr>
175
- </thead>
176
- <tbody>
177
- <tr>
178
- <td rowspan="5"><a href="https://www.ruby-lang.org/">Ruby</a></td>
179
- <td>Array, Hash, String, Set, …</td>
180
- <td>⚠️ not thread-safe</td>
181
- <td>✅ supported</td>
182
- <td>⚠️ immutable/copy/move only</td>
183
- </tr>
184
- <tr>
185
- <td>Thread, Fiber</td>
186
- <td>✅ supported</td>
187
- <td>✅ supported</td>
188
- <td>❌ <b>not supported</b></td>
189
- </tr>
190
- <tr>
191
- <td>Mutex, ConditionVariable, Queue</td>
192
- <td>✅ supported</td>
193
- <td>⚠️ CRuby only</td>
194
- <td>❌ <b>not supported</b></td>
195
- </tr>
196
- <tr>
197
- <td>Ractor, Ractor::Port</td>
198
- <td>✅ supported</td>
199
- <td>❌ <b>not supported</b></td>
200
- <td>✅ supported</td>
201
- </tr>
202
- <tr>
203
- <td>WeakRef, Ruby::Box</td>
204
- <td>⚠️ Main Ractor only</td>
205
- <td>⚠️ Main Ractor only</td>
206
- <td>❌ <b>not supported</b></td>
207
- </tr>
208
- <tr>
209
- <td><a href="https://github.com/ruby-concurrency/concurrent-ruby">concurrent-ruby</a></td>
210
- <td>Future, Exchanger, Tuple, …</td>
211
- <td>⚠️ Main Ractor only</td>
212
- <td>⚠️ Main Ractor only</td>
213
- <td>❌ <b>not supported</b></td>
214
- </tr>
215
- <tr>
216
- <td rowspan="2"><a href="https://github.com/socketry/async">async</a></td>
217
- <td>Promise, Condition, Queue, …</td>
218
- <td>✅ supported</td>
219
- <td>✅ supported</td>
220
- <td>❌ <b>not supported</b></td>
221
- </tr>
222
- <tr>
223
- <td>Scheduler</td>
224
- <td>⚠️ Main Ractor only</td>
225
- <td>⚠️ Main Ractor only</td>
226
- <td>❌ <b>not supported</b></td>
227
- </tr>
228
- <tr>
229
- <td rowspan="3"><a href="https://github.com/mperham/ratomic">ratomic</a></td>
230
- <td>Pool</td>
231
- <td>⚠️ CRuby only<b>¹</b></td>
232
- <td>❌ <b>not supported</b></td>
233
- <td>⚠️ CRuby only<b>¹</b></td>
234
- </tr>
235
- <tr>
236
- <td>LocalPool</td>
237
- <td>⚠️ CRuby only<b>¹</b></td>
238
- <td>⚠️ CRuby only<b>¹</b></td>
239
- <td>⚠️ CRuby only<b>¹</b></td>
240
- </tr>
241
- <tr>
242
- <td>Queue, Map</td>
243
- <td>⚠️ CRuby only<b>¹</b></td>
244
- <td>❌ <b>not supported</b></td>
245
- <td>💣 <b>breaks isolation¹²</b></td>
246
- </tr>
247
- <tr>
248
- <td><a href="https://github.com/MadBomber/ractor_queue">ractor_queue</a></td>
249
- <td>RactorQueue</td>
250
- <td>⚠️ CRuby only<b>¹</b></td>
251
- <td>⚠️ CRuby only<b>¹</b></td>
252
- <td>💣 <b>breaks isolation¹²</b></td>
253
- </tr>
254
- <tr>
255
- <td>
256
- <a href="https://github.com/hamstergem/hamster">hamster</a> /
257
- <a href="https://github.com/immutable-ruby/immutable-ruby">immutable</a>
258
- </td>
259
- <td>Hash, Vector, Set, List, …</td>
260
- <td>⚠️ Main Ractor only</td>
261
- <td>⚠️ Main Ractor only</td>
262
- <td>❌ <b>not supported</b></td>
263
- </tr>
264
- <tr>
265
- <td><a href="https://github.com/ko1/ractor-sharing">ractor-sharing</a></td>
266
- <td>TVar, LockVar, LockHash, …</td>
267
- <td>⚠️ CRuby only<b>¹</b></td>
268
- <td>❌ <b>not supported</b></td>
269
- <td>⚠️ CRuby only<b>¹</b></td>
270
- </tr>
271
- <tr>
272
- <td rowspan="2"><a href="https://github.com/jhawthorn/ractor_safe">ractor_safe</a></td>
273
- <td>HashMap, AtomicInteger</td>
274
- <td>⚠️ CRuby only</td>
275
- <td>⚠️ CRuby only</td>
276
- <td>✅ supported</td>
277
- </tr>
278
- <tr>
279
- <td>Queue</td>
280
- <td>⚠️ CRuby only</td>
281
- <td>❌ <b>not supported</b></td>
282
- <td>✅ supported</td>
283
- </tr>
284
- </tbody>
285
- </table>
286
-
287
- Notes:
288
- 1. Incorrectly flags mutable objects as frozen.
289
- 2. Breaking Ractor isolation introduces concurrency issues not just in application code, but in Ruby itself, possibly leading to segmentation faults or undefined behavior. Note that `ractor_queue` has the ability to enforce shareability, but this feature is opt-in.
290
-
291
- ### The Solution
292
-
293
- **Farce is an attempt to solve this.** Code relying on Farce will work well with any concurrency primitive. Use an **event loop with the [async](https://github.com/socketry/async)** gem? Farce will fit right in. Running a **pool of Ractors** with [Kino](https://github.com/yaroslav/kino)? Farce got you covered! You manually manage a single **background thread**? Farce solves that, too!
294
-
295
- And it does so in a non-invasive way. You should be able to use Farce alongside any other gem!
296
-
297
- ```ruby
298
- # Using a counter as an example, there are many more classes provided by Farce
299
- counter = Farce::Counter.new
300
-
301
- # Works in code without concurrency
302
- counter.add(5)
303
- counter.to_i # => 5
304
-
305
- # Works with threads
306
- wait_for = 5.times.map do
307
- Thread.new { counter.increment }
308
- end
309
-
310
- # Works with ractors
311
- wait_for += 5.times.map do
312
- Ractor.new(counter) { it.increment }
313
- end
314
-
315
- # Works with the async gem
316
- Async do
317
- 5.times do
318
- Async { counter.increment }
319
- end
320
17
  end
18
+ workers.each(&:join)
321
19
 
322
- wait_for.each(&:join)
323
- counter.to_i # => 20
20
+ counts[:completed] # => 400
324
21
  ```
325
22
 
326
- Farce also aims to be fast and efficient, with performance ranging from minimal overhead to outperforming other options.
327
-
328
- It does so by choosing the best implementation for the situation. The counter in the above example will use Java's `AtomicLong` on JRuby and TruffleRuby in GraalVM mode, an `AtomicReference` on TruffleRuby in native mode, and an atomic, native counter on CRuby, so it runs without having to use any locks on any of these platforms.
23
+ [Introduction](#introduction) · [Features](#features) · [Installation](#installation) · [Compatibility](#compatibility-and-dependencies) · [API reference](https://rkh.github.io/farce/)
329
24
 
330
- This is especially useful when coordinating work between Fiber schedulers and Ractors:
25
+ ## Introduction
331
26
 
332
- ```ruby
333
- queue = Farce::Queue.new
27
+ Threads share memory, Ractors enable parallel Ruby execution on CRuby with restricted sharing, and Fiber schedulers handle concurrent IO efficiently. A primitive that works with one may fail with another.
334
28
 
335
- # Background ractor pushing work into the queue
336
- # This would not work with Ruby's built-in Queue
337
- Ractor.new(queue) do |queue|
338
- loop { queue.push expensive_work }
339
- end
29
+ Farce's shared containers preserve Ractor isolation. Their blocking operations suspend non-blocking fibers without stopping the scheduler. You can use them alongside existing gems, without adopting Farce's own schedulers.
340
30
 
341
- # This would not work with Ratomic::Queue
342
- Async do
343
- # Task waiting for work from the queue
344
- Async { loop { do_something queue.pop } }
345
-
346
- # Doesn't get blocked by the other task waiting for the queue
347
- Async { unrelated_work }
348
- end
349
- ```
31
+ → [Why Farce exists, comparisons, and a larger example](docs/introduction.md).
350
32
 
351
33
  ## Features
352
34
 
353
35
  ### Data Structures
354
36
 
355
- Farce provides a range of data structures that are ractor-safe, mutable, and expose high-level concurrency APIs in addition to standard Ruby APIs for the built-in classes it offers replacements for.
37
+ Use familiar Ruby APIs with coordinated access to shared state:
356
38
 
357
- #### Maps
39
+ - **Maps, vectors, and sets** for collections, with sorted and weak-reference variants.
40
+ - **Atoms, counters, flags, and molecules** for individual values and structured records.
41
+ - **FIFO, priority, and timer queues** for passing work, applying back pressure, or delaying delivery.
358
42
 
359
- Maps are Hash-like key-value data structures. They do not preserve insertion order.
360
-
361
- They implement almost all methods Ruby's Hash offers:
362
-
363
- ```ruby
364
- map = Farce::Map.new
365
- map[:foo] = :bar
366
-
367
- map.merge! answer: 42
368
- map.transform_values! { -it.to_s }
369
- map.to_h # => {answer: "42", foo: "bar"}
370
- ```
371
-
372
- In addition, they come with a range of [atomic operations](#atomic-operations), and have built-in key normalization support:
373
-
374
- ```ruby
375
- # normalize_keys can be a symbol, proc, hash, or another map
376
- map = Farce::Map.new({ a: 10 }, normalize_keys: :to_s)
377
-
378
- # Atomic upsert operation
379
- 2.times { map.upsert(:b, 42) { it * map[:a] } }
380
-
381
- # Keys have been converted to strings
382
- map.keys.sort # => ["a", "b"]
383
- ```
384
-
385
- Besides the standard map implementation, Farce includes a range of specialized maps:
386
-
387
- * `LRUMap` will evict the least recently used entry to not grow beyond the maximum size.
388
- * `LFUMap` will evict the least frequently used entry to not grow beyond the maximum size.
389
- * `LeaseMap` manages [leases](#leases) associated with known keys.
390
- * `TreeMap` uses a [red-black tree](https://en.wikipedia.org/wiki/Red%E2%80%93black_tree) instead of a [hash table](https://en.wikipedia.org/wiki/Hash_table) to store its entries, keeping them sorted by the key's value.
391
- * `WeakMap` only holds weak references to its keys and values, automatically removing entries when the corresponding key or value gets garbage collected.
392
- * `WeakKeyMap` only holds weak references to its keys, automatically removing entries when the corresponding key gets garbage collected.
393
- * `WeakValueMap` only holds weak references to its values, automatically removing entries when the corresponding value gets garbage collected.
394
-
395
- For example, bounded maps are useful for implementing caches:
396
-
397
- ```ruby
398
- cache = Farce::LRUMap.new(max_size: 2)
399
-
400
- cache[:first] = 1
401
- cache[:second] = 2
402
- cache[:first] # makes sure :first was accessed more recently than :second
403
- cache[:third] = 3
404
-
405
- cache.key?(:second) # => false
406
- ```
407
-
408
- #### Vectors
409
-
410
- `Farce::Vector` is to `Array` what `Farce::Map` is to `Hash`. It implements the same interface, with additional atomic operations.
43
+ For example, use a Vector as a shared list:
411
44
 
412
45
  ```ruby
413
46
  list = Farce::Vector.new
414
- 10.times { list << it }
415
- list.to_a # => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
416
- list.sum # => 45
417
- ```
418
-
419
- There is one important difference to arrays: Bulk mutations, such as `Vector#map!` are atomic. Either the block succeeds for all entries, or if it raises an exception then none of the changes are applied. Such mutations may also not write to the same vector from within the block.
420
-
421
- ```ruby
422
- list = Farce::Vector[1, 2, 3, 4]
423
-
424
- begin
425
- list.map! do |value|
426
- raise "error" if value == 2
427
- value * 2
428
- end
429
- rescue => e
430
- end
431
-
432
- # This would return 2 if list was an array
433
- list.first # => 1
434
- ```
435
-
436
- #### Sets
437
-
438
- `Farce::Set` is to `Set` what `Farce::Map` is to `Hash`. It implements the same interface, with additional atomic operations.
439
-
440
- ```ruby
441
- jobs = Farce::Set[:compile, :test]
442
- jobs.add? :publish # => jobs
443
- jobs.add? :test # => nil
444
- job.include? :publish # => true
445
- ```
446
-
447
- Besides the standard set implementation, Farce includes two specialized sets:
448
-
449
- * `SortedSet` keeps its entries in order based on value comparison.
450
- * `WeakSet` only holds weak references to its entries.
451
-
452
- #### Atoms
453
-
454
- Atoms are value containers. These store a single reference to any Ruby object, and expose atomic operations for mutating these values:
455
-
456
- ```ruby
457
- atom = Farce::Atom.new("initial value")
458
- atom.value # => "initial value"
459
-
460
- # atoms are ractor-shareable
461
- Ractor.new(atom) do |atom|
462
- atom.update { |current| current + " updated" } # => "initial value updated"
463
- end
464
-
465
- # waits for the other ractor to perform its update
466
- atom.wait_until_changed "initial value"
467
- ```
468
-
469
- `WeakAtom` only keeps a weak reference for its current value, reverting to `nil` when the current value gets garbage collected.
470
-
471
- #### Counters
472
-
473
- Ractor-shareable, atomic counter. This is a stateful, numeric object.
474
-
475
- It is similar to creating an [atom](#atoms) for an integer, but significantly faster, as it will use a truly lock-free implementation on most CPU/Ruby combinations.
476
-
477
- ```ruby
478
- counter = Farce::Counter.new
479
- counter.value # => 0
480
-
481
- # Increase the counter by 1
482
- counter.increment
483
- counter.value # => 1
484
-
485
- # Increase the counter by 5 on another Ractor
486
- Ractor.new(counter) { it.add(5) }
487
-
488
- # Give the other ractor time to run
489
- sleep 0.1
490
-
491
- counter.value # => 6
492
- ```
493
-
494
- #### Flags
495
-
496
- Ractor-shareable, atomic boolean.
497
-
498
- Just like Counter for Integer, this is similar to creating an [atom](#atoms) for a boolean, but significantly faster, as it will use a truly lock-free implementation on most CPU/Ruby combinations.
499
-
500
- ```ruby
501
- flag = Farce::Flag.new(true)
502
- flag.value # => true
503
- ```
504
-
505
- #### References
506
-
507
- Atoms, as well as other value objects, like counters and flags, can be wrapped in a reference object, which delegates all methods to the given atom for convenience:
508
-
509
- ```ruby
510
- atom = Farce::Atom.new(42)
511
- ref = Farce::Reference.new(atom)
512
-
513
- ref.to_s # => "42"
514
- ref > 10 # => true
515
-
516
- atom.value = :foo
517
- ref.to_s # => "foo"
518
- ```
519
-
520
- #### Molecules
521
-
522
- Molecules are structures made up of multiple atoms. Clever naming, right?
523
-
524
- They are similar to Ruby's built-in `Struct` and `Data` classes, but their members are stored in atoms:
525
-
526
- ```ruby
527
- Request = Farce::Molecule.define(:verb, :path)
528
- request = Request.new("GET", "/index.html")
529
- request.verb # => "GET"
530
- request.verb = "POST"
531
-
532
- # This doesn't succeed, as the verb isn't "HEAD"
533
- request.verb_atom.compare_and_set("HEAD", "GET")
534
- request.verb # => "POST"
535
- ```
536
-
537
- You can also pass a block to add additional methods:
538
-
539
- ```ruby
540
- Request = Farce::Molecule.define(:verb, :path) do
541
- def to_s = "#{verb} #{path}"
542
- end
543
-
544
- Request.new(verb: "GET", path: "/").to_s # => "GET /"
545
- ```
546
47
 
547
- You can also inherit from the generated class:
548
-
549
- ```ruby
550
- class Request < Farce::Molecule.define(:verb, :path)
551
- def to_s = "#{verb} #{path}"
48
+ 100.times do
49
+ Farce.in_parallel(list) { it << Some.expensive_computation }
552
50
  end
553
51
  ```
554
52
 
555
- #### Queues
556
-
557
- `Farce::Queue` is an alternative to Ruby's `Queue` and `SizedQueue`. It provides a superset of their API, offering both blocking and a non-blocking API, while implementing Ractor-sharing via [sharing modes](#sharing-modes).
558
-
559
- ```ruby
560
- queue = Farce::Queue.new
561
- queue.push(:ok)
562
- queue.pop # => :ok
563
- queue.try_pop # => nil
564
- ```
565
-
566
- Queues provide additional features over Ruby's queue:
567
- * Like the rest of Farce, they work with Ractors (implementing [sharing modes](#sharing-modes) for their [default variant](#variants-and-scopes)).
568
- * In addition to closing, they allow sealing, which prevents pushes but still allows pulling from the queue until it has been drained.
569
- * They have an API to wait for them to be ready for a push or pull based on capacity without actually adding or removing values.
570
- * They can optionally monitor how long the oldest item has been sitting in the queue (which is great for building auto-scaling on top of the queue).
571
-
572
- The [strict variant](#available-variants) has comparable performance to `SizedQueue`, while the default variant pays a fixed overhead for [handling unshareable data](#sharing-unshareable-data). Both variants outperform other third-party, Ractor-shareable queues (none of which are Fiber scheduler compatible, which Farce's queues are).
573
-
574
- ##### Priority Queues
575
-
576
- In addition to normal queues, which are FIFO, Farce also offers a `PriorityQueue`, where values are removed from the queue in order of priority instead of insertion order:
577
-
578
- ```ruby
579
- # use order: :descending to reverse priorities
580
- queue = Farce::PriorityQueue.new
581
-
582
- queue.push :a, priority: 2
583
- queue.push :b, priority: 1
584
-
585
- queue.pop # => :b
586
- ```
587
-
588
- ##### Timer Queues
589
-
590
- Timer queues are similar to priority queues, sorted by float values, but these values are timestamps (based on [Farce::Clock](#additional-classes), not on Ruby's Time, which isn't monotonic). Any `pop` will block until the next timestamp has been reached.
591
-
592
- ```ruby
593
- queue = Farce::TimerQueue.new
594
- queue.push(:ok, in: 1.5)
595
-
596
- # blocks for ~1.5 seconds
597
- queue.pop # => :ok
598
- ```
599
-
600
- Timer queues are useful for implementing schedulers (which is what Farce uses them for under the hood). Both `push` and `try_push` return a cancellation handle when they succeed. Use `entry.delete` or `queue.delete(entry)` to cancel a scheduled value. The handle exposes its `queue` and scheduled `clock` time.
53
+ Or use a bounded map as a shared cache:
601
54
 
602
55
  ```ruby
603
- queue = Farce::TimerQueue.new
604
- entry = queue.push(:work, in: 60)
605
-
606
- entry.delete # => true
607
- ```
608
-
609
- You can also delete a value if you know its timestamp:
610
-
611
- ```ruby
612
- queue = Farce::TimerQueue.new
613
- timestamp = Farce.clock(in: 0.2)
614
-
615
- queue.push(:first, at: timestamp - 0.1)
616
- queue.push(:second, at: timestamp)
617
- queue.push(:third, at: timestamp + 0.1)
618
-
619
- fourth = queue.push(:fourth, at: timestamp + 0.2)
620
- queue.delete(fourth)
621
-
622
- queue.delete(:second, at: timestamp)
623
-
624
- result = []
625
- result << queue.pop until queue.empty?
626
- result # => [:first, :third]
627
- ```
628
-
629
- ##### Queue capacity
630
-
631
- Queues support setting a maximum capacity. If the capacity has been reached it will block any pushes to create back pressure.
632
-
633
- ```ruby
634
- queue = Farce::Queue.new(capacity: 2)
635
-
636
- queue.try_push(:a) # => true
637
- queue.try_push(:b) # => true
638
- queue.try_push(:c) # => false
56
+ cache = Farce::LRUMap.new(max_size: 2)
57
+ cache[:a] = 1
58
+ cache[:b] = 2
59
+ cache[:c] = 3
60
+ cache.key?(:a) # => false, the least recently used entry was evicted
639
61
  ```
640
62
 
641
- Normal queues have a default capacity of 1024. You can explicitly set the capacity to `nil` to create an unbounded queue.
642
- Priority queues and timer queues do not have a default capacity, so earlier entries are not getting blocked unexpectedly.
63
+ → [Collection APIs and examples](docs/data-structures.md).
643
64
 
644
65
  ### Shims, Polyfills, and Extensions
645
66
 
646
- #### Ractor
647
-
648
- Farce includes `Farce::Ractor`, which will either delegate to `Ractor` or supply a polyfill for it.
67
+ - **`Farce::Ractor`** uses native Ractors on CRuby and threads on JRuby and TruffleRuby.
68
+ - **`Farce::Port`** adds transfer modes, receive timeouts, and Fiber scheduler support.
69
+ - **`Farce::WeakRef` and `Farce::Resolv`** work outside the main Ractor.
70
+ - **`Farce::Backports`** backports newer `Ractor` and `Ractor::Port` features to older Ruby versions. Can be invoked directly, used with refinements, or installed globally.
649
71
 
650
- On platforms that don't support Ractors, a Thread-based implementation is provided that tracks Ractor-membership via Thread
651
- groups.
72
+ Send a result back from a worker:
652
73
 
653
74
  ```ruby
654
- ractor = Farce::Ractor.new do
655
- value = receive
656
- puts "Received #{value.inspect}"
657
- end
658
-
659
- ractor.send 42
75
+ results = Farce::Port.new
76
+ worker = Farce::Ractor.new(results) { |port| port.send(6 * 7) }
77
+ results.receive # => 42
660
78
  ```
661
79
 
662
- #### Port
663
-
664
- Farce includes `Farce::Port`, which is either an extended subclass of `Ractor::Port` if it is available, or a polyfill if it isn't.
665
-
666
- In addition to `Ractor::Port` it supports the following features:
667
- * [Sharing modes](#sharing-modes) both for `.new` and `#send`.
668
- * Opt-in auto-local sharing: If the port belongs to the current Ractor, any object can be sent to it without copying or moving it.
669
- * `#receive` supports a `timeout` option on all Ruby implementations and versions, not just CRuby 4.1+
670
- * You can use `#owned?` to check if the current Ractor owns a port.
671
- * `#receive` does not block a Fiber scheduler when called from a non-blocking Fiber.
672
-
673
- ```ruby
674
- # reject unshareable values except if they are being sent from the ractor owning the port
675
- port = Farce::Port.new(mode: :raise, auto_local: true)
676
- object = Object.new
677
-
678
- port.send(object)
679
- port.receive.equal?(object) # => true
680
- ```
681
-
682
- #### WeakRef
683
-
684
- `Farce::WeakRef` is a drop-in replacement for Ruby's [`WeakRef`](https://docs.ruby-lang.org/en/4.0/WeakRef.html), which at the time of writing cannot be used outside of the main Ractor at all.
685
-
686
- Farce implements a version based on weak [atoms](#atoms). Not only can it be used on non-main Ractors, `Farce::WeakRef` instances themselves are Ractor-shareable if the value they reference is also Ractor-shareable (or has been garbage collected). Moreover, calling `Ractor.make_shareable(weak_ref)` will propagate through to the referenced value.
687
-
688
- #### Resolv
689
-
690
- `Farce::Resolv` is a version of Ruby's [`Resolv`](https://docs.ruby-lang.org/en/4.0/Resolv.html), using [counters](#counters) instead of a global instance variable, and can therefore be used outside the main Ractor.
80
+ → [Portable Ractor APIs and extensions](docs/shims.md).
691
81
 
692
82
  ### Sharing Unshareable Data
693
83
 
694
- Ruby's `Ractor::Port#send` and related methods share Ruby objects between Ractors with this logic:
695
-
696
- 1. If the object is Ractor shareable, pass it by reference.
697
- 2. If the object isn't Ractor shareable and the `move` option hasn't been set to `true`, pass it by value (copy it).
698
- 3. Otherwise move it from the sending Ractor to the receiving Ractor, invalidating any previous references.
699
-
700
- This is a great start but has some complications:
701
- * Copying can lead to a lot of data duplication, as large object trees may be copied between Ractors over and over again.
702
- * Moving may invalidate nested references unexpectedly, breaking code in the sending Ractor.
703
- * If you want to prevent sending unshareable objects to other Ractors, you have to manually check them every time.
704
-
705
- Farce has mechanisms and tools to improve the situation.
706
-
707
- #### Sharing Modes
708
-
709
- > [!TIP]
710
- > Learn more in the dedicated [modes documentation](docs/modes.md).
711
-
712
- The [default variants](#variants-and-scopes) of most [data structures](#data-structures), as well as [`Farce::Port`](#port), implement a range of sharing modes, typically as a keyword argument for initialization as well as for methods that modify their content.
713
-
714
- These modes are:
715
-
716
- | Mode | What happens to non-shareable data | A typical use |
717
- | --- | --- | --- |
718
- | `:smart` (default) | Applies smart rules or a dedicated proxy, raising for unsupported values. | Share supported values with less copying. |
719
- | `:smart_raise` | Applies smart rules or a dedicated proxy, with a raising fallback. | Reject unsupported transfers. |
720
- | `:smart_copy` | Applies smart rules or a dedicated proxy, then tries copying and Marshal. Default for `Farce::Port`. | Send values with a copying fallback. |
721
- | `:smart_move` | Applies smart rules or a dedicated proxy, then tries moving. | Hand off unsupported values. |
722
- | `:copy` | Transfers a copy and leaves the original usable. | Send a snapshot of a request. |
723
- | `:marshal` | Serializes a snapshot and reconstructs it once per receiving Ractor. | Transfer an Unshared container.|
724
- | `:move` | Transfers ownership and makes the original inaccessible. | Hand a completed batch to a consumer. |
725
- | `:local` | Keeps the same object in its originating Ractor. | Pass work between local threads or fibers. |
726
- | `:make_shareable` | Calls `Ractor.make_shareable` on the original. | Publish finished configuration. |
727
- | `:mutable` | Copies non-shareable objects into a [`Farce::Mutable`](#mutables). | Synchronize mutations across Ractors. |
728
- | `:shareable_copy` | Makes a shareable copy and leaves the original alone. | Publish a snapshot of an editable document. |
729
- | `:dedup` | Deduplicates the value, then makes it shareable. May update and freeze the original. | Reuse repeated message contents. |
730
- | `:proxy` | Creates a [`Farce::Proxy`](#proxies) that executes calls in the original Ractor. | Share access to a mutable object. |
731
- | `:raise` | Raises `Ractor::IsolationError`. | Enforce a shareable-data boundary. |
732
-
733
- Here's an example:
734
-
735
- ```ruby
736
- map = Farce::Map.new(mode: :mutable)
737
- map[:content] = "Hi there!"
738
-
739
- Ractor.new(map) { it[:content] << " How are you doing?" }.join
740
-
741
- # Hi there! How are you doing?
742
- puts map[:content]
743
- ```
744
-
745
- #### Envelopes
746
-
747
- Farce allows you to explicitly wrap an object in an envelope. This is useful if you want to pass a value around between multiple Ractors (or multiple times between the same Ractors) without copying, wrapping, converting, or moving it every single time.
748
-
749
- Instead, you can wrap it in an envelope, and then later explicitly retrieve the value again.
750
-
751
- ```ruby
752
- # We don't want to copy this over and over again.
753
- big_array = 10_000.times.map { rand }
754
- envelope = Farce::Envelope.new(big_array) # copied in once, copied out on demand
755
-
756
- Ractor.new(envelope) do |envelope|
757
- # we have a reference to envelope, but not a copy of its data
758
- # this allows us to pass it on easily
759
- queue = Farce::Queue.new(mode: :raise)
760
- queue.push(envelope) # this is okay, the envelope is shareable
761
-
762
- # We can pass the envelope by other means, like a queue!
763
- Ractor.new(queue) do |queue|
764
- message = queue.pop
765
- # okay, let's get the copy, bypassing the outer ractor
766
- copy_of_array = message.value
767
- puts "Size of the array: #{copy_of_array.size}"
768
- end
769
- end
770
- ```
771
-
772
- Envelopes support `copy`, `move`, `local`, as well as a dummy envelope for wrapping shareable objects.
773
-
774
- #### Mutables
775
-
776
- Mutables are special wrappers for Ruby objects that are Ractor-shareable when frozen but can be modified when not.
777
-
778
- ```ruby
779
- mutable = Farce::Mutable.new("content")
780
- Ractor.new(mutable) { it << " & additional content" }.join
781
- mutable # => #<Farce::Mutable[String] "content & additional content">
782
- ```
783
-
784
- They expose a mutable API by keeping a frozen snapshot of the wrapped object, atomically unfreezing, mutating, and freezing it whenever a method otherwise throws a `FrozenError`. This means non-mutating methods have almost no additional cost, and in contrast to [proxies](#proxies), method calls do not have to be dispatched across Ractors, even when modifying the object. However, mutating methods will copy the wrapped object's content. This may be fine, but could be expensive when used repeatedly on large objects.
785
-
786
- #### Proxies
787
-
788
- A proxy mimics the API of another object, executing method calls within the Ractor that created it. This is an easy way to have drop-in replacements for objects that cannot be shared across Ractors.
789
-
790
- ```ruby
791
- # Mutable arrays aren't Ractor-shareable
792
- array = []
793
- proxied = []
794
- proxy = Farce::Proxy.new(proxied)
795
-
796
- Farce::Ractor.new(array, proxy) do |*list|
797
- list.each { it << 42 }
798
- end.join
799
-
800
- # The array got copied instead of being modified in place
801
- array # => []
802
-
803
- # The proxy didn't get copied
804
- proxied # => [42]
805
- ```
806
-
807
- Use this for very large objects, where copying the object would be more expensive than the Ractor-coordination overhead, or objects that can not be moved or copied between Ractors.
808
-
809
- The downside is a dispatch to another Ractor on every method call.
810
- If copying on writes is an option, consider [`Farce::Mutable`](#mutables) instead.
811
- If the object may be moved across Ractors, maybe a [`Farce::Lease`](#leases) is a better option.
812
-
813
- There is an advanced [customization API](https://rkh.github.io/farce/main/Farce/Proxy.html), which allows fine-tuning and reducing overhead.
814
-
815
- #### Mode Managers
816
-
817
- You can use a mode manager if you want to support sharing modes for a custom object.
818
-
819
- The mode manager might wrap objects in an envelope. It also exposes a method to unwrap objects again, which will only do so for envelopes created by the specific mode manager. That's why we were able to pass an envelope through the queue in the [example above](#sharing-modes) without it getting opened automatically.
820
-
821
- ```ruby
822
- manager = Farce::ModeManager.new(mode: :move)
823
- payload = ["inside a mutable array"]
824
- Ractor.shareable?(payload) # => false
825
-
826
- shareable = manager.wrap(payload) # => #<Farce::Envelope::Move>
827
- Ractor.shareable?(shareable) # => true
828
-
829
- payload = shareable.value
830
- Ractor.shareable?(payload) # => false
831
- ```
832
-
833
- #### Leases
834
-
835
- You can think of a `Farce::Lease` as a reusable `move` envelope. Or [Haskell's MVar](https://hackage-content.haskell.org/package/base-4.22.0.0/docs/Control-Concurrent-MVar.html) if that's more your jam.
836
-
837
- You can store any object in it, including non-shareable ones (if they are movable). A Ractor can check objects out (at which point they get moved into that Ractor), work with them, and move them back into the lease container. The checkout call will block while another Ractor holds the lease, serializing any operations.
838
-
839
- This is very useful for objects that can be moved but can't be copied, especially IO-based objects like database connections.
840
-
841
- ```ruby
842
- file_lease = Farce::Lease.new { File.open("example.txt", "w") }
843
-
844
- Ractor.new(file_lease) do |file_lease|
845
- file_lease.checkout do |file|
846
- file.puts "Written from another Ractor!"
847
- end
848
- end.join
849
-
850
- # make sure we close the file
851
- file_lease.checkout(&:close)
852
-
853
- # dereference the lease so it can be garbage collected
854
- file_lease = nil
855
- ```
856
-
857
- However, this might not be enough for handling database connections, where you'd usually have a pool of connections. You can use `Farce::LeasePool` which will manage multiple objects, only blocking on `checkout` if all objects have been leased.
858
-
859
- Moreover, it can generate these objects for you on demand (until a certain number has been reached).
84
+ Transfer modes control non-shareable values on native Ractors. For example, `:copy` sends a snapshot while leaving the original usable:
860
85
 
861
86
  ```ruby
862
- # db connections are created on demand if there are fewer than five
863
- pool = Farce::LeasePool.new(max_size: 5) { DB.connect }
864
- pool.size # => 0
865
- pool.max_size # => 5
87
+ queue = Farce::Queue.new(mode: :copy)
88
+ job = { ids: [1, 2] }
89
+ queue.push(job)
90
+ job[:ids] << 3 # added after copying
866
91
 
867
- pool.checkout do |db|
868
- # ... do something with db ...
92
+ Ractor.new(queue) do |queue|
93
+ queue.pop # => { ids: [1, 2] }
869
94
  end
870
95
  ```
871
96
 
872
- Or, if you want to associate leasable values with keys, you can use a `LeaseMap`:
873
-
874
- ```ruby
875
- leases = Farce::LeaseMap.new { { primary: [], replica: [] } }
876
- leases.checkout(:primary) { |items| items << :updated }
877
- leases.checkout(:primary, &:dup) # => [:updated]
878
- ```
879
-
880
- At first glance, this looks just like a [map](#maps) of `Lease` instances. And it pretty much is, except it has some nice tooling on top of it, where it can automatically check values out and back in:
97
+ JRuby and TruffleRuby share ordinary values directly (`Farce::Ractor.shareable?` reflects this).
881
98
 
882
- ```ruby
883
- leases = Farce::LeaseMap.new { { primary: [], replica: [] } }
99
+ Other tools cover different ownership needs:
884
100
 
885
- leases.auto_lease do
886
- leases[:primary] << :updated
887
- leases[:replica] << :replicated
888
- leases[:primary] << :verified # Reuses the same checkout
889
- end
101
+ - **Envelopes** carry values through several Ractors without repeated transfers.
102
+ - **Mutables** synchronize edits to wrapped objects.
103
+ - **Proxies** run calls in the object's original Ractor.
104
+ - **Leases and lease pools** give callers exclusive access to reusable resources, such as connections.
890
105
 
891
- # Both resources are checked back in when the block exits, even on an exception.
892
- leases.available?(:primary) # => true
893
- leases.checkout(:primary, &:dup) # => [:updated, :verified]
894
- ```
106
+ → [Transfer modes, wrappers, and resource checkout](docs/sharing.md).
895
107
 
896
108
  ### Variants and Scopes
897
109
 
898
- Many classes, including all [data structures](#data-structures), implement multiple variants as separate subclasses within module namespaces.
899
-
900
- `Farce::Map`, `Farce::Strict::Map`, `Farce::Local::Map`, and `Farce::Unshared::Map` all support the same API, except that:
901
-
902
- * `Farce::Map` accepts an optional `mode` keyword for its initializer and many methods.
903
- * `Farce::Strict::Map` will not allow any non-shareable data to be stored in it. It is slightly faster than `Farce::Map`.
904
- * `Farce::Local::Map` will accept an optional `scope` keyword for its initializer, and will have different content for each [scope](#provided-scopes).
905
- * `Farce::Unshared::Map` cannot be shared across Ractors, but in turn can store any Ruby object directly, including unshareable objects. It is as fast as `Farce::Strict::Map`.
906
-
907
- #### Available Variants
908
-
909
- > [!TIP]
910
- > Learn more in the dedicated [variants documentation](docs/variants.md).
911
-
912
- These variants are provided by Farce:
913
-
914
- 1. **Default** variants, under the `Farce` namespace:
915
- * Can **store unshareable values**, usually via [modes](#sharing-modes).
916
- For maps, this is only supported for values. Keys must be shareable.
917
- * Instances are **Ractor-shareable**.
918
- * The data structure is **thread-safe**.
919
- 2. **Strict** variants, under the `Farce::Strict` namespace:
920
- * **Forbid unshareable values**
921
- * Instances are **Ractor-shareable**.
922
- * The data structure is **thread-safe**.
923
- * May provide performance benefits over the default variant.
924
- 3. **Local** variants, under the `Farce::Local` namespace:
925
- * Will have **different content** for each [scope](#provided-scopes).
926
- * Can **store unshareable values**, including unshareable map keys.
927
- * Instances are **Ractor-shareable**.
928
- * The data structure is **thread-safe**.
929
- 4. **Unshared** variants, under `Farce::Unshared` namespace:
930
- * Can **store unshareable values**, including unshareable map keys.
931
- * Instances are **<u>not</u> Ractor-shareable**.
932
- * The data structure is **thread-safe**.
933
- * May provide performance benefits over the default variant.
934
- 5. **Unsafe** variants, under `Farce::Unsafe` namespace:
935
- * Same as Unshared, except they do **<u>not</u> guarantee thread-safety**
936
- * May provide performance benefits over all other variants.
937
- 6. **Transaction** variants, under `Farce::Transaction` namespace:
938
- * Created as mirrors of another object within a [transaction](#transactions).
939
- * Should not be shared across transaction boundaries.
940
- * Should not be initialized directly.
941
-
942
- The following is true for all variants:
943
- * Blocking operations will suspend a non-blocking fiber, but not block the underlying scheduler.
944
- * The return value of `frozen?` will correctly reflect whether an instance is mutable or not.
945
-
946
- However, keep the following in mind:
947
- * Not all classes implement all variants. Check out the [full list](docs/variants.md#classes-implementing-variants)
948
- * Other classes that don't implement variants are also nested under the `Farce` namespace.
949
-
950
- #### Provided scopes
951
-
952
- > [!TIP]
953
- > Learn more in the dedicated [scopes documentation](docs/scopes.md).
954
-
955
- Local variants support scopes:
956
-
957
- ```ruby
958
- map = Farce::Map.new(scope: :thread)
959
- map[:id] = 1
960
-
961
- Thread.new do
962
- map[:id] = 2
963
- map[:id] # => 2
964
- end.join
965
-
966
- map[:id] # => 1
967
- ```
968
-
969
- The following scopes are available:
970
-
971
- | Scope | Description |
972
- | ------------------ | ------------------------------------------------------------------------------------------------ |
973
- | `ractor` (default) | Content varies by [Ractor](https://docs.ruby-lang.org/en/4.0/Ractor.html) |
974
- | `thread_group` | Content varies by [ThreadGroup](https://docs.ruby-lang.org/en/4.0/ThreadGroup.html) |
975
- | `thread` | Content varies by [Thread](https://docs.ruby-lang.org/en/4.0/Thread.html) |
976
- | `fiber_storage` | Content varies by [Fiber storage](https://docs.ruby-lang.org/en/4.0/Fiber.html#method-i-storage) |
977
- | `fiber` | Content varies by [Fiber](https://docs.ruby-lang.org/en/4.0/Fiber.html) |
978
-
979
- Fiber storage is typically inherited by a blocking fiber from the fiber creating it.
980
-
981
- ### Concurrency
982
-
983
- Farce is built for concurrent and parallel code execution.
984
-
985
- #### Locks
110
+ Choose where state lives and what it can contain:
986
111
 
987
- Farce ships with two lock classes:
988
- * `Lock` is a drop-in replacement for Ruby's `Mutex`.
989
- * `ReadWriteLock` exposes `with_read_lock`/`with_write_lock` to allow multiple concurrent reads, but exclusive write access.
990
-
991
- However, Farce exposes many APIs to eliminate the need for locks altogether.
112
+ | Variant | Use it when… |
113
+ |-------------------|---------------------------------------------------------------------------------------|
114
+ | `Farce` | Workers across Ractors need shared state, including values handled by transfer modes. |
115
+ | `Farce::Strict` | All stored values must already be shareable. |
116
+ | `Farce::Unshared` | Threads in one Ractor need to store arbitrary Ruby objects directly. |
117
+ | `Farce::Local` | Each Ractor, thread, or fiber needs its own contents behind one handle. |
992
118
 
993
- #### Atomic Operations
994
-
995
- Farce's [data structures](#data-structures) all expose a range of methods for atomic operations:
119
+ For example, keep request context separate for each fiber:
996
120
 
997
121
  ```ruby
998
- # A Hash-like object
999
- map = Farce::Map.new
1000
-
1001
- # Atomically store something for :key if it hasn't been set
1002
- map.store_if_absent(:key) { "initial value" }
1003
-
1004
- # Atomically update :key
1005
- map.update(:key, &:upcase)
1006
-
1007
- # An Array-like object
1008
- list = Farce::Vector.new
1009
- list[0] = 42
1010
-
1011
- # Atomically replace list[0] with 256 if the value is still 42
1012
- list.compare_and_set(0, 42, 256)
122
+ context = Farce::Local::Map.new(scope: :fiber)
123
+ context[:request_id] = :parent
124
+ Fiber.new { context[:request_id] = :child }.resume
125
+ context[:request_id] # => :parent
1013
126
  ```
1014
127
 
1015
- #### Observability and Signaling
1016
-
1017
- All the [data structures](#data-structures) come with extra observability methods, which eliminate the need for using [condition variables](https://docs.ruby-lang.org/en/4.0/Thread/ConditionVariable.html) and [mutexes](https://docs.ruby-lang.org/en/4.0/Thread/Mutex.html).
1018
-
1019
- Even setting aside that these don't work across ractors, this drastically reduces the risk of race conditions (very easy to do if you <u>don't</u> use the same mutex everywhere) or blocking code you don't need to block (very easy to do if you <u>do</u> use the same mutex everywhere).
1020
-
1021
- So instead of sharing locks you can simply wait for a change to happen!
1022
-
1023
- ```ruby
1024
- # block until the stored value for :key is greater than 10
1025
- map.wait_until(:key) { it > 10 }
1026
-
1027
- # block until the value for an atom is no longer :initial
1028
- map.wait_until_changed(:initial)
128
+ Available variants depend on the class. → [All variants and scopes](docs/variants-and-scopes.md).
1029
129
 
1030
- # block until a counter has reached at least 20
1031
- counter.wait_while_below(20)
1032
- ```
130
+ ### Concurrency
1033
131
 
1034
- If these conditions are met right away, these never block.
132
+ - **Atomic operations**, such as `upsert` above, combine reads and writes.
133
+ - **Wait methods and signals** wait for state changes without managing condition variables.
134
+ - **Transactions** publish coupled changes together. Locks are available too.
1035
135
 
1036
- But what about more complex conditions, involving multiple variables, or objects from other libraries that don't implement similar methods? No need to reach for a lock! You can use a Signal!
136
+ Use a flag to wait for an event:
1037
137
 
1038
138
  ```ruby
1039
- signal = Farce::Signal.new
1040
- target = 100
1041
- counter = Farce::Counter.new
139
+ flag = Farce::Flag.new
1042
140
 
1043
- # A Thread that keeps reducing the target value every 20 milliseconds
1044
141
  Thread.new do
1045
- while target.positive?
1046
- sleep 0.02
1047
- target -= 1
1048
- signal.broadcast # notify everyone else
1049
- end
142
+ lots_of_work_here
143
+ flag.set
1050
144
  end
1051
145
 
1052
- # A Ractor that counts up in 10 millisecond intervals
1053
- Ractor.new(counter, signal) do |counter, signal|
1054
- while counter < 100
1055
- sleep 0.01
1056
- counter.increment
1057
- signal.broadcast # notify everyone else
1058
- end
1059
- end
1060
-
1061
- # wait until the counter is at or above the target
1062
- signal.wait_until { counter >= target }
146
+ flag.wait_until_set
1063
147
  ```
1064
148
 
1065
- #### Transactions
1066
-
1067
- This all sounds great. But you still might want to reach for a lock if you want to modify more than one data structure, if their state is tightly coupled (i.e., updating one without yet updating the other would leave your code in an invalid state).
1068
-
1069
- And a lock is an acceptable solution here. Again, you might want to wrap all the read access in a lock, as there will still be an invalid state. And you also want to roll back any changes already made while holding the lock, if an exception occurs. That is the standard approach in a lot of Ruby code.
1070
-
1071
- Farce offers an alternative. It implements [STM-style transactions](https://en.wikipedia.org/wiki/Software_transactional_memory). You supply a block of code that makes changes to multiple data structures. These changes are only written to these data structures in a commit phase after the block finishes, and they either all succeed or all fail, and they only succeed if the values you've read from any of these data structures haven't changed.
1072
-
1073
- Otherwise the block is rerun.
1074
-
1075
- And this isn't limited to special `TVar` containers, like the APIs provided by concurrent-ruby or ractor-sharing. It supports Farce's main data structures, including [maps](#maps), [vectors](#vectors), [sets](#sets), [atoms](#atoms), and [molecules](#molecules). Moreover, it also supports [mutables](#mutables), meaning you can turn most Ruby objects into something transaction compatible fairly easily!
1076
-
1077
- In contrast to other implementations mentioned above, there is no implicit tracking. You need to explicitly add an object to a transaction. This also avoids any uncertainty around nested transactions and unexpected rollbacks.
149
+ Transfer funds without exposing a partially updated balance:
1078
150
 
1079
151
  ```ruby
1080
- accounts = Farce::Map.new({a: 100, b: 200})
1081
-
1082
- # transfer 80 from :a to :b, but only if both succeed
1083
- success = Farce.transaction(accounts) do |tx, accounts|
1084
- tx.abort! if accounts[:b] < 80
1085
- accounts[:a] += 80
1086
- accounts[:b] -= 80
1087
- end
1088
-
1089
- if success
1090
- puts "Transaction succeeded"
1091
- else
1092
- puts "Transaction failed"
152
+ accounts = Farce::Map.new({ checking: 100, savings: 0 })
153
+ Farce.transaction(accounts) do |tx, balances|
154
+ tx.abort! if balances[:checking] < 20
155
+ balances[:checking] -= 20
156
+ balances[:savings] += 20
1093
157
  end
158
+ accounts.to_h # => { checking: 80, savings: 20 }
1094
159
  ```
1095
160
 
1096
- In the above example, `accounts` was added to the transaction right away. But you can also add new objects to the transaction programmatically by calling `tx[object]`. All reads and writes need to happen through the wrapper object returned by that call (or passed to the block).
1097
-
1098
- ##### Maps and Sets
1099
-
1100
- Maps and sets use fine-grained transaction tracking. In the above example, if accounts had another entry, `:c`, its value changing would not impact the transaction at all.
1101
-
1102
- Similarly, if your transaction uses `Transaction::Map#size` as input, only a change in size would trigger a rerun, not a change in content.
1103
-
1104
- This is ideal for scenarios where you use maps as a general data store and sets to track whether an operation was performed on an object (a common pattern to prevent infinite recursion).
1105
-
1106
- ##### TVars
161
+ Transaction blocks may rerun after a conflict. Keep external side effects outside them.
1107
162
 
1108
- Farce doesn't come with a `TVar` class. You can just use an [atom](#atoms) instead.
1109
-
1110
- But it does come with built-in support for [`Concurrent::TVar`](https://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/TVar.html) and [`Ractor::TVar`](https://github.com/ko1/ractor-sharing/blob/main/docs/tvar.md) (assuming you also load these libraries, see [integrations](#integrations)).
1111
-
1112
- ```ruby
1113
- tvar = Concurrent::TVar.new(50)
1114
- map = Farce::Map.new({a: 100, b: 200})
1115
-
1116
- Farce.transaction(map, tvar) do |map, tvar|
1117
- # subtract tvar's value from a and b, then set tvar to 0
1118
- # no invalid in-between state is ever visible to anything outside of this transaction
1119
- map[:a] -= tvar.value
1120
- map[:b] -= tvar.value
1121
- tvar.value = 0
1122
- end
1123
- ```
163
+ → [Atomic updates, waiting, locks, and transactions](docs/concurrency.md).
1124
164
 
1125
165
  ### Scheduling Code
1126
166
 
1127
- Farce offers built-in code scheduling support.
1128
-
1129
- #### `Farce.on_main`
1130
-
1131
- Some code has to be executed on the main Ractor, especially when working with legacy code that doesn't support Ractors.
1132
-
1133
- You can pass a block to `Farce.on_main` to run code on the main Ractor. The method call will block until the code has been executed.
1134
-
1135
- ```ruby
1136
- # most of concurrent-ruby is not usable outside of the main-ractor
1137
- $tvar = Concurrent::TVar
1138
-
1139
- # let's run computation outside of the main ractor
1140
- Ractor.new do
1141
- value = compute_expensive_value
1142
-
1143
- # need to report back to the main ractor
1144
- Farce.on_main(value) { $tvar.value = it }
1145
- end
1146
- ```
1147
-
1148
- Sometimes you don't need to wait for the code to be done running on the main Ractor. In such cases, you can use `schedule`, as `on_main` returns a [scheduler](#schedulers) instance when called without a block:
1149
-
1150
- ```ruby
1151
- Ractor.new do
1152
- Farce.on_main.schedule do
1153
- sleep 1
1154
- puts "Hello from the main Ractor"
1155
- end
1156
- puts "Hello from the nested Ractor"
1157
- end
1158
-
1159
- sleep 1.1
1160
- ```
1161
-
1162
- #### `Farce.in_parallel`
1163
-
1164
- As you can see in the [very first example](#farce-fiber-and-ractor-compatibility-enabler), creating Ractors ad hoc because you want to run something in parallel is quite expensive.
1165
-
1166
- Farce offers `Farce.in_parallel` instead, which will manage a [Ractor Pool](#ractor-pools), starting new ractors if the current ones cannot keep up with the current load (but at most as many as the system's CPU cores). It also shuts unused Ractors down again after some inactivity.
1167
-
1168
- ```ruby
1169
- input = "this is my input"
1170
- Farce.in_parallel(input) { expensive_computation(it) }
1171
- ```
1172
-
1173
- Like most of Farce, this is completely opt-in. If you never use this feature, no pool is being set up and no extra Ractors are created.
1174
-
1175
- #### `Farce.schedule`
1176
-
1177
- `Farce.in_parallel` might not always be what you want. Maybe you are working on a library and don't want to dictate a concurrency model?
1178
-
1179
- You can use `Farce.schedule`, which will automatically figure out how to run code off-band. You can use `mode: local` to force execution inside the current Ractor (which it will prefer by default if there already is some scheduler running, but not enforce).
1180
-
1181
- ```ruby
1182
- # just run this asynchronously, don't care how
1183
- Farce.schedule("hello") { |message| puts message.upcase }
1184
- ```
1185
-
1186
- "Some scheduler?" you might say? Some scheduler! It will automatically detect if there is a local scheduler. This can either be a Fiber scheduler, like [async](https://socketry.github.io/async/) or [Carbon Fiber](https://yaroslav.io/opensource/carbon_fiber), or an internal scheduler created by Farce:
1187
-
1188
- ```ruby
1189
- Async do
1190
- # this is basically the same as calling Async { do_something }
1191
- Farce.schedule { do_something }
1192
- end
1193
- ```
1194
-
1195
- #### Schedulers
1196
-
1197
- You can create your own `Farce::Scheduler`, running on a dedicated Ractor:
1198
-
1199
- ```ruby
1200
- scheduler = Farce::Scheduler.create
1201
- scheduler.schedule("hello") { |message| puts message }
1202
- scheduler.close # we're done, shut it down
1203
- ```
1204
-
1205
- Or you can set one up as a Fiber scheduler for the current Thread:
1206
-
1207
- ```ruby
1208
- scheduler = Farce::Scheduler.new
1209
- Fiber.set_scheduler(scheduler)
1210
- Fiber.schedule { puts "Hello from the scheduler!" }
1211
- ```
1212
-
1213
- Scheduler instances are Ractor-shareable.
1214
-
1215
- #### Ractor Pools
1216
-
1217
- `Farce::Pool` implements the same scheduling interface, but manages a pool of Ractors. This is what [`in_parallel`](#farcein_parallel) uses under the hood.
1218
-
1219
- ```ruby
1220
- # At least two Ractors, up to four. Launch a new one if a task waits longer than 100 milliseconds.
1221
- pool = Farce::Pool.new(min_size: 4, max_size: 10, grow_after: 0.1)
1222
- pool.schedule { puts "Hello from the pool!" }
1223
- ```
1224
-
1225
- #### Third-Party Fiber Schedulers
167
+ - **`Farce.schedule`** runs background work, using an available local scheduler when possible.
168
+ - **`Farce.on_main`** dispatches calls to the main Ractor.
169
+ - **`Farce.in_parallel`** schedules parallel work, reusing a Ractor pool on CRuby.
1226
170
 
1227
- Both `Farce::Scheduler` and `Farce::Pool` run a Fiber scheduler under the hood to execute tasks. This is a Farce-internal scheduler by default, but you can replace it with your own if you want:
171
+ Use a task when you need the result:
1228
172
 
1229
173
  ```ruby
1230
- # Use the fiber scheduler from the carbon_fiber gem.
1231
- pool = Farce::Pool.new { CarbonFiber::Scheduler.new }
1232
- pool.schedule { puts "⚡️ Running Carbon Fiber on a pool of Ractors! ⚡️" }
174
+ task = Farce::Task.in_parallel(21) { |number| number * 2 }
175
+ task.value # => 42, waits for completion
1233
176
  ```
1234
177
 
1235
- You can also use a different fiber scheduler as the default (in which case it will be picked up by `in_parallel` and `on_main` as well) by setting the `FARCE_FIBER_SCHEDULER` environment variable or using the `fiber_scheduler` configuration setting:
178
+ You can also create your own schedulers and pools, or use third-party Fiber schedulers.
1236
179
 
1237
- ```ruby
1238
- # This needs to happen before the first scheduler call.
1239
- Farce.configure do |config|
1240
- config.fiber_scheduler = :carbon_fiber
1241
-
1242
- # or, alternatively:
1243
- config.fiber_scheduler { CarbonFiber::Scheduler.new }
1244
- end
1245
-
1246
- Farce.in_parallel do
1247
- Fiber.scheduler.class # => CarbonFiber::Scheduler
1248
- end
1249
- ```
1250
-
1251
- If a string or symbol is provided (like `carbon_fiber`), it will first resolve this to a constant (i.e., `CarbonFiber`). If that constant is a class, it will use that for creating the fiber scheduler. If it is a module, it will look for a `Scheduler` constant inside it, which matches the established pattern implemented by most gems, including `farce`, `async`, `carbon_fiber`, `libev_scheduler` (use `libev` as value), `itsi_scheduler` (use `itsi` as value), but it also works for gems that define their scheduler class at top level, like `fiber_scheduler`.
1252
-
1253
- Note that most of these are outdated and don't work with any recent Ruby version, with the notable exception of `async` and `carbon_fiber`.
180
+ → [Tasks, execution choices, and scheduler lifecycle](docs/scheduling.md).
1254
181
 
1255
182
  ### Integrations
1256
183
 
1257
- Farce ships with a couple of integrations that are automatically loaded if and only if both farce and the other gem have also been loaded (it does not automatically load these gems, even if they are part of the current bundle). This is load-order independent.
1258
-
1259
- #### Active Support
1260
-
1261
- The Active Support integration adds the following methods:
1262
-
1263
- * For all data structures: `as_json`, `blank?`, `deep_dup`, and `duplicable?`
1264
- * For maps: `assert_valid_keys`, `compact_blank`, `reverse_merge`, `stringify_keys`, `symbolize_keys`, `to_param`, `to_query`, `with_defaults`, and `with_indifferent_access`
1265
- * For vectors: `compact_blank`, `excluding`, `from`, `including`, `inquiry`, `in_groups`, `in_groups_of`, `in_order_of`, `maximum`, `minimum`, `pluck`, `pick`, `split`, `to`, `to_fs`, `to_param`, `to_sentence`, `to_query`, `to_xml`, `second`, `third`, `fourth`, `fifth`, `forty_two`, `third_to_last`, and `second_to_last`
1266
-
1267
- And the following features:
1268
- * Converting `ActiveSupport::HashWithIndifferentAccess` to a map via [`Farce.enfarce`](#top-level-methods) will set up the correct key normalization.
1269
- * [`Farce::Clock`](#additional-classes) understands `ActiveSupport::Duration`.
1270
-
1271
- Other methods are already being inherited by various objects via `Object`, `Enumerable` for vectors, sets, and maps, `Numeric` for counters, etc.
1272
-
1273
- ```ruby
1274
- require "active_support/all"
1275
- require "farce"
1276
-
1277
- map = Farce::Map.new.with_indifferent_access
1278
- map[:a] = 10
1279
-
1280
- map.blank? # => false
1281
- map["a"] # => 10
1282
- ```
1283
-
1284
- The integration isn't triggered by loading `active_support`, but instead looks for `active_support/core_ext`, so you should require that (or `active_support/all`, which in turn requires it).
1285
-
1286
- #### Dry Types
184
+ Use Farce collections with existing libraries:
1287
185
 
1288
- Adds support for Farce [data structures](#data-structures) to [Dry Types](https://hanakai.org/learn/dry/dry-types):
1289
-
1290
- ```ruby
1291
- require "dry-types"
1292
- require "farce"
1293
-
1294
- module Types
1295
- include Dry.Types()
1296
- include Farce.DryTypes()
1297
-
1298
- IntegerVector = Vector.of(Coercible::Integer)
1299
- end
1300
-
1301
- numbers = Types::IntegerVector[["1", 2]]
1302
- numbers.class # => Farce::Vector
1303
- numbers.to_a # => [1, 2]
1304
- ```
1305
-
1306
- Check out the [dedicated documentation](docs/gems/dry-types.md) to learn more.
1307
-
1308
- #### JSON, YAML, etc
1309
-
1310
- It ships integrations for serialization (and some deserialization) with the following gems:
1311
-
1312
- * For BSON support: `bson`
1313
- * For CBOR support: `cbor`
1314
- * For JSON support: `json` (from the Ruby standard library), `oj`, and `yajl`
1315
- * For MessagePack: `msgpack` – see the [detailed documentation](docs/gems/msgpack.md)
1316
- * For YAML: `psych` (from the Ruby standard library)
1317
-
1318
- Example:
186
+ - **Active Support** adds helpers such as indifferent key access.
187
+ - **Dry Types** validates and coerces Farce collections.
188
+ - **JSON, YAML, MessagePack, BSON, and CBOR** serialize them.
189
+ - **TVar adapters** bring existing transactional values into Farce transactions.
1319
190
 
1320
191
  ```ruby
1321
192
  require "json"
1322
193
  require "farce"
1323
194
 
1324
- map = Farce::Map.new
1325
- map[:x] = Farce::Vector[1, 2, 3]
1326
- map.to_json # => '{"x":[1,2,3]}'
195
+ map = Farce::Map.new({ ids: Farce::Vector[1, 2] })
196
+ map.to_json # => '{"ids":[1,2]}'
1327
197
  ```
1328
198
 
1329
- #### Concurrent Ruby
1330
-
1331
- The integration adds the following features if [concurrent-ruby](https://github.com/ruby-concurrency/concurrent-ruby) has been loaded:
199
+ Integrations load when both libraries have been required, in either order. Farce does not load the other library for you.
1332
200
 
1333
- * [Transaction support for `Concurrent::TVar`](#tvars)
1334
- * Automatic conversion of `Concurrent::Map` instances to [maps](#maps) via [`Farce.enfarce`](#top-level-methods).
201
+ → [Integration APIs and disabling automatic loading](docs/integrations.md).
1335
202
 
1336
- #### Ractor Sharing
203
+ ### Utilities and Custom Classes
1337
204
 
1338
- The integration adds the following features if [ractor-sharing](https://github.com/ko1/ractor-sharing) has been loaded:
1339
-
1340
- * [Transaction support for `Ractor::TVar`](#tvars)
1341
- * Support for traversing and deep freezing `Ractor::TVar`, `Ractor::LockVar`, `Ractor::LockHash`, and `Ractor::KeyLockHash`
1342
- * Support for converting `Ractor::LockHash` and `Ractor::KeyLockHash` to [maps](#maps) via [`Farce.enfarce`](#top-level-methods).
1343
-
1344
- #### Additional Integrations
1345
-
1346
- * [`sorted_set`](https://github.com/knu/sorted_set): Add support for walking and converting them.
1347
- * [`ractor-tmvar`](https://github.com/yoshitsugu/ractor-tmvar): Adds transaction support.
1348
- * [`weakref`](https://github.com/ruby/weakref): Add support for walking and converting them.
1349
-
1350
- #### Disable Automatic loading
1351
-
1352
- You can set the environment variable `FARCE_AUTOLOAD_INTEGRATIONS` to `false` or `0` to disable automatic integration loading.
1353
-
1354
- Or you can use `Farce.configure` to disable it. But you may have to do so before loading `farce` unless you're absolutely certain the other gem has not yet been loaded:
205
+ Adapt existing data without rebuilding it by hand:
1355
206
 
1356
207
  ```ruby
1357
- # This could go in an initializer
1358
- require "farce/config"
1359
-
1360
- Farce.configure do |config|
1361
- config.autoload_integrations = false
1362
- end
1363
-
1364
- require "farce"
1365
-
1366
- # you now need to load any integrations you might want explicitly
1367
- # these are available via "farce/integrations/#{gem_name}"
1368
- require "farce/integrations/json"
1369
- require "farce/integrations/concurrent"
1370
- ```
1371
-
1372
- ### Miscellaneous
1373
-
1374
- #### Top Level Methods
1375
-
1376
- * `Farce.clock` returns the monotonic clock time in seconds as a Float.
1377
- * `Farce.config` returns the global configuration object.
1378
- * `Farce.configure` allows you to configure Farce.
1379
- * `Farce.dedup` de-duplicates the given object based on a deduplication cache shared by all Ractors.
1380
- * `Farce.enfarce` turns a vanilla data structure into its Farce equivalent.
1381
- * `Farce.freeze_graph` recursively freezes an object graph.
1382
- * `Farce.in_parallel`, `Farce.on_main`, and `Farce.schedule`, see [Scheduling Code](#scheduling-code)
1383
- * `Farce.rebind` rebinds a proc or lambda while preserving its Ractor-shareability.
1384
- * `Farce.transaction` creates and runs a [transaction](#transactions).
1385
-
1386
- Some examples:
1387
-
1388
- ```ruby
1389
- a = { a: [+"b"] }
1390
- b = { a: [+"b"] }
1391
-
1392
- # Farce.clock
1393
- Farce.clock # => 0.017476999908685684
1394
- Farce.clock(in: 10) # => 10.017520000003278
1395
-
1396
- # Farce.dedup
1397
- a.equal? b # => false
1398
- Farce.dedup(a).equal? Farce.dedup(b) # => true
1399
-
1400
- # Farce.enfarce
1401
- Farce.enfarce(a) # => #<Farce::Map {a: #<Farce::Vector ["b"]>}>
1402
- Farce::Strict.enfarce(a) # => #<Farce::Strict::Map {a: #<Farce::Strict::Vector ["b"]>}>
1403
-
1404
- # Farce.freeze_graph
1405
- Farce.freeze_graph(a)
1406
-
1407
- # Farce.rebind
1408
- callback = ->(add) { self + add }
1409
- rebound = Farce.rebind(callback, self: 42)
1410
- rebound.call(18) # => 50
208
+ config = Farce.enfarce({ retries: [1, 2, 3] })
209
+ config.class # => Farce::Map
210
+ config[:retries].class # => Farce::Vector
1411
211
  ```
1412
212
 
1413
- #### Additional Classes
1414
-
1415
- Other classes Farce provides include:
1416
- * `Config`: Configuration class, see [Third-Party Fiber Schedulers](#third-party-fiber-schedulers) example.
1417
- * `ClassMirror`: inheritance-aware registry for classes
1418
- * `Clock`: Timing functions based on a monotonic clock rather than on `Time`.
1419
- * `Deduper`: Create your own deduplication cache. Direct usage isn't recommended, use [`Farce.dedup`](#top-level-methods) instead.
1420
- * `Exchanger`: A synchronization point for two-way data swapping between Threads, Ractors, and/or Fibers. Drop-in replacement for concurrent-ruby's exchanger.
1421
- * `Lazy`: Lazily initialized value.
1422
- * `LazyRef`: A [reference](#references) for a lazily initialized value.
1423
- * `ThreadScheduler`: An alternative [scheduler](#scheduling-code) creating a new thread for each unit of work. Used on TruffleRuby.
1424
- * `Walker`: A tool for walking a Ruby object tree.
1425
- * `WeakValue`: A value object version of [`WeakRef`](#weakref). Allows handling references more explicitly, without automatic method delegation.
1426
-
1427
- In addition, Farce includes a range of error classes not listed here. Check the [API documentation](https://rkh.github.io/farce/) or [code base](lib/farce/error.rb) for these.
1428
-
1429
- #### Shareability Mixins
1430
-
1431
- Farce includes mixins to help you make your custom classes shareable:
1432
-
1433
- * `Shareable` automatically marks objects as shareable (via `Ractor.make_shareable`) after initialization.
1434
- * `Shareable::Delegated` delegates `freeze` and `frozen?` to another object holding your object's state.
1435
- * `Shareable::Immutable` instances are always immutable, being frozen after initialization.
1436
- * `Shareable::Native` is for objects implemented in a native extension, which allows setting the frozen and shareable state separately.
1437
- * `Shareable::Tracked` for objects implementing frozen tracking (via an internal [flag](#flags)).
1438
- * `Shareable::Unfreezable` for objects that cannot be frozen (like [queues](#queues)).
1439
-
1440
- And also mixins to prevent them from being shareable:
1441
-
1442
- * `Unshareable`: Instances aren't shareable and cannot be made shareable. By default, they also cannot be copied or moved between Ractors.
1443
- * `Unshareable::Copyable`: Instances aren't shareable but may be copied to another Ractor.
1444
- * `Unshareable::Movable`: Instances aren't shareable but may be moved to another Ractor.
1445
-
1446
- `Unshareable::Copyable` and `Unshareable::Movable` may be combined.
1447
-
1448
- #### Constants
1449
-
1450
- * `Farce::MODES`: List of supported [sharing modes](#sharing-modes).
1451
- * `Farce::SCOPES`: List of [provided scopes](#provided-scopes).
1452
- * `Farce::VERSION`: The current version.
1453
-
1454
- ## Compatibility and Dependencies
1455
-
1456
- Farce has no mandatory dependencies beyond Ruby itself.
1457
-
1458
- ### Ruby
1459
-
1460
- Each Farce release is expected to be compatible with:
1461
-
1462
- * The [latest patch release](https://www.ruby-lang.org/en/downloads/releases/) for each [CRuby](https://www.ruby-lang.org/en/) version [still receiving bug fixes](https://www.ruby-lang.org/en/downloads/branches/).
1463
- * Ruby's [master branch](https://github.com/ruby/ruby/tree/master) at the time of release (i.e., the upcoming major version of CRuby).
1464
- * The latest stable release of [JRuby](https://www.jruby.org/) and [TruffleRuby](https://truffleruby.dev/) (both in native and GraalVM modes).
1465
-
1466
- Moreover:
1467
-
1468
- * Dropping support for a CRuby version is only done in major releases.
1469
- * If support for an older CRuby version is dropped, Farce will still backport security fixes for at least as long as that CRuby version is [still receiving security fixes](https://www.ruby-lang.org/en/downloads/branches/).
1470
-
1471
- ### Optional Dependencies
1472
-
1473
- Farce's optional [integrations](#integrations) depend on external libraries. Automated tests only run against recent versions of these libraries, but the code is written to be compatible with as wide a range as possible.
1474
-
1475
- Farce should be compatible with these versions:
1476
-
1477
- | Gem | Minimum Version | Release Date |
1478
- |-------------------|-----------------|--------------|
1479
- | `ractor-tmvar` | 0.3.0 | 2026-10-06 |
1480
- | `msgpack` | 1.7.0 | 2023-03-29 |
1481
- | `concurrent-ruby` | 1.1.8 | 2021-01-20 |
1482
- | `dry-types` | 1.0.0 | 2019-04-23 |
1483
- | `activesupport` | 5.1.0 | 2017-04-27 |
1484
- | `oj` | 2.8.1 | 2014-04-21 |
1485
- | `bson` | 2.0.0 | 2013-12-02 |
1486
- | `psych` | 1.1.0 | 2011-03-30 |
1487
-
1488
- Any version of `cbor`, `json`, `ractor-sharing`, `sorted_set`, and `weakref` should be supported
1489
-
1490
- If you want to use any of these in a version that is incompatible with Farce, you can [disable automatic integration loading](#disable-automatic-loading).
1491
-
1492
- ### Similar Projects
213
+ Other helpers freeze object graphs, deduplicate values, and measure monotonic time. Lazy values and exchangers coordinate initialization and handoffs. Shareability mixins define how custom classes can be frozen, shared, copied, or moved.
1493
214
 
1494
- * [ractor-shim](https://github.com/eregon/ractor-shim/) provides similar functionality to `Farce::Ractor`. See [the comparison document](docs/gems/ractor-shim.md) for more details.
1495
- * [concurrent-ruby](https://github.com/ruby-concurrency/concurrent-ruby) provides a more complete set of concurrency primitives than Farce, but is not compatible with Ractors.
1496
- * [ratomic](https://mperham.github.io/ratomic/) has overlapping functionality with Farce for basic data structures like maps, counters, and queues.
1497
- * [ractor_safe](https://github.com/jhawthorn/ractor_safe/) has overlapping functionality with Farce for basic data structures like maps, counters, and queues.
1498
- * [ractor-sharing](https://github.com/ko1/ractor-sharing) has overlapping functionality with Farce for basic data structures like maps, counters, queues, as well as software transactional memory.
1499
-
1500
- All of the above projects can safely be used alongside Farce in the same application.
215
+ → [Conversion helpers and extension points](docs/utilities.md).
1501
216
 
1502
217
  ## Installation
1503
218
 
1504
- ### Globally
1505
-
1506
- To install Farce globally, you can use the following command:
1507
-
1508
- ```console
1509
- $ gem install farce
1510
- ```
1511
-
1512
- ### As a project dependency
1513
-
1514
- If you want to use Farce directly in your project, it is recommended to do so via [Bundler](https://bundler.io).
1515
- Add Farce to your `Gemfile`:
219
+ Add to your application's `Gemfile`, then run `bundle install`:
1516
220
 
1517
221
  ```ruby
1518
- source "https://gem.coop" # or "https://rubygems.org"
1519
-
1520
222
  gem "farce"
1521
223
  ```
1522
224
 
1523
- Then run `bundle install` to install the dependencies.
1524
-
1525
- ### As a library dependency
1526
-
1527
- Farce's main purpose is to be used as a dependency for other libraries. As such, it will most commonly be added as a [runtime dependency](https://guides.rubygems.org/specification-reference/#add_dependency) to your gemspec:
1528
-
1529
- ```ruby
1530
- Gem::Specification.new do |spec|
1531
- # ...
1532
- spec.add_dependency "farce"
1533
- end
1534
- ```
1535
-
1536
- ### Local setup
225
+ For a library, add `spec.add_dependency "farce"` to its gemspec. Load the public entry point with `require "farce"`. Classes load lazily.
1537
226
 
1538
- If you want to work on Farce itself, you can clone the repository and use [mise](https://mise.jdx.dev) to set everything up:
227
+ → [Global installation and local development setup](docs/installation.md).
1539
228
 
1540
- For more details, or if you aren't using mise, check the [contribution guidelines](CONTRIBUTING.md).
1541
-
1542
- ```console
1543
- $ git clone https://github.com/rkh/farce.git # prefix with `jj` if you're using Jujutsu
1544
- $ cd farce
1545
- $ mise run
1546
- ```
1547
-
1548
- ### Loading Farce
1549
- You should always require `farce`, rather than any other files in `lib`. Other files are not intended as entry points.
229
+ ## Compatibility and Dependencies
1550
230
 
1551
- ```ruby
1552
- require "farce"
1553
- ```
231
+ Farce has **no mandatory dependencies beyond Ruby**. It supports maintained CRuby versions, the upcoming CRuby release, and current stable JRuby and TruffleRuby releases.
1554
232
 
1555
- Constants (classes, modules, etc.) under the `Farce` namespace are loaded lazily (thread- and ractor-safe), so there is no
1556
- need to specifically load any particular file.
233
+ → [Support policy, optional gem versions, and related projects](docs/compatibility.md) · [Benchmarks](docs/benchmarks.md).
1557
234
 
1558
235
  ## Known Issues and Limitations
1559
236
 
1560
- ### Possible discrepancy regarding frozen state in Ruby and C
237
+ Ruby's `freeze` and `frozen?` reflect Farce objects' mutability. Direct C-level frozen checks can report a different state for some objects.
1561
238
 
1562
- > I agree that **freezing means the object's own state is immutable**, not just its instance variables, so we should not freeze [*shareable, mutable object*]. Forbidding instance variables on them is the right approach. [...] **A shareable object that is not frozen never has instance variables**. This should also hold when C extensions define such objects in the future.
1563
- > — *Yukihiro Matsumoto* (Ruby Issue [#22291](https://bugs.ruby-lang.org/issues/22291#note-4), emphasis added)
1564
-
1565
- In Ruby, Farce objects reflect their frozen state accurately. If a map returns `true` for `frozen?`, you cannot add, remove, or replace its entries.
1566
-
1567
- There are some technical challenges implementing this behavior: From within Ruby, you cannot mark an object as Ractor shareable without freezing it first. This is possible from a C-extension, but then instance variables can no longer be used, so state tracking needs to happen purely at the C level or outside of the Ruby object.
1568
-
1569
- To work around this, Farce follows a hybrid approach, marking objects with state purely defined in a C extension as Ractor shareable without freezing them, and reimplementing freezing behavior in Ruby for objects where this isn't safely possible.
1570
-
1571
- This means:
1572
-
1573
- * `frozen?` and `freeze` will behave as expected for all Farce classes.
1574
- * `Kernel.instance_method(:frozen?).bind_call(object)` might report a different value from `object.frozen?`
1575
- * C-level checks for frozen state, such as `RB_OBJ_FROZEN`, might differ from what Ruby-level methods report.
239
+ → [Frozen-state behavior for extension authors](docs/compatibility.md#known-issues-and-limitations).
1576
240
 
1577
241
  ## Housekeeping
1578
242
 
243
+ Want to contribute? Read the [contribution guide](CONTRIBUTING.md), [code of conduct](CODE_OF_CONDUCT.md), and [security policy](SECURITY.md).
244
+
1579
245
  Farce follows [Semantic Versioning](https://semver.org/) and [the RubyGems versioning policy](https://guides.rubygems.org/patterns/#versioning). Farce's original code is released under the [MIT License](MIT-LICENSE). Native builds also contain Kazlib 1.20-derived `dict.c` and `dict.h`; their original permissive license and copyright notice are retained in those files. The native gem therefore declares `MIT` and `LicenseRef-Kazlib-1.20`, while the pure-Java gem declares only `MIT` because it does not ship Kazlib.
1580
246
 
1581
247
  Built with love in Berlin, by Konstantin Haase.