farce 0 → 0.1.0
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/CODE_OF_CONDUCT.md +26 -0
- data/CONTRIBUTING.md +71 -0
- data/MIT-LICENSE +20 -0
- data/README.md +1525 -0
- data/SECURITY.md +10 -0
- data/docs/benchmarks.md +185 -0
- data/docs/gems/dry-types.md +290 -0
- data/docs/gems/msgpack.md +70 -0
- data/docs/gems/ractor-shim.md +63 -0
- data/docs/modes.md +628 -0
- data/docs/scopes.md +649 -0
- data/docs/variants.md +346 -0
- data/ext/ext_helper.rb +20 -0
- data/ext/farce/README.md +21 -0
- data/ext/farce/atom.c +1023 -0
- data/ext/farce/bounded_map.c +1683 -0
- data/ext/farce/containers.h +77 -0
- data/ext/farce/counter.c +399 -0
- data/ext/farce/darwin.c +100 -0
- data/ext/farce/depend +12 -0
- data/ext/farce/dict.c +1523 -0
- data/ext/farce/dict.h +152 -0
- data/ext/farce/drivers.c +237 -0
- data/ext/farce/exchanger.c +299 -0
- data/ext/farce/extconf.rb +99 -0
- data/ext/farce/farce.c +359 -0
- data/ext/farce/flag.c +288 -0
- data/ext/farce/io.c +338 -0
- data/ext/farce/lock.c +510 -0
- data/ext/farce/map.c +2240 -0
- data/ext/farce/priority_queue.c +2056 -0
- data/ext/farce/queue.c +1059 -0
- data/ext/farce/reactor.c +820 -0
- data/ext/farce/reactor.h +103 -0
- data/ext/farce/shareable.h +31 -0
- data/ext/farce/signal.c +350 -0
- data/ext/farce/transaction.c +354 -0
- data/ext/farce/transaction.h +40 -0
- data/ext/farce/tree_map.c +1953 -0
- data/ext/farce/trie.c +2020 -0
- data/ext/farce/unshareable.c +155 -0
- data/ext/farce/unshared_io_pool.h +234 -0
- data/ext/farce/unshared_signal.c +263 -0
- data/ext/farce/unshared_wait.h +193 -0
- data/ext/farce/unsupported.c +6 -0
- data/ext/farce/vector.c +1195 -0
- data/ext/farce/weak_map.c +1714 -0
- data/ext/java/org/farce/BoundedMap.java +394 -0
- data/ext/java/org/farce/FiberScheduler.java +141 -0
- data/ext/java/org/farce/PriorityKey.java +45 -0
- data/ext/java/org/farce/PriorityQueue.java +373 -0
- data/ext/java/org/farce/QueueSignal.java +28 -0
- data/ext/rebind/README.md +14 -0
- data/ext/rebind/extconf.rb +10 -0
- data/ext/rebind/rebind.c +186 -0
- data/lib/farce/_yard/internal.rb +13 -0
- data/lib/farce/_yard/macros.rb +61 -0
- data/lib/farce/_yard/ractor.rb +46 -0
- data/lib/farce/abstract/atom.rb +186 -0
- data/lib/farce/abstract/bounded_map.rb +232 -0
- data/lib/farce/abstract/collection.rb +151 -0
- data/lib/farce/abstract/concurrent_map.rb +381 -0
- data/lib/farce/abstract/counter.rb +193 -0
- data/lib/farce/abstract/duplicable_map.rb +229 -0
- data/lib/farce/abstract/exchanger.rb +26 -0
- data/lib/farce/abstract/flag.rb +71 -0
- data/lib/farce/abstract/lazy.rb +115 -0
- data/lib/farce/abstract/lease.rb +104 -0
- data/lib/farce/abstract/lease_map.rb +261 -0
- data/lib/farce/abstract/lease_pool.rb +91 -0
- data/lib/farce/abstract/lfu_map.rb +20 -0
- data/lib/farce/abstract/lru_map.rb +25 -0
- data/lib/farce/abstract/map.rb +345 -0
- data/lib/farce/abstract/molecule.rb +245 -0
- data/lib/farce/abstract/port.rb +74 -0
- data/lib/farce/abstract/priority_queue.rb +117 -0
- data/lib/farce/abstract/queue.rb +269 -0
- data/lib/farce/abstract/scheduler.rb +111 -0
- data/lib/farce/abstract/set.rb +910 -0
- data/lib/farce/abstract/sorted_set.rb +172 -0
- data/lib/farce/abstract/timer_queue.rb +136 -0
- data/lib/farce/abstract/tree_map.rb +269 -0
- data/lib/farce/abstract/value.rb +68 -0
- data/lib/farce/abstract/vector.rb +1126 -0
- data/lib/farce/abstract/weak_atom.rb +32 -0
- data/lib/farce/abstract/weak_key_map.rb +12 -0
- data/lib/farce/abstract/weak_map.rb +13 -0
- data/lib/farce/abstract/weak_set.rb +12 -0
- data/lib/farce/abstract/weak_value_map.rb +12 -0
- data/lib/farce/abstract.rb +17 -0
- data/lib/farce/atom.rb +293 -0
- data/lib/farce/class_mirror.rb +87 -0
- data/lib/farce/clock.rb +121 -0
- data/lib/farce/config.rb +229 -0
- data/lib/farce/counter.rb +71 -0
- data/lib/farce/deduper.rb +122 -0
- data/lib/farce/engine/jruby/bounded_map.rb +314 -0
- data/lib/farce/engine/jruby/fiber_scheduler.jar +0 -0
- data/lib/farce/engine/jruby/fiber_scheduler.rb +119 -0
- data/lib/farce/engine/jruby/lease_waiting.rb +19 -0
- data/lib/farce/engine/jruby/map.rb +505 -0
- data/lib/farce/engine/jruby/mutable_numeric_copy.rb +42 -0
- data/lib/farce/engine/jruby/signal.rb +147 -0
- data/lib/farce/engine/jruby.rb +63 -0
- data/lib/farce/engine/jvm/concurrent_weak_registry.rb +55 -0
- data/lib/farce/engine/jvm/counter.rb +102 -0
- data/lib/farce/engine/jvm/extension.rb +32 -0
- data/lib/farce/engine/jvm/farce.jar +0 -0
- data/lib/farce/engine/jvm/flag.rb +79 -0
- data/lib/farce/engine/jvm/priority_queue.rb +217 -0
- data/lib/farce/engine/jvm/tree_map.rb +350 -0
- data/lib/farce/engine/jvm/types.rb +180 -0
- data/lib/farce/engine/jvm.rb +19 -0
- data/lib/farce/engine/ruby/3.4/fiber_scheduler.rb +20 -0
- data/lib/farce/engine/ruby/3.4/port.rb +186 -0
- data/lib/farce/engine/ruby/3.4/ractor_methods.rb +26 -0
- data/lib/farce/engine/ruby/3.4/ractor_selector.rb +92 -0
- data/lib/farce/engine/ruby/3.4/vault.rb +56 -0
- data/lib/farce/engine/ruby/4.0/port.rb +19 -0
- data/lib/farce/engine/ruby/4.0/ractor_methods.rb +20 -0
- data/lib/farce/engine/ruby/4.0/ractor_selector.rb +108 -0
- data/lib/farce/engine/ruby/4.0/vault.rb +74 -0
- data/lib/farce/engine/ruby/4.1/port.rb +21 -0
- data/lib/farce/engine/ruby/4.1/ractor_methods.rb +22 -0
- data/lib/farce/engine/ruby/4.1/ractor_selector.rb +25 -0
- data/lib/farce/engine/ruby/4.1/vault.rb +5 -0
- data/lib/farce/engine/ruby/fiber_scheduler.rb +32 -0
- data/lib/farce/engine/ruby/key_lock_map.rb +28 -0
- data/lib/farce/engine/ruby/shared/lease.rb +26 -0
- data/lib/farce/engine/ruby/shared/lease_pool.rb +24 -0
- data/lib/farce/engine/ruby/shared/main_scheduler.rb +9 -0
- data/lib/farce/engine/ruby/shared/parallel_scheduler.rb +9 -0
- data/lib/farce/engine/ruby/shared/proxy_owner.rb +31 -0
- data/lib/farce/engine/ruby/shared/ractor_methods.rb +34 -0
- data/lib/farce/engine/ruby/shared/ractor_selector.rb +328 -0
- data/lib/farce/engine/ruby/shared/strict_map.rb +13 -0
- data/lib/farce/engine/ruby/shared/unshared_vector.rb +14 -0
- data/lib/farce/engine/ruby/shared/vault.rb +144 -0
- data/lib/farce/engine/ruby/shared/vault_weak_map.rb +336 -0
- data/lib/farce/engine/ruby/shared/weak_atom.rb +54 -0
- data/lib/farce/engine/ruby/shared/weak_map.rb +413 -0
- data/lib/farce/engine/ruby.rb +130 -0
- data/lib/farce/engine/shared/atom.rb +10 -0
- data/lib/farce/engine/shared/exchanger.rb +130 -0
- data/lib/farce/engine/shared/identity_key.rb +22 -0
- data/lib/farce/engine/shared/lease.rb +10 -0
- data/lib/farce/engine/shared/lease_pool.rb +10 -0
- data/lib/farce/engine/shared/main_scheduler.rb +20 -0
- data/lib/farce/engine/shared/map_key_coordination.rb +226 -0
- data/lib/farce/engine/shared/parallel_scheduler.rb +9 -0
- data/lib/farce/engine/shared/port.rb +44 -0
- data/lib/farce/engine/shared/portable_bounded_map.rb +619 -0
- data/lib/farce/engine/shared/proxy_owner.rb +43 -0
- data/lib/farce/engine/shared/queue.rb +233 -0
- data/lib/farce/engine/shared/ractor_methods.rb +73 -0
- data/lib/farce/engine/shared/rebindable.rb +15 -0
- data/lib/farce/engine/shared/strict_atom.rb +73 -0
- data/lib/farce/engine/shared/strict_map.rb +117 -0
- data/lib/farce/engine/shared/strict_queue_values.rb +27 -0
- data/lib/farce/engine/shared/strict_tree_map.rb +56 -0
- data/lib/farce/engine/shared/transaction_map_backend.rb +20 -0
- data/lib/farce/engine/shared/trie.rb +317 -0
- data/lib/farce/engine/shared/trie_builder.rb +199 -0
- data/lib/farce/engine/shared/unshareable.rb +14 -0
- data/lib/farce/engine/shared/unshared_atom.rb +256 -0
- data/lib/farce/engine/shared/unshared_priority_queue.rb +9 -0
- data/lib/farce/engine/shared/unshared_queue.rb +18 -0
- data/lib/farce/engine/shared/unshared_signal.rb +21 -0
- data/lib/farce/engine/shared/unshared_vector.rb +377 -0
- data/lib/farce/engine/shared/unshared_weak_atom.rb +30 -0
- data/lib/farce/engine/shared/unshared_weak_map.rb +445 -0
- data/lib/farce/engine/shared/vault.rb +31 -0
- data/lib/farce/engine/shared/vector.rb +83 -0
- data/lib/farce/engine/shared/weak_atom/base.rb +189 -0
- data/lib/farce/engine/shared/weak_atom.rb +20 -0
- data/lib/farce/engine/shared/weak_map/cell.rb +99 -0
- data/lib/farce/engine/shared/weak_map/index.rb +249 -0
- data/lib/farce/engine/shared/weak_map/lock.rb +80 -0
- data/lib/farce/engine/shared/weak_map/reference.rb +33 -0
- data/lib/farce/engine/shared.rb +63 -0
- data/lib/farce/engine/truffleruby/fiber_scheduler.rb +13 -0
- data/lib/farce/engine/truffleruby/lock.rb +121 -0
- data/lib/farce/engine/truffleruby/map.rb +656 -0
- data/lib/farce/engine/truffleruby/native/counter.rb +105 -0
- data/lib/farce/engine/truffleruby/native/flag.rb +77 -0
- data/lib/farce/engine/truffleruby/native/ordered_array_support.rb +67 -0
- data/lib/farce/engine/truffleruby/native/priority_queue.rb +388 -0
- data/lib/farce/engine/truffleruby/native/tree_map.rb +51 -0
- data/lib/farce/engine/truffleruby/native/unsafe_tree_map.rb +350 -0
- data/lib/farce/engine/truffleruby/signal.rb +77 -0
- data/lib/farce/engine/truffleruby.rb +148 -0
- data/lib/farce/envelope.rb +335 -0
- data/lib/farce/error.rb +35 -0
- data/lib/farce/exchanger.rb +41 -0
- data/lib/farce/flag.rb +27 -0
- data/lib/farce/integrations/active_support/blank.rb +68 -0
- data/lib/farce/integrations/active_support/clock.rb +17 -0
- data/lib/farce/integrations/active_support/duplicable.rb +78 -0
- data/lib/farce/integrations/active_support/map.rb +175 -0
- data/lib/farce/integrations/active_support/set.rb +60 -0
- data/lib/farce/integrations/active_support/value_serialization.rb +17 -0
- data/lib/farce/integrations/active_support/vector.rb +231 -0
- data/lib/farce/integrations/active_support.rb +9 -0
- data/lib/farce/integrations/activesupport.rb +5 -0
- data/lib/farce/integrations/bson.rb +108 -0
- data/lib/farce/integrations/cbor.rb +51 -0
- data/lib/farce/integrations/concurrent.rb +112 -0
- data/lib/farce/integrations/dry-types.rb +5 -0
- data/lib/farce/integrations/dry_types.rb +694 -0
- data/lib/farce/integrations/json.rb +7 -0
- data/lib/farce/integrations/msgpack.rb +112 -0
- data/lib/farce/integrations/oj.rb +69 -0
- data/lib/farce/integrations/psych.rb +191 -0
- data/lib/farce/integrations/ractor-sharing.rb +5 -0
- data/lib/farce/integrations/ractor-tmvar.rb +5 -0
- data/lib/farce/integrations/ractor_sharing.rb +172 -0
- data/lib/farce/integrations/ractor_tmvar.rb +64 -0
- data/lib/farce/integrations/shared/to_json.rb +42 -0
- data/lib/farce/integrations/sorted_set.rb +11 -0
- data/lib/farce/integrations/weakref.rb +38 -0
- data/lib/farce/integrations/yajl.rb +10 -0
- data/lib/farce/integrations.rb +156 -0
- data/lib/farce/internal/_frozen_config.rb +11 -0
- data/lib/farce/internal/autoloads.rb +47 -0
- data/lib/farce/internal/blocking_priority_queue.rb +122 -0
- data/lib/farce/internal/converter.rb +106 -0
- data/lib/farce/internal/copyable.rb +31 -0
- data/lib/farce/internal/delegation.rb +20 -0
- data/lib/farce/internal/external_transaction.rb +59 -0
- data/lib/farce/internal/fake_ractor.rb +179 -0
- data/lib/farce/internal/freeze.rb +118 -0
- data/lib/farce/internal/inspect.rb +157 -0
- data/lib/farce/internal/key_lock_map.rb +29 -0
- data/lib/farce/internal/key_normalizer.rb +289 -0
- data/lib/farce/internal/lease_initialization.rb +115 -0
- data/lib/farce/internal/lease_map.rb +441 -0
- data/lib/farce/internal/lease_pool_state.rb +228 -0
- data/lib/farce/internal/lease_state.rb +342 -0
- data/lib/farce/internal/lease_waiting.rb +13 -0
- data/lib/farce/internal/managed_queue.rb +26 -0
- data/lib/farce/internal/map_value_modes.rb +227 -0
- data/lib/farce/internal/marshal_support.rb +227 -0
- data/lib/farce/internal/mixin.rb +20 -0
- data/lib/farce/internal/mutable_ordered_key_lock_map.rb +63 -0
- data/lib/farce/internal/noncopyable.rb +17 -0
- data/lib/farce/internal/ordered_key_lock_map.rb +69 -0
- data/lib/farce/internal/pool_supervisor.rb +41 -0
- data/lib/farce/internal/pool_worker.rb +61 -0
- data/lib/farce/internal/portable_transaction/reservation_entry.rb +29 -0
- data/lib/farce/internal/portable_transaction/strong_map_entry.rb +137 -0
- data/lib/farce/internal/portable_transaction/strong_map_size_entry.rb +44 -0
- data/lib/farce/internal/portable_transaction/tree_entry.rb +64 -0
- data/lib/farce/internal/portable_transaction.rb +164 -0
- data/lib/farce/internal/proxy_owner_notifications.rb +29 -0
- data/lib/farce/internal/reservation_waiting.rb +24 -0
- data/lib/farce/internal/scheduled_task.rb +48 -0
- data/lib/farce/internal/scheduler_io.rb +171 -0
- data/lib/farce/internal/scheduler_lifecycle.rb +314 -0
- data/lib/farce/internal/select_scheduler.rb +235 -0
- data/lib/farce/internal/storage.rb +162 -0
- data/lib/farce/internal/strict_lease.rb +30 -0
- data/lib/farce/internal/strict_lease_map.rb +20 -0
- data/lib/farce/internal/strict_lease_pool.rb +29 -0
- data/lib/farce/internal/thread_pool.rb +166 -0
- data/lib/farce/internal/transaction_conflict.rb +9 -0
- data/lib/farce/internal/transaction_freeze_guard.rb +30 -0
- data/lib/farce/internal/transaction_map_snapshot.rb +123 -0
- data/lib/farce/internal/undefined.rb +22 -0
- data/lib/farce/internal/unshared_lease.rb +29 -0
- data/lib/farce/internal/unshared_lease_pool.rb +34 -0
- data/lib/farce/internal/unshared_queue_waiting.rb +28 -0
- data/lib/farce/internal/value_serialization.rb +13 -0
- data/lib/farce/internal/weak_map_value_modes.rb +58 -0
- data/lib/farce/internal/weak_mode_manager.rb +26 -0
- data/lib/farce/internal.rb +129 -0
- data/lib/farce/lazy.rb +100 -0
- data/lib/farce/lazy_ref.rb +40 -0
- data/lib/farce/lease.rb +28 -0
- data/lib/farce/lease_map.rb +34 -0
- data/lib/farce/lease_pool.rb +29 -0
- data/lib/farce/lfu_map.rb +53 -0
- data/lib/farce/local/atom.rb +23 -0
- data/lib/farce/local/counter.rb +88 -0
- data/lib/farce/local/flag.rb +65 -0
- data/lib/farce/local/lazy.rb +38 -0
- data/lib/farce/local/lazy_ref.rb +36 -0
- data/lib/farce/local/lease.rb +77 -0
- data/lib/farce/local/lease_map.rb +80 -0
- data/lib/farce/local/lease_pool.rb +38 -0
- data/lib/farce/local/lfu_map.rb +55 -0
- data/lib/farce/local/lru_map.rb +62 -0
- data/lib/farce/local/map.rb +47 -0
- data/lib/farce/local/molecule.rb +45 -0
- data/lib/farce/local/priority_queue.rb +53 -0
- data/lib/farce/local/queue.rb +32 -0
- data/lib/farce/local/scoped.rb +166 -0
- data/lib/farce/local/set.rb +18 -0
- data/lib/farce/local/sorted_set.rb +50 -0
- data/lib/farce/local/timer_queue.rb +41 -0
- data/lib/farce/local/tree_map.rb +47 -0
- data/lib/farce/local/vector.rb +30 -0
- data/lib/farce/local/weak_atom.rb +23 -0
- data/lib/farce/local/weak_key_map.rb +39 -0
- data/lib/farce/local/weak_map.rb +39 -0
- data/lib/farce/local/weak_set.rb +18 -0
- data/lib/farce/local/weak_value_map.rb +39 -0
- data/lib/farce/local.rb +35 -0
- data/lib/farce/lock.rb +54 -0
- data/lib/farce/lru_map.rb +63 -0
- data/lib/farce/map.rb +58 -0
- data/lib/farce/mode_manager.rb +142 -0
- data/lib/farce/molecule.rb +60 -0
- data/lib/farce/mutable.rb +171 -0
- data/lib/farce/pool.rb +332 -0
- data/lib/farce/port.rb +134 -0
- data/lib/farce/priority_queue.rb +70 -0
- data/lib/farce/proxy/register.rb +101 -0
- data/lib/farce/proxy/supervisor.rb +137 -0
- data/lib/farce/proxy/wrapper.rb +49 -0
- data/lib/farce/proxy.rb +291 -0
- data/lib/farce/queue.rb +60 -0
- data/lib/farce/ractor.rb +269 -0
- data/lib/farce/read_write_lock.rb +256 -0
- data/lib/farce/reference.rb +158 -0
- data/lib/farce/resolv/dns.rb +42 -0
- data/lib/farce/resolv.rb +85 -0
- data/lib/farce/scheduler.rb +536 -0
- data/lib/farce/set.rb +39 -0
- data/lib/farce/shareable.rb +150 -0
- data/lib/farce/signal.rb +97 -0
- data/lib/farce/sorted_set.rb +36 -0
- data/lib/farce/strict/atom.rb +34 -0
- data/lib/farce/strict/counter.rb +10 -0
- data/lib/farce/strict/exchanger.rb +25 -0
- data/lib/farce/strict/flag.rb +10 -0
- data/lib/farce/strict/lazy.rb +26 -0
- data/lib/farce/strict/lazy_ref.rb +26 -0
- data/lib/farce/strict/lease.rb +27 -0
- data/lib/farce/strict/lease_map.rb +33 -0
- data/lib/farce/strict/lease_pool.rb +28 -0
- data/lib/farce/strict/lfu_map.rb +27 -0
- data/lib/farce/strict/lru_map.rb +28 -0
- data/lib/farce/strict/map.rb +54 -0
- data/lib/farce/strict/molecule.rb +16 -0
- data/lib/farce/strict/port.rb +40 -0
- data/lib/farce/strict/priority_queue.rb +20 -0
- data/lib/farce/strict/queue.rb +17 -0
- data/lib/farce/strict/set.rb +14 -0
- data/lib/farce/strict/sorted_set.rb +19 -0
- data/lib/farce/strict/timer_queue.rb +13 -0
- data/lib/farce/strict/tree_map.rb +27 -0
- data/lib/farce/strict/vector.rb +24 -0
- data/lib/farce/strict/weak_atom.rb +31 -0
- data/lib/farce/strict/weak_key_map.rb +49 -0
- data/lib/farce/strict/weak_map.rb +49 -0
- data/lib/farce/strict/weak_set.rb +14 -0
- data/lib/farce/strict/weak_value_map.rb +48 -0
- data/lib/farce/strict.rb +28 -0
- data/lib/farce/system.rb +40 -0
- data/lib/farce/thread_scheduler.rb +70 -0
- data/lib/farce/timer_queue.rb +66 -0
- data/lib/farce/transaction/atom.rb +73 -0
- data/lib/farce/transaction/map.rb +25 -0
- data/lib/farce/transaction/map_operations.rb +143 -0
- data/lib/farce/transaction/molecule.rb +90 -0
- data/lib/farce/transaction/mutable.rb +61 -0
- data/lib/farce/transaction/set.rb +17 -0
- data/lib/farce/transaction/set_operations.rb +48 -0
- data/lib/farce/transaction/sorted_set.rb +18 -0
- data/lib/farce/transaction/tree_map.rb +78 -0
- data/lib/farce/transaction/vector.rb +139 -0
- data/lib/farce/transaction/wrapper.rb +202 -0
- data/lib/farce/transaction.rb +343 -0
- data/lib/farce/tree_map.rb +56 -0
- data/lib/farce/unsafe/lfu_map.rb +36 -0
- data/lib/farce/unsafe/lru_map.rb +36 -0
- data/lib/farce/unsafe/tree_map.rb +21 -0
- data/lib/farce/unsafe.rb +29 -0
- data/lib/farce/unshareable.rb +67 -0
- data/lib/farce/unshared/atom.rb +24 -0
- data/lib/farce/unshared/counter.rb +10 -0
- data/lib/farce/unshared/flag.rb +10 -0
- data/lib/farce/unshared/lazy.rb +38 -0
- data/lib/farce/unshared/lazy_ref.rb +26 -0
- data/lib/farce/unshared/lease.rb +27 -0
- data/lib/farce/unshared/lease_map.rb +26 -0
- data/lib/farce/unshared/lease_pool.rb +25 -0
- data/lib/farce/unshared/lfu_map.rb +20 -0
- data/lib/farce/unshared/lru_map.rb +20 -0
- data/lib/farce/unshared/map.rb +49 -0
- data/lib/farce/unshared/molecule.rb +16 -0
- data/lib/farce/unshared/priority_queue.rb +16 -0
- data/lib/farce/unshared/queue.rb +27 -0
- data/lib/farce/unshared/set.rb +14 -0
- data/lib/farce/unshared/sorted_set.rb +28 -0
- data/lib/farce/unshared/timer_queue.rb +16 -0
- data/lib/farce/unshared/tree_map.rb +21 -0
- data/lib/farce/unshared/vector.rb +19 -0
- data/lib/farce/unshared/weak_atom.rb +31 -0
- data/lib/farce/unshared/weak_key_map.rb +44 -0
- data/lib/farce/unshared/weak_map.rb +44 -0
- data/lib/farce/unshared/weak_set.rb +14 -0
- data/lib/farce/unshared/weak_value_map.rb +43 -0
- data/lib/farce/unshared.rb +28 -0
- data/lib/farce/vector.rb +228 -0
- data/lib/farce/version.rb +8 -0
- data/lib/farce/walker/definitions.rb +186 -0
- data/lib/farce/walker/modification.rb +355 -0
- data/lib/farce/walker.rb +319 -0
- data/lib/farce/weak_atom.rb +121 -0
- data/lib/farce/weak_key_map.rb +31 -0
- data/lib/farce/weak_map.rb +33 -0
- data/lib/farce/weak_ref.rb +67 -0
- data/lib/farce/weak_set.rb +82 -0
- data/lib/farce/weak_value.rb +195 -0
- data/lib/farce/weak_value_map.rb +32 -0
- data/lib/farce.rb +519 -0
- metadata +435 -8
data/README.md
ADDED
|
@@ -0,0 +1,1525 @@
|
|
|
1
|
+
# Farce: Fiber and Ractor Compatibility Enabler
|
|
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.
|
|
4
|
+
|
|
5
|
+
**Here's a taste:**
|
|
6
|
+
|
|
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
|
|
17
|
+
|
|
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}")
|
|
46
|
+
end
|
|
47
|
+
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
|
+
- [Similar Projects](#similar-projects)
|
|
137
|
+
- [Installation](#installation)
|
|
138
|
+
- [Globally](#globally)
|
|
139
|
+
- [As a project dependency](#as-a-project-dependency)
|
|
140
|
+
- [As a library dependency](#as-a-library-dependency)
|
|
141
|
+
- [Local setup](#local-setup)
|
|
142
|
+
- [Loading Farce](#loading-farce)
|
|
143
|
+
- [Known Issues and Limitations](#known-issues-and-limitations)
|
|
144
|
+
- [Possible discrepancy regarding frozen state in Ruby and C](#possible-discrepancy-regarding-frozen-state-in-ruby-and-c)
|
|
145
|
+
- [Housekeeping](#housekeeping)
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
## Introduction
|
|
149
|
+
|
|
150
|
+
Ruby 3.0 introduced two powerful concurrency primitives: **Fiber schedulers** and **Ractors**, besides the already existing **Threads**:
|
|
151
|
+
|
|
152
|
+
* **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.
|
|
153
|
+
* **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.
|
|
154
|
+
* **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.
|
|
155
|
+
|
|
156
|
+
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).
|
|
157
|
+
|
|
158
|
+
### The Problem
|
|
159
|
+
|
|
160
|
+
> [!NOTE]
|
|
161
|
+
> 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.
|
|
162
|
+
|
|
163
|
+
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:
|
|
164
|
+
|
|
165
|
+
<table>
|
|
166
|
+
<thead>
|
|
167
|
+
<tr>
|
|
168
|
+
<th>Library</th>
|
|
169
|
+
<th>Example classes</th>
|
|
170
|
+
<th>Multithreading</th>
|
|
171
|
+
<th>Non-blocking Fibers</th>
|
|
172
|
+
<th>Cross-Ractor Usage</th>
|
|
173
|
+
</tr>
|
|
174
|
+
</thead>
|
|
175
|
+
<tbody>
|
|
176
|
+
<tr>
|
|
177
|
+
<td rowspan="5"><a href="https://www.ruby-lang.org/">Ruby</a></td>
|
|
178
|
+
<td>Array, Hash, String, Set, …</td>
|
|
179
|
+
<td>⚠️ not thread-safe</td>
|
|
180
|
+
<td>✅ supported</td>
|
|
181
|
+
<td>⚠️ immutable/copy/move only</td>
|
|
182
|
+
</tr>
|
|
183
|
+
<tr>
|
|
184
|
+
<td>Thread, Fiber</td>
|
|
185
|
+
<td>✅ supported</td>
|
|
186
|
+
<td>✅ supported</td>
|
|
187
|
+
<td>❌ <b>not supported</b></td>
|
|
188
|
+
</tr>
|
|
189
|
+
<tr>
|
|
190
|
+
<td>Mutex, ConditionVariable, Queue</td>
|
|
191
|
+
<td>✅ supported</td>
|
|
192
|
+
<td>⚠️ CRuby only</td>
|
|
193
|
+
<td>❌ <b>not supported</b></td>
|
|
194
|
+
</tr>
|
|
195
|
+
<tr>
|
|
196
|
+
<td>Ractor, Ractor::Port</td>
|
|
197
|
+
<td>✅ supported</td>
|
|
198
|
+
<td>❌ <b>not supported</b></td>
|
|
199
|
+
<td>✅ supported</td>
|
|
200
|
+
</tr>
|
|
201
|
+
<tr>
|
|
202
|
+
<td>WeakRef, Ruby::Box</td>
|
|
203
|
+
<td>⚠️ Main Ractor only</td>
|
|
204
|
+
<td>⚠️ Main Ractor only</td>
|
|
205
|
+
<td>❌ <b>not supported</b></td>
|
|
206
|
+
</tr>
|
|
207
|
+
<tr>
|
|
208
|
+
<td><a href="https://github.com/ruby-concurrency/concurrent-ruby">concurrent-ruby</a></td>
|
|
209
|
+
<td>Future, Exchanger, Tuple, …</td>
|
|
210
|
+
<td>⚠️ Main Ractor only</td>
|
|
211
|
+
<td>⚠️ Main Ractor only</td>
|
|
212
|
+
<td>❌ <b>not supported</b></td>
|
|
213
|
+
</tr>
|
|
214
|
+
<tr>
|
|
215
|
+
<td rowspan="2"><a href="https://github.com/socketry/async">async</a></td>
|
|
216
|
+
<td>Promise, Condition, Queue, …</td>
|
|
217
|
+
<td>✅ supported</td>
|
|
218
|
+
<td>✅ supported</td>
|
|
219
|
+
<td>❌ <b>not supported</b></td>
|
|
220
|
+
</tr>
|
|
221
|
+
<tr>
|
|
222
|
+
<td>Scheduler</td>
|
|
223
|
+
<td>⚠️ Main Ractor only</td>
|
|
224
|
+
<td>⚠️ Main Ractor only</td>
|
|
225
|
+
<td>❌ <b>not supported</b></td>
|
|
226
|
+
</tr>
|
|
227
|
+
<tr>
|
|
228
|
+
<td rowspan="3"><a href="https://github.com/mperham/ratomic">ratomic</a></td>
|
|
229
|
+
<td>Pool</td>
|
|
230
|
+
<td>⚠️ CRuby only<b>¹</b></td>
|
|
231
|
+
<td>❌ <b>not supported</b></td>
|
|
232
|
+
<td>⚠️ CRuby only<b>¹</b></td>
|
|
233
|
+
</tr>
|
|
234
|
+
<tr>
|
|
235
|
+
<td>LocalPool</td>
|
|
236
|
+
<td>⚠️ CRuby only<b>¹</b></td>
|
|
237
|
+
<td>⚠️ CRuby only<b>¹</b></td>
|
|
238
|
+
<td>⚠️ CRuby only<b>¹</b></td>
|
|
239
|
+
</tr>
|
|
240
|
+
<tr>
|
|
241
|
+
<td>Queue, Map</td>
|
|
242
|
+
<td>⚠️ CRuby only<b>¹</b></td>
|
|
243
|
+
<td>❌ <b>not supported</b></td>
|
|
244
|
+
<td>💣 <b>breaks isolation¹²</b></td>
|
|
245
|
+
</tr>
|
|
246
|
+
<tr>
|
|
247
|
+
<td><a href="https://github.com/MadBomber/ractor_queue">ractor_queue</a></td>
|
|
248
|
+
<td>RactorQueue</td>
|
|
249
|
+
<td>⚠️ CRuby only<b>¹</b></td>
|
|
250
|
+
<td>⚠️ CRuby only<b>¹</b></td>
|
|
251
|
+
<td>💣 <b>breaks isolation¹²</b></td>
|
|
252
|
+
</tr>
|
|
253
|
+
<tr>
|
|
254
|
+
<td>
|
|
255
|
+
<a href="https://github.com/hamstergem/hamster">hamster</a> /
|
|
256
|
+
<a href="https://github.com/immutable-ruby/immutable-ruby">immutable</a>
|
|
257
|
+
</td>
|
|
258
|
+
<td>Hash, Vector, Set, List, …</td>
|
|
259
|
+
<td>⚠️ Main Ractor only</td>
|
|
260
|
+
<td>⚠️ Main Ractor only</td>
|
|
261
|
+
<td>❌ <b>not supported</b></td>
|
|
262
|
+
</tr>
|
|
263
|
+
<tr>
|
|
264
|
+
<td><a href="https://github.com/ko1/ractor-sharing">ractor-sharing</a></td>
|
|
265
|
+
<td>TVar, LockVar, LockHash, …</td>
|
|
266
|
+
<td>⚠️ CRuby only<b>¹</b></td>
|
|
267
|
+
<td>❌ <b>not supported</b></td>
|
|
268
|
+
<td>⚠️ CRuby only<b>¹</b></td>
|
|
269
|
+
</tr>
|
|
270
|
+
<tr>
|
|
271
|
+
<td rowspan="2"><a href="https://github.com/jhawthorn/ractor_safe">ractor_safe</a></td>
|
|
272
|
+
<td>HashMap, AtomicInteger</td>
|
|
273
|
+
<td>⚠️ CRuby only</td>
|
|
274
|
+
<td>⚠️ CRuby only</td>
|
|
275
|
+
<td>✅ supported</td>
|
|
276
|
+
</tr>
|
|
277
|
+
<tr>
|
|
278
|
+
<td>Queue</td>
|
|
279
|
+
<td>⚠️ CRuby only</td>
|
|
280
|
+
<td>❌ <b>not supported</b></td>
|
|
281
|
+
<td>✅ supported</td>
|
|
282
|
+
</tr>
|
|
283
|
+
</tbody>
|
|
284
|
+
</table>
|
|
285
|
+
|
|
286
|
+
Notes:
|
|
287
|
+
1. Incorrectly flags mutable objects as frozen.
|
|
288
|
+
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.
|
|
289
|
+
|
|
290
|
+
### The Solution
|
|
291
|
+
|
|
292
|
+
**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!
|
|
293
|
+
|
|
294
|
+
And it does so in a non-invasive way. You should be able to use Farce alongside any other gem!
|
|
295
|
+
|
|
296
|
+
```ruby
|
|
297
|
+
# Using a counter as an example, there are many more classes provided by Farce
|
|
298
|
+
counter = Farce::Counter.new
|
|
299
|
+
|
|
300
|
+
# Works in code without concurrency
|
|
301
|
+
counter.add(5)
|
|
302
|
+
counter.to_i # => 5
|
|
303
|
+
|
|
304
|
+
# Works with threads
|
|
305
|
+
wait_for = 5.times.map do
|
|
306
|
+
Thread.new { counter.increment }
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
# Works with ractors
|
|
310
|
+
wait_for += 5.times.map do
|
|
311
|
+
Ractor.new(counter) { it.increment }
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
# Works with the async gem
|
|
315
|
+
Async do
|
|
316
|
+
5.times do
|
|
317
|
+
Async { counter.increment }
|
|
318
|
+
end
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
wait_for.each(&:join)
|
|
322
|
+
counter.to_i # => 20
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
Farce also aims to be fast and efficient, with performance ranging from minimal overhead to outperforming other options.
|
|
326
|
+
|
|
327
|
+
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.
|
|
328
|
+
|
|
329
|
+
This is especially useful when coordinating work between Fiber schedulers and Ractors:
|
|
330
|
+
|
|
331
|
+
```ruby
|
|
332
|
+
queue = Farce::Queue.new
|
|
333
|
+
|
|
334
|
+
# Background ractor pushing work into the queue
|
|
335
|
+
# This would not work with Ruby's built-in Queue
|
|
336
|
+
Ractor.new(queue) do |queue|
|
|
337
|
+
loop { queue.push expensive_work }
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
# This would not work with Ratomic::Queue
|
|
341
|
+
Async do
|
|
342
|
+
# Task waiting for work from the queue
|
|
343
|
+
Async { loop { do_something queue.pop } }
|
|
344
|
+
|
|
345
|
+
# Doesn't get blocked by the other task waiting for the queue
|
|
346
|
+
Async { unrelated_work }
|
|
347
|
+
end
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
## Features
|
|
351
|
+
|
|
352
|
+
### Data Structures
|
|
353
|
+
|
|
354
|
+
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.
|
|
355
|
+
|
|
356
|
+
#### Maps
|
|
357
|
+
|
|
358
|
+
Maps are Hash-like key-value data structures. They do not preserve insertion order.
|
|
359
|
+
|
|
360
|
+
They implement almost all methods Ruby's Hash offers:
|
|
361
|
+
|
|
362
|
+
```ruby
|
|
363
|
+
map = Farce::Map.new
|
|
364
|
+
map[:foo] = :bar
|
|
365
|
+
|
|
366
|
+
map.merge! answer: 42
|
|
367
|
+
map.transform_values! { -it.to_s }
|
|
368
|
+
map.to_h # => {answer: "42", foo: "bar"}
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
In addition, they come with a range of [atomic operations](#atomic-operations), and have built-in key normalization support:
|
|
372
|
+
|
|
373
|
+
```ruby
|
|
374
|
+
# normalize_keys can be a symbol, proc, hash, or another map
|
|
375
|
+
map = Farce::Map.new({ a: 10 }, normalize_keys: :to_s)
|
|
376
|
+
|
|
377
|
+
# Atomic upsert operation
|
|
378
|
+
2.times { map.upsert(:b, 42) { it * map[:a] } }
|
|
379
|
+
|
|
380
|
+
# Keys have been converted to strings
|
|
381
|
+
map.keys.sort # => ["a", "b"]
|
|
382
|
+
```
|
|
383
|
+
|
|
384
|
+
Besides the standard map implementation, Farce includes a range of specialized maps:
|
|
385
|
+
|
|
386
|
+
* `LRUMap` will evict the least recently used entry to not grow beyond the maximum size.
|
|
387
|
+
* `LFUMap` will evict the least frequently used entry to not grow beyond the maximum size.
|
|
388
|
+
* `LeaseMap` manages [leases](#leases) associated with known keys.
|
|
389
|
+
* `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.
|
|
390
|
+
* `WeakMap` only holds weak references to its keys and values, automatically removing entries when the corresponding key or value gets garbage collected.
|
|
391
|
+
* `WeakKeyMap` only holds weak references to its keys, automatically removing entries when the corresponding key gets garbage collected.
|
|
392
|
+
* `WeakValueMap` only holds weak references to its values, automatically removing entries when the corresponding value gets garbage collected.
|
|
393
|
+
|
|
394
|
+
For example, bounded maps are useful for implementing caches:
|
|
395
|
+
|
|
396
|
+
```ruby
|
|
397
|
+
cache = Farce::LRUMap.new(max_size: 2)
|
|
398
|
+
|
|
399
|
+
cache[:first] = 1
|
|
400
|
+
cache[:second] = 2
|
|
401
|
+
cache[:first] # makes sure :first was accessed more recently than :second
|
|
402
|
+
cache[:third] = 3
|
|
403
|
+
|
|
404
|
+
cache.key?(:second) # => false
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
#### Vectors
|
|
408
|
+
|
|
409
|
+
`Farce::Vector` is to `Array` what `Farce::Map` is to `Hash`. It implements the same interface, with additional atomic operations.
|
|
410
|
+
|
|
411
|
+
```ruby
|
|
412
|
+
list = Farce::Vector.new
|
|
413
|
+
10.times { list << it }
|
|
414
|
+
list.to_a # => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
415
|
+
list.sum # => 45
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
#### Sets
|
|
419
|
+
|
|
420
|
+
`Farce::Set` is to `Set` what `Farce::Map` is to `Hash`. It implements the same interface, with additional atomic operations.
|
|
421
|
+
|
|
422
|
+
```ruby
|
|
423
|
+
jobs = Farce::Set[:compile, :test]
|
|
424
|
+
jobs.add? :publish # => jobs
|
|
425
|
+
jobs.add? :test # => nil
|
|
426
|
+
job.include? :publish # => true
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
Besides the standard set implementation, Farce includes two specialized sets:
|
|
430
|
+
|
|
431
|
+
* `SortedSet` keeps its entries in order based on value comparison.
|
|
432
|
+
* `WeakSet` only holds weak references to its entries.
|
|
433
|
+
|
|
434
|
+
#### Atoms
|
|
435
|
+
|
|
436
|
+
Atoms are value containers. These store a single reference to any Ruby object, and expose atomic operations for mutating these values:
|
|
437
|
+
|
|
438
|
+
```ruby
|
|
439
|
+
atom = Farce::Atom.new("initial value")
|
|
440
|
+
atom.value # => "initial value"
|
|
441
|
+
|
|
442
|
+
# atoms are ractor-shareable
|
|
443
|
+
Ractor.new(atom) do |atom|
|
|
444
|
+
atom.update { |current| current + " updated" } # => "initial value updated"
|
|
445
|
+
end
|
|
446
|
+
|
|
447
|
+
# waits for the other ractor to perform its update
|
|
448
|
+
atom.wait_until_changed "initial value"
|
|
449
|
+
```
|
|
450
|
+
|
|
451
|
+
`WeakAtom` only keeps a weak reference for its current value, reverting to `nil` when the current value gets garbage collected.
|
|
452
|
+
|
|
453
|
+
#### Counters
|
|
454
|
+
|
|
455
|
+
Ractor-shareable, atomic counter. This is a stateful, numeric object.
|
|
456
|
+
|
|
457
|
+
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.
|
|
458
|
+
|
|
459
|
+
```ruby
|
|
460
|
+
counter = Farce::Counter.new
|
|
461
|
+
counter.value # => 0
|
|
462
|
+
|
|
463
|
+
# Increase the counter by 1
|
|
464
|
+
counter.increment
|
|
465
|
+
counter.value # => 1
|
|
466
|
+
|
|
467
|
+
# Increase the counter by 5 on another Ractor
|
|
468
|
+
Ractor.new(counter) { it.add(5) }
|
|
469
|
+
|
|
470
|
+
# Give the other ractor time to run
|
|
471
|
+
sleep 0.1
|
|
472
|
+
|
|
473
|
+
counter.value # => 6
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
#### Flags
|
|
477
|
+
|
|
478
|
+
Ractor-shareable, atomic boolean.
|
|
479
|
+
|
|
480
|
+
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.
|
|
481
|
+
|
|
482
|
+
```ruby
|
|
483
|
+
flag = Farce::Flag.new(true)
|
|
484
|
+
flag.value # => true
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
#### References
|
|
488
|
+
|
|
489
|
+
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:
|
|
490
|
+
|
|
491
|
+
```ruby
|
|
492
|
+
atom = Farce::Atom.new(42)
|
|
493
|
+
ref = Farce::Reference.new(atom)
|
|
494
|
+
|
|
495
|
+
ref.to_s # => "42"
|
|
496
|
+
ref > 10 # => true
|
|
497
|
+
|
|
498
|
+
atom.value = :foo
|
|
499
|
+
ref.to_s # => "foo"
|
|
500
|
+
```
|
|
501
|
+
|
|
502
|
+
#### Molecules
|
|
503
|
+
|
|
504
|
+
Molecules are structures made up of multiple atoms. Clever naming, right?
|
|
505
|
+
|
|
506
|
+
They are similar to Ruby's built-in `Struct` and `Data` classes, but their members are stored in atoms:
|
|
507
|
+
|
|
508
|
+
```ruby
|
|
509
|
+
Request = Farce::Molecule.define(:verb, :path)
|
|
510
|
+
request = Request.new("GET", "/index.html")
|
|
511
|
+
request.verb # => "GET"
|
|
512
|
+
request.verb = "POST"
|
|
513
|
+
|
|
514
|
+
# This doesn't succeed, as the verb isn't "HEAD"
|
|
515
|
+
request.verb_atom.compare_and_set("HEAD", "GET")
|
|
516
|
+
request.verb # => "POST"
|
|
517
|
+
```
|
|
518
|
+
|
|
519
|
+
You can also pass a block to add additional methods:
|
|
520
|
+
|
|
521
|
+
```ruby
|
|
522
|
+
Request = Farce::Molecule.define(:verb, :path) do
|
|
523
|
+
def to_s = "#{verb} #{path}"
|
|
524
|
+
end
|
|
525
|
+
|
|
526
|
+
Request.new(verb: "GET", path: "/").to_s # => "GET /"
|
|
527
|
+
```
|
|
528
|
+
|
|
529
|
+
You can also inherit from the generated class:
|
|
530
|
+
|
|
531
|
+
```ruby
|
|
532
|
+
class Request < Farce::Molecule.define(:verb, :path)
|
|
533
|
+
def to_s = "#{verb} #{path}"
|
|
534
|
+
end
|
|
535
|
+
```
|
|
536
|
+
|
|
537
|
+
#### Queues
|
|
538
|
+
|
|
539
|
+
`Farce::Queue` is a drop-in replacement for Ruby's `Queue` and `SizedQueue`. It provides a blocking and a non-blocking API.
|
|
540
|
+
|
|
541
|
+
```ruby
|
|
542
|
+
queue = Farce::Queue.new
|
|
543
|
+
queue.push(:ok)
|
|
544
|
+
queue.pop # => :ok
|
|
545
|
+
queue.try_pop # => nil
|
|
546
|
+
```
|
|
547
|
+
|
|
548
|
+
Queues provide additional features over Ruby's queue:
|
|
549
|
+
* Like the rest of Farce, they work with Ractors (implementing [sharing modes](#sharing-modes) for their [default variant](#variants-and-scopes)).
|
|
550
|
+
* In addition to closing, they allow sealing, which prevents pushes but still allows pulling from the queue until it has been drained.
|
|
551
|
+
* 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.
|
|
552
|
+
* 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).
|
|
553
|
+
|
|
554
|
+
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).
|
|
555
|
+
|
|
556
|
+
##### Priority Queues
|
|
557
|
+
|
|
558
|
+
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:
|
|
559
|
+
|
|
560
|
+
```ruby
|
|
561
|
+
# use order: :descending to reverse priorities
|
|
562
|
+
queue = Farce::PriorityQueue.new
|
|
563
|
+
|
|
564
|
+
queue.push :a, priority: 2
|
|
565
|
+
queue.push :b, priority: 1
|
|
566
|
+
|
|
567
|
+
queue.pop # => :b
|
|
568
|
+
```
|
|
569
|
+
|
|
570
|
+
##### Timer Queues
|
|
571
|
+
|
|
572
|
+
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 `pull` will block until the next timestamp has been reached.
|
|
573
|
+
|
|
574
|
+
```ruby
|
|
575
|
+
queue = Farce::TimerQueue.new
|
|
576
|
+
queue.push(:ok, in: 1.5)
|
|
577
|
+
|
|
578
|
+
# blocks for ~1.5 seconds
|
|
579
|
+
queue.pop # => :ok
|
|
580
|
+
```
|
|
581
|
+
|
|
582
|
+
Timer queues are useful for implementing schedulers (which is what Farce uses them for under the hood), so they have one additional feature: You can delete an entry if you know its timestamp.
|
|
583
|
+
|
|
584
|
+
```ruby
|
|
585
|
+
queue = Farce::TimerQueue.new
|
|
586
|
+
timestamp = Farce.clock(in: 0.2)
|
|
587
|
+
|
|
588
|
+
queue.push(:first, at: timestamp - 0.1)
|
|
589
|
+
queue.push(:second, at: timestamp)
|
|
590
|
+
queue.push(:third, at: timestamp + 0.1)
|
|
591
|
+
|
|
592
|
+
queue.delete(:second, at: timestamp)
|
|
593
|
+
|
|
594
|
+
result = []
|
|
595
|
+
result << queue.pop until queue.empty?
|
|
596
|
+
result # => [:first, :third]
|
|
597
|
+
```
|
|
598
|
+
|
|
599
|
+
##### Queue capacity
|
|
600
|
+
|
|
601
|
+
Queues support setting a maximum capacity. If the capacity has been reached it will block any pushes to create back pressure.
|
|
602
|
+
|
|
603
|
+
```ruby
|
|
604
|
+
queue = Farce::Queue.new(2)
|
|
605
|
+
|
|
606
|
+
queue.try_push(:a) # => true
|
|
607
|
+
queue.try_push(:b) # => true
|
|
608
|
+
queue.try_push(:c) # => false
|
|
609
|
+
```
|
|
610
|
+
|
|
611
|
+
Normal queues have a default capacity of 1024. You can explicitly set the capacity to `nil` to create an unbounded queue.
|
|
612
|
+
Priority queues and timer queues do not have a default capacity, so earlier entries are not getting blocked unexpectedly.
|
|
613
|
+
|
|
614
|
+
### Shims, Polyfills, and Extensions
|
|
615
|
+
|
|
616
|
+
#### Ractor
|
|
617
|
+
|
|
618
|
+
Farce includes `Farce::Ractor`, which will either delegate to `Ractor` or supply a polyfill for it.
|
|
619
|
+
|
|
620
|
+
On platforms that don't support Ractors, a Thread-based implementation is provided that tracks Ractor-membership via Thread
|
|
621
|
+
groups.
|
|
622
|
+
|
|
623
|
+
```ruby
|
|
624
|
+
ractor = Farce::Ractor.new do
|
|
625
|
+
value = receive
|
|
626
|
+
puts "Received #{value.inspect}"
|
|
627
|
+
end
|
|
628
|
+
|
|
629
|
+
ractor.send 42
|
|
630
|
+
```
|
|
631
|
+
|
|
632
|
+
#### Port
|
|
633
|
+
|
|
634
|
+
Farce includes `Farce::Port`, which is either an extended subclass of `Ractor::Port` if it is available, or a polyfill if it isn't.
|
|
635
|
+
|
|
636
|
+
In addition to `Ractor::Port` it supports the following features:
|
|
637
|
+
* [Sharing modes](#sharing-modes) both for `.new` and `#send`.
|
|
638
|
+
* 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.
|
|
639
|
+
* `#receive` supports a `timeout` option on all Ruby implementations and versions, not just CRuby 4.1+
|
|
640
|
+
* You can use `#owned?` to check if the current Ractor owns a port.
|
|
641
|
+
* `#receive` does not block a Fiber scheduler when called from a non-blocking Fiber.
|
|
642
|
+
|
|
643
|
+
```ruby
|
|
644
|
+
# reject unshareable values except if they are being sent from the ractor owning the port
|
|
645
|
+
port = Farce::Port.new(mode: :raise, auto_local: true)
|
|
646
|
+
object = Object.new
|
|
647
|
+
|
|
648
|
+
port.send(object)
|
|
649
|
+
port.receive.equal?(object) # => true
|
|
650
|
+
```
|
|
651
|
+
|
|
652
|
+
#### WeakRef
|
|
653
|
+
|
|
654
|
+
`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.
|
|
655
|
+
|
|
656
|
+
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.
|
|
657
|
+
|
|
658
|
+
#### Resolv
|
|
659
|
+
|
|
660
|
+
`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.
|
|
661
|
+
|
|
662
|
+
### Sharing Unshareable Data
|
|
663
|
+
|
|
664
|
+
Ruby's `Ractor::Port#send` and related methods share Ruby objects between Ractors with this logic:
|
|
665
|
+
|
|
666
|
+
1. If the object is Ractor shareable, pass it by reference.
|
|
667
|
+
2. If the object isn't Ractor shareable and the `move` option hasn't been set to `true`, pass it by value (copy it).
|
|
668
|
+
3. Otherwise move it from the sending Ractor to the receiving Ractor, invalidating any previous references.
|
|
669
|
+
|
|
670
|
+
This is a great start but has some complications:
|
|
671
|
+
* Copying can lead to a lot of data duplication, as large object trees may be copied between Ractors over and over again.
|
|
672
|
+
* Moving may invalidate nested references unexpectedly, breaking code in the sending Ractor.
|
|
673
|
+
* If you want to prevent sending unshareable objects to other Ractors, you have to manually check them every time.
|
|
674
|
+
|
|
675
|
+
Farce has mechanisms and tools to improve the situation.
|
|
676
|
+
|
|
677
|
+
#### Sharing Modes
|
|
678
|
+
|
|
679
|
+
> [!TIP]
|
|
680
|
+
> Learn more in the dedicated [modes documentation](docs/modes.md).
|
|
681
|
+
|
|
682
|
+
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.
|
|
683
|
+
|
|
684
|
+
These modes are:
|
|
685
|
+
|
|
686
|
+
| Mode | What happens to non-shareable data | A typical use |
|
|
687
|
+
| ----------------- | ------------------------------------------------------------------------------------ | ------------------------------------------- |
|
|
688
|
+
| `:copy` (default) | Transfers a copy and leaves the original usable. This is the default. | Send a snapshot of a request. |
|
|
689
|
+
| `:move` | Transfers ownership and makes the original inaccessible. | Hand a completed batch to a consumer. |
|
|
690
|
+
| `:local` | Keeps the same object in its originating Ractor. | Pass work between local threads or fibers. |
|
|
691
|
+
| `:make_shareable` | Calls `Ractor.make_shareable` on the original. | Publish finished configuration. |
|
|
692
|
+
| `:mutable` | Copies non-shareable objects into a [`Farce::Mutable`](#mutables). | Synchronize mutations across Ractors. |
|
|
693
|
+
| `:shareable_copy` | Makes a shareable copy and leaves the original alone. | Publish a snapshot of an editable document. |
|
|
694
|
+
| `:dedup` | Deduplicates the value, then makes it shareable. May update and freeze the original. | Reuse repeated message contents. |
|
|
695
|
+
| `:proxy` | Creates a [`Farce::Proxy`](#proxies) that executes calls in the original Ractor. | Share access to a mutable object. |
|
|
696
|
+
| `:raise` | Raises `Ractor::IsolationError`. | Enforce a shareable-data boundary. |
|
|
697
|
+
|
|
698
|
+
Here's an example:
|
|
699
|
+
|
|
700
|
+
```ruby
|
|
701
|
+
map = Farce::Map.new(mode: :mutable)
|
|
702
|
+
map[:content] = "Hi there!"
|
|
703
|
+
|
|
704
|
+
Ractor.new(map) { it[:content] << " How are you doing?" }.join
|
|
705
|
+
|
|
706
|
+
# Hi there! How are you doing?
|
|
707
|
+
puts map[:content]
|
|
708
|
+
```
|
|
709
|
+
|
|
710
|
+
#### Envelopes
|
|
711
|
+
|
|
712
|
+
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.
|
|
713
|
+
|
|
714
|
+
Instead, you can wrap it in an envelope, and then later explicitly retrieve the value again.
|
|
715
|
+
|
|
716
|
+
```ruby
|
|
717
|
+
# We don't want to copy this over and over again.
|
|
718
|
+
big_array = 10_000.times.map { rand }
|
|
719
|
+
envelope = Farce::Envelope.new(big_array) # copied in once, copied out on demand
|
|
720
|
+
|
|
721
|
+
Ractor.new(envelope) do |envelope|
|
|
722
|
+
# we have a reference to envelope, but not a copy of its data
|
|
723
|
+
# this allows us to pass it on easily
|
|
724
|
+
queue = Farce::Queue.new(mode: :raise)
|
|
725
|
+
queue.push(envelope) # this is okay, the envelope is shareable
|
|
726
|
+
|
|
727
|
+
# We can pass the envelope by other means, like a queue!
|
|
728
|
+
Ractor.new(queue) do |queue|
|
|
729
|
+
message = queue.pop
|
|
730
|
+
# okay, let's get the copy, bypassing the outer ractor
|
|
731
|
+
copy_of_array = message.value
|
|
732
|
+
puts "Size of the array: #{copy_of_array.size}"
|
|
733
|
+
end
|
|
734
|
+
end
|
|
735
|
+
```
|
|
736
|
+
|
|
737
|
+
Envelopes support `copy`, `move`, `local`, as well as a dummy envelope for wrapping shareable objects.
|
|
738
|
+
|
|
739
|
+
#### Mutables
|
|
740
|
+
|
|
741
|
+
Mutables are special wrappers for Ruby objects that are Ractor-shareable when frozen but can be modified when not.
|
|
742
|
+
|
|
743
|
+
```ruby
|
|
744
|
+
mutable = Farce::Mutable.new("content")
|
|
745
|
+
Ractor.new(mutable) { it << " & additional content" }.join
|
|
746
|
+
mutable # => #<Farce::Mutable[String] "content & additional content">
|
|
747
|
+
```
|
|
748
|
+
|
|
749
|
+
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.
|
|
750
|
+
|
|
751
|
+
#### Proxies
|
|
752
|
+
|
|
753
|
+
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.
|
|
754
|
+
|
|
755
|
+
```ruby
|
|
756
|
+
# Mutable arrays aren't Ractor-shareable
|
|
757
|
+
array = []
|
|
758
|
+
proxied = []
|
|
759
|
+
proxy = Farce::Proxy.new(proxied)
|
|
760
|
+
|
|
761
|
+
Farce::Ractor.new(array, proxy) do |*list|
|
|
762
|
+
list.each { it << 42 }
|
|
763
|
+
end.join
|
|
764
|
+
|
|
765
|
+
# The array got copied instead of being modified in place
|
|
766
|
+
array # => []
|
|
767
|
+
|
|
768
|
+
# The proxy didn't get copied
|
|
769
|
+
proxied # => [42]
|
|
770
|
+
```
|
|
771
|
+
|
|
772
|
+
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.
|
|
773
|
+
|
|
774
|
+
The downside is a dispatch to another Ractor on every method call.
|
|
775
|
+
If copying on writes is an option, consider [`Farce::Mutable`](#mutables) instead.
|
|
776
|
+
If the object may be moved across Ractors, maybe a [`Farce::Lease`](#leases) is a better option.
|
|
777
|
+
|
|
778
|
+
There is an advanced [customization API](https://rkh.github.io/farce/main/Farce/Proxy.html), which allows fine-tuning and reducing overhead.
|
|
779
|
+
|
|
780
|
+
#### Mode Managers
|
|
781
|
+
|
|
782
|
+
You can use a mode manager if you want to support sharing modes for a custom object.
|
|
783
|
+
|
|
784
|
+
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.
|
|
785
|
+
|
|
786
|
+
```ruby
|
|
787
|
+
manager = Farce::ModeManager.new(mode: :move)
|
|
788
|
+
payload = ["inside a mutable array"]
|
|
789
|
+
Ractor.shareable?(payload) # => false
|
|
790
|
+
|
|
791
|
+
shareable = manager.wrap(payload) # => #<Farce::Envelope::Move>
|
|
792
|
+
Ractor.shareable?(shareable) # => true
|
|
793
|
+
|
|
794
|
+
payload = shareable.value
|
|
795
|
+
Ractor.shareable?(payload) # => false
|
|
796
|
+
```
|
|
797
|
+
|
|
798
|
+
#### Leases
|
|
799
|
+
|
|
800
|
+
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.
|
|
801
|
+
|
|
802
|
+
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.
|
|
803
|
+
|
|
804
|
+
This is very useful for objects that can be moved but can't be copied, especially IO-based objects like database connections.
|
|
805
|
+
|
|
806
|
+
```ruby
|
|
807
|
+
file_lease = Farce::Lease.new { File.open("example.txt", "w") }
|
|
808
|
+
|
|
809
|
+
Ractor.new(file_lease) do |file_lease|
|
|
810
|
+
file_lease.checkout do |file|
|
|
811
|
+
file.puts "Written from another Ractor!"
|
|
812
|
+
end
|
|
813
|
+
end.join
|
|
814
|
+
|
|
815
|
+
# make sure we close the file
|
|
816
|
+
file_lease.checkout(&:close)
|
|
817
|
+
|
|
818
|
+
# dereference the lease so it can be garbage collected
|
|
819
|
+
file_lease = nil
|
|
820
|
+
```
|
|
821
|
+
|
|
822
|
+
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.
|
|
823
|
+
|
|
824
|
+
Moreover, it can generate these objects for you on demand (until a certain number has been reached).
|
|
825
|
+
|
|
826
|
+
```ruby
|
|
827
|
+
# db connections are created on demand if there are fewer than five
|
|
828
|
+
pool = Farce::LeasePool.new(max_size: 5) { DB.connect }
|
|
829
|
+
pool.size # => 0
|
|
830
|
+
pool.max_size # => 5
|
|
831
|
+
|
|
832
|
+
pool.checkout do |db|
|
|
833
|
+
# ... do something with db ...
|
|
834
|
+
end
|
|
835
|
+
```
|
|
836
|
+
|
|
837
|
+
Or, if you want to associate leasable values with keys, you can use a `LeaseMap`:
|
|
838
|
+
|
|
839
|
+
```ruby
|
|
840
|
+
leases = Farce::LeaseMap.new { { primary: [], replica: [] } }
|
|
841
|
+
leases.checkout(:primary) { |items| items << :updated }
|
|
842
|
+
leases.checkout(:primary, &:dup) # => [:updated]
|
|
843
|
+
```
|
|
844
|
+
|
|
845
|
+
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:
|
|
846
|
+
|
|
847
|
+
```ruby
|
|
848
|
+
leases = Farce::LeaseMap.new { { primary: [], replica: [] } }
|
|
849
|
+
|
|
850
|
+
leases.auto_lease do
|
|
851
|
+
leases[:primary] << :updated
|
|
852
|
+
leases[:replica] << :replicated
|
|
853
|
+
leases[:primary] << :verified # Reuses the same checkout
|
|
854
|
+
end
|
|
855
|
+
|
|
856
|
+
# Both resources are checked back in when the block exits, even on an exception.
|
|
857
|
+
leases.available?(:primary) # => true
|
|
858
|
+
leases.checkout(:primary, &:dup) # => [:updated, :verified]
|
|
859
|
+
```
|
|
860
|
+
|
|
861
|
+
### Variants and Scopes
|
|
862
|
+
|
|
863
|
+
Many classes, including all [data structures](#data-structures), implement multiple variants as separate subclasses within module namespaces.
|
|
864
|
+
|
|
865
|
+
`Farce::Map`, `Farce::Strict::Map`, `Farce::Local::Map`, and `Farce::Unshared::Map` all support the same API, except that:
|
|
866
|
+
|
|
867
|
+
* `Farce::Map` accepts an optional `mode` keyword for its initializer and many methods.
|
|
868
|
+
* `Farce::Strict::Map` will not allow any non-shareable data to be stored in it. It is slightly faster than `Farce::Map`.
|
|
869
|
+
* `Farce::Local::Map` will accept an optional `scope` keyword for its initializer, and will have different content for each [scope](#provided-scopes).
|
|
870
|
+
* `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`.
|
|
871
|
+
|
|
872
|
+
#### Available Variants
|
|
873
|
+
|
|
874
|
+
> [!TIP]
|
|
875
|
+
> Learn more in the dedicated [variants documentation](docs/variants.md).
|
|
876
|
+
|
|
877
|
+
These variants are provided by Farce:
|
|
878
|
+
|
|
879
|
+
1. **Default** variants, under the `Farce` namespace:
|
|
880
|
+
* Can **store unshareable values**, usually via [modes](#sharing-modes).
|
|
881
|
+
For maps, this is only supported for values. Keys must be shareable.
|
|
882
|
+
* Instances are **Ractor-shareable**.
|
|
883
|
+
* The data structure is **thread-safe**.
|
|
884
|
+
2. **Strict** variants, under the `Farce::Strict` namespace:
|
|
885
|
+
* **Forbid unshareable values**
|
|
886
|
+
* Instances are **Ractor-shareable**.
|
|
887
|
+
* The data structure is **thread-safe**.
|
|
888
|
+
* May provide performance benefits over the default variant.
|
|
889
|
+
3. **Local** variants, under the `Farce::Local` namespace:
|
|
890
|
+
* Will have **different content** for each [scope](#provided-scopes).
|
|
891
|
+
* Can **store unshareable values**, including unshareable map keys.
|
|
892
|
+
* Instances are **Ractor-shareable**.
|
|
893
|
+
* The data structure is **thread-safe**.
|
|
894
|
+
4. **Unshared** variants, under `Farce::Unshared` namespace:
|
|
895
|
+
* Can **store unshareable values**, including unshareable map keys.
|
|
896
|
+
* Instances are **<u>not</u> Ractor-shareable**.
|
|
897
|
+
* The data structure is **thread-safe**.
|
|
898
|
+
* May provide performance benefits over the default variant.
|
|
899
|
+
5. **Unsafe** variants, under `Farce::Unsafe` namespace:
|
|
900
|
+
* Same as Unshared, except they do **<u>not</u> guarantee thread-safety**
|
|
901
|
+
* May provide performance benefits over all other variants.
|
|
902
|
+
6. **Transaction** variants, under `Farce::Transaction` namespace:
|
|
903
|
+
* Created as mirrors of another object within a [transaction](#transactions).
|
|
904
|
+
* Should not be shared across transaction boundaries.
|
|
905
|
+
* Should not be initialized directly.
|
|
906
|
+
|
|
907
|
+
The following is true for all variants:
|
|
908
|
+
* Blocking operations will suspend a non-blocking fiber, but not block the underlying scheduler.
|
|
909
|
+
* The return value of `frozen?` will correctly reflect whether an instance is mutable or not.
|
|
910
|
+
|
|
911
|
+
However, keep the following in mind:
|
|
912
|
+
* Not all classes implement all variants. Check out the [full list](docs/variants.md#classes-implementing-variants)
|
|
913
|
+
* Other classes that don't implement variants are also nested under the `Farce` namespace.
|
|
914
|
+
|
|
915
|
+
#### Provided scopes
|
|
916
|
+
|
|
917
|
+
> [!TIP]
|
|
918
|
+
> Learn more in the dedicated [scopes documentation](docs/scopes.md).
|
|
919
|
+
|
|
920
|
+
Local variants support scopes:
|
|
921
|
+
|
|
922
|
+
```ruby
|
|
923
|
+
map = Farce::Map.new(scope: :thread)
|
|
924
|
+
map[:id] = 1
|
|
925
|
+
|
|
926
|
+
Thread.new do
|
|
927
|
+
map[:id] = 2
|
|
928
|
+
map[:id] # => 2
|
|
929
|
+
end.join
|
|
930
|
+
|
|
931
|
+
map[:id] # => 1
|
|
932
|
+
```
|
|
933
|
+
|
|
934
|
+
The following scopes are available:
|
|
935
|
+
|
|
936
|
+
| Scope | Description |
|
|
937
|
+
| ------------------ | ------------------------------------------------------------------------------------------------ |
|
|
938
|
+
| `ractor` (default) | Content varies by [Ractor](https://docs.ruby-lang.org/en/4.0/Ractor.html) |
|
|
939
|
+
| `thread_group` | Content varies by [ThreadGroup](https://docs.ruby-lang.org/en/4.0/ThreadGroup.html) |
|
|
940
|
+
| `thread` | Content varies by [Thread](https://docs.ruby-lang.org/en/4.0/Thread.html) |
|
|
941
|
+
| `fiber_storage` | Content varies by [Fiber storage](https://docs.ruby-lang.org/en/4.0/Fiber.html#method-i-storage) |
|
|
942
|
+
| `fiber` | Content varies by [Fiber](https://docs.ruby-lang.org/en/4.0/Fiber.html) |
|
|
943
|
+
|
|
944
|
+
Fiber storage is typically inherited by a blocking fiber from the fiber creating it.
|
|
945
|
+
|
|
946
|
+
### Concurrency
|
|
947
|
+
|
|
948
|
+
Farce is built for concurrent and parallel code execution.
|
|
949
|
+
|
|
950
|
+
#### Locks
|
|
951
|
+
|
|
952
|
+
Farce ships with two lock classes:
|
|
953
|
+
* `Lock` is a drop-in replacement for Ruby's `Mutex`.
|
|
954
|
+
* `ReadWriteLock` exposes `with_read_lock`/`with_write_lock` to allow multiple concurrent reads, but exclusive write access.
|
|
955
|
+
|
|
956
|
+
However, Farce exposes many APIs to eliminate the need for locks altogether.
|
|
957
|
+
|
|
958
|
+
#### Atomic Operations
|
|
959
|
+
|
|
960
|
+
Farce's [data structures](#data-structures) all expose a range of methods for atomic operations:
|
|
961
|
+
|
|
962
|
+
```ruby
|
|
963
|
+
# A Hash-like object
|
|
964
|
+
map = Farce::Map.new
|
|
965
|
+
|
|
966
|
+
# Atomically store something for :key if it hasn't been set
|
|
967
|
+
map.store_if_absent(:key) { "initial value" }
|
|
968
|
+
|
|
969
|
+
# Atomically update :key
|
|
970
|
+
map.update(:key, &:upcase)
|
|
971
|
+
|
|
972
|
+
# An Array-like object
|
|
973
|
+
list = Farce::Vector.new
|
|
974
|
+
list[0] = 42
|
|
975
|
+
|
|
976
|
+
# Atomically replace list[0] with 256 if the value is still 42
|
|
977
|
+
list.compare_and_swap(0, 42, 256)
|
|
978
|
+
```
|
|
979
|
+
|
|
980
|
+
#### Observability and Signaling
|
|
981
|
+
|
|
982
|
+
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).
|
|
983
|
+
|
|
984
|
+
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).
|
|
985
|
+
|
|
986
|
+
So instead of sharing locks you can simply wait for a change to happen!
|
|
987
|
+
|
|
988
|
+
```ruby
|
|
989
|
+
# block until the stored value for :key is greater than 10
|
|
990
|
+
map.wait_until(:key) { it > 10 }
|
|
991
|
+
|
|
992
|
+
# block until the value for an atom is no longer :initial
|
|
993
|
+
map.wait_until_changed(:initial)
|
|
994
|
+
|
|
995
|
+
# block until a counter has reached at least 20
|
|
996
|
+
counter.wait_while_below(20)
|
|
997
|
+
```
|
|
998
|
+
|
|
999
|
+
If these conditions are met right away, these never block.
|
|
1000
|
+
|
|
1001
|
+
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!
|
|
1002
|
+
|
|
1003
|
+
```ruby
|
|
1004
|
+
signal = Farce::Signal.new
|
|
1005
|
+
target = 100
|
|
1006
|
+
counter = Farce::Counter.new
|
|
1007
|
+
|
|
1008
|
+
# A Thread that keeps reducing the target value every 20 milliseconds
|
|
1009
|
+
Thread.new do
|
|
1010
|
+
while target.positive?
|
|
1011
|
+
sleep 0.02
|
|
1012
|
+
target -= 1
|
|
1013
|
+
signal.broadcast # notify everyone else
|
|
1014
|
+
end
|
|
1015
|
+
end
|
|
1016
|
+
|
|
1017
|
+
# A Ractor that counts up in 10 millisecond intervals
|
|
1018
|
+
Ractor.new(counter, signal) do |counter, signal|
|
|
1019
|
+
while counter < 100
|
|
1020
|
+
sleep 0.01
|
|
1021
|
+
counter.increment
|
|
1022
|
+
signal.broadcast # notify everyone else
|
|
1023
|
+
end
|
|
1024
|
+
end
|
|
1025
|
+
|
|
1026
|
+
# wait until the counter is at or above the target
|
|
1027
|
+
signal.wait_until { counter >= target }
|
|
1028
|
+
```
|
|
1029
|
+
|
|
1030
|
+
#### Transactions
|
|
1031
|
+
|
|
1032
|
+
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).
|
|
1033
|
+
|
|
1034
|
+
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.
|
|
1035
|
+
|
|
1036
|
+
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.
|
|
1037
|
+
|
|
1038
|
+
Otherwise the block is rerun.
|
|
1039
|
+
|
|
1040
|
+
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!
|
|
1041
|
+
|
|
1042
|
+
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.
|
|
1043
|
+
|
|
1044
|
+
```ruby
|
|
1045
|
+
accounts = Farce::Map.new({a: 100, b: 200})
|
|
1046
|
+
|
|
1047
|
+
# transfer 80 from :a to :b, but only if both succeed
|
|
1048
|
+
success = Farce.transaction(accounts) do |tx, accounts|
|
|
1049
|
+
tx.abort! if accounts[:b] < 80
|
|
1050
|
+
accounts[:a] += 80
|
|
1051
|
+
accounts[:b] -= 80
|
|
1052
|
+
end
|
|
1053
|
+
|
|
1054
|
+
if success
|
|
1055
|
+
puts "Transaction succeeded"
|
|
1056
|
+
else
|
|
1057
|
+
puts "Transaction failed"
|
|
1058
|
+
end
|
|
1059
|
+
```
|
|
1060
|
+
|
|
1061
|
+
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).
|
|
1062
|
+
|
|
1063
|
+
##### Maps and Sets
|
|
1064
|
+
|
|
1065
|
+
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.
|
|
1066
|
+
|
|
1067
|
+
Similarly, if your transaction uses `Transaction::Map#size` as input, only a change in size would trigger a rerun, not a change in content.
|
|
1068
|
+
|
|
1069
|
+
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).
|
|
1070
|
+
|
|
1071
|
+
##### TVars
|
|
1072
|
+
|
|
1073
|
+
Farce doesn't come with a `TVar` class. You can just use an [atom](#atoms) instead.
|
|
1074
|
+
|
|
1075
|
+
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)).
|
|
1076
|
+
|
|
1077
|
+
```ruby
|
|
1078
|
+
tvar = Concurrent::TVar.new(50)
|
|
1079
|
+
map = Farce::Map.new({a: 100, b: 200})
|
|
1080
|
+
|
|
1081
|
+
Farce.transaction(map, tvar) do |map, tvar|
|
|
1082
|
+
# subtract tvar's value from a and b, then set tvar to 0
|
|
1083
|
+
# no invalid in-between state is ever visible to anything outside of this transaction
|
|
1084
|
+
map[:a] -= tvar.value
|
|
1085
|
+
map[:b] -= tvar.value
|
|
1086
|
+
tvar.value = 0
|
|
1087
|
+
end
|
|
1088
|
+
```
|
|
1089
|
+
|
|
1090
|
+
### Scheduling Code
|
|
1091
|
+
|
|
1092
|
+
Farce offers built-in code scheduling support.
|
|
1093
|
+
|
|
1094
|
+
#### `Farce.on_main`
|
|
1095
|
+
|
|
1096
|
+
Some code has to be executed on the main Ractor, especially when working with legacy code that doesn't support Ractors.
|
|
1097
|
+
|
|
1098
|
+
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.
|
|
1099
|
+
|
|
1100
|
+
```ruby
|
|
1101
|
+
# most of concurrent-ruby is not usable outside of the main-ractor
|
|
1102
|
+
$tvar = Concurrent::TVar
|
|
1103
|
+
|
|
1104
|
+
# let's run computation outside of the main ractor
|
|
1105
|
+
Ractor.new do
|
|
1106
|
+
value = compute_expensive_value
|
|
1107
|
+
|
|
1108
|
+
# need to report back to the main ractor
|
|
1109
|
+
Farce.on_main(value) { $tvar.value = it }
|
|
1110
|
+
end
|
|
1111
|
+
```
|
|
1112
|
+
|
|
1113
|
+
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:
|
|
1114
|
+
|
|
1115
|
+
```ruby
|
|
1116
|
+
Ractor.new do
|
|
1117
|
+
Farce.on_main.schedule do
|
|
1118
|
+
sleep 1
|
|
1119
|
+
puts "Hello from the main Ractor"
|
|
1120
|
+
end
|
|
1121
|
+
puts "Hello from the nested Ractor"
|
|
1122
|
+
end
|
|
1123
|
+
|
|
1124
|
+
sleep 1.1
|
|
1125
|
+
```
|
|
1126
|
+
|
|
1127
|
+
#### `Farce.in_parallel`
|
|
1128
|
+
|
|
1129
|
+
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.
|
|
1130
|
+
|
|
1131
|
+
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.
|
|
1132
|
+
|
|
1133
|
+
```ruby
|
|
1134
|
+
input = "this is my input"
|
|
1135
|
+
Farce.in_parallel(input) { expensive_computation(it) }
|
|
1136
|
+
```
|
|
1137
|
+
|
|
1138
|
+
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.
|
|
1139
|
+
|
|
1140
|
+
#### `Farce.schedule`
|
|
1141
|
+
|
|
1142
|
+
`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?
|
|
1143
|
+
|
|
1144
|
+
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).
|
|
1145
|
+
|
|
1146
|
+
```ruby
|
|
1147
|
+
# just run this asynchronously, don't care how
|
|
1148
|
+
Farce.schedule("hello") { |message| puts message.upcase }
|
|
1149
|
+
```
|
|
1150
|
+
|
|
1151
|
+
"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:
|
|
1152
|
+
|
|
1153
|
+
```ruby
|
|
1154
|
+
Async do
|
|
1155
|
+
# this is basically the same as calling Async { do_something }
|
|
1156
|
+
Farce.schedule { do_something }
|
|
1157
|
+
end
|
|
1158
|
+
```
|
|
1159
|
+
|
|
1160
|
+
#### Schedulers
|
|
1161
|
+
|
|
1162
|
+
You can create your own `Farce::Scheduler`, running on a dedicated Ractor:
|
|
1163
|
+
|
|
1164
|
+
```ruby
|
|
1165
|
+
scheduler = Farce::Scheduler.create
|
|
1166
|
+
scheduler.schedule("hello") { |message| puts message }
|
|
1167
|
+
scheduler.close # we're done, shut it down
|
|
1168
|
+
```
|
|
1169
|
+
|
|
1170
|
+
Or you can set one up as a Fiber scheduler for the current Thread:
|
|
1171
|
+
|
|
1172
|
+
```ruby
|
|
1173
|
+
scheduler = Farce::Scheduler.new
|
|
1174
|
+
Fiber.set_scheduler(scheduler)
|
|
1175
|
+
Fiber.schedule { puts "Hello from the scheduler!" }
|
|
1176
|
+
```
|
|
1177
|
+
|
|
1178
|
+
Scheduler instances are Ractor-shareable.
|
|
1179
|
+
|
|
1180
|
+
#### Ractor Pools
|
|
1181
|
+
|
|
1182
|
+
`Farce::Pool` implements the same scheduling interface, but manages a pool of Ractors. This is what [`in_parallel`](#farcein_parallel) uses under the hood.
|
|
1183
|
+
|
|
1184
|
+
```ruby
|
|
1185
|
+
# At least two Ractors, up to four. Launch a new one if a task waits longer than 100 milliseconds.
|
|
1186
|
+
pool = Farce::Pool.new(min_size: 4, max_size: 10, grow_after: 0.1)
|
|
1187
|
+
pool.schedule { puts "Hello from the pool!" }
|
|
1188
|
+
```
|
|
1189
|
+
|
|
1190
|
+
#### Third-Party Fiber Schedulers
|
|
1191
|
+
|
|
1192
|
+
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:
|
|
1193
|
+
|
|
1194
|
+
```ruby
|
|
1195
|
+
# Use the fiber scheduler from the carbon_fiber gem.
|
|
1196
|
+
pool = Farce::Pool.new { CarbonFiber::Scheduler.new }
|
|
1197
|
+
pool.schedule { puts "⚡️ Running Carbon Fiber on a pool of Ractors! ⚡️" }
|
|
1198
|
+
```
|
|
1199
|
+
|
|
1200
|
+
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:
|
|
1201
|
+
|
|
1202
|
+
```ruby
|
|
1203
|
+
# This needs to happen before the first scheduler call.
|
|
1204
|
+
Farce.configure do |config|
|
|
1205
|
+
config.fiber_scheduler = :carbon_fiber
|
|
1206
|
+
|
|
1207
|
+
# or, alternatively:
|
|
1208
|
+
config.fiber_scheduler { CarbonFiber::Scheduler.new }
|
|
1209
|
+
end
|
|
1210
|
+
|
|
1211
|
+
Farce.in_parallel do
|
|
1212
|
+
Fiber.scheduler.class # => CarbonFiber::Scheduler
|
|
1213
|
+
end
|
|
1214
|
+
```
|
|
1215
|
+
|
|
1216
|
+
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`.
|
|
1217
|
+
|
|
1218
|
+
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`.
|
|
1219
|
+
|
|
1220
|
+
### Integrations
|
|
1221
|
+
|
|
1222
|
+
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.
|
|
1223
|
+
|
|
1224
|
+
#### Active Support
|
|
1225
|
+
|
|
1226
|
+
The Active Support integration adds the following methods:
|
|
1227
|
+
|
|
1228
|
+
* For all data structures: `as_json`, `blank?`, `deep_dup`, and `duplicable?`
|
|
1229
|
+
* For maps: `assert_valid_keys`, `compact_blank`, `reverse_merge`, `stringify_keys`, `symbolize_keys`, `to_param`, `to_query`, `with_defaults`, and `with_indifferent_access`
|
|
1230
|
+
* 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`
|
|
1231
|
+
|
|
1232
|
+
And the following features:
|
|
1233
|
+
* Converting `ActiveSupport::HashWithIndifferentAccess` to a map via [`Farce.enfarce`](#top-level-methods) will set up the correct key normalization.
|
|
1234
|
+
* [`Farce::Clock`](#additional-classes) understands `ActiveSupport::Duration`.
|
|
1235
|
+
|
|
1236
|
+
Other methods are already being inherited by various objects via `Object`, `Enumerable` for vectors, sets, and maps, `Numeric` for counters, etc.
|
|
1237
|
+
|
|
1238
|
+
```ruby
|
|
1239
|
+
require "active_support/all"
|
|
1240
|
+
require "farce"
|
|
1241
|
+
|
|
1242
|
+
map = Farce::Map.new.with_indifferent_access
|
|
1243
|
+
map[:a] = 10
|
|
1244
|
+
|
|
1245
|
+
map.blank? # => false
|
|
1246
|
+
map["a"] # => 10
|
|
1247
|
+
```
|
|
1248
|
+
|
|
1249
|
+
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).
|
|
1250
|
+
|
|
1251
|
+
#### Dry Types
|
|
1252
|
+
|
|
1253
|
+
Adds support for Farce [data structures](#data-structures) to [Dry Types](https://hanakai.org/learn/dry/dry-types):
|
|
1254
|
+
|
|
1255
|
+
```ruby
|
|
1256
|
+
require "dry-types"
|
|
1257
|
+
require "farce"
|
|
1258
|
+
|
|
1259
|
+
module Types
|
|
1260
|
+
include Dry.Types()
|
|
1261
|
+
include Farce.DryTypes()
|
|
1262
|
+
|
|
1263
|
+
IntegerVector = Vector.of(Coercible::Integer)
|
|
1264
|
+
end
|
|
1265
|
+
|
|
1266
|
+
numbers = Types::IntegerVector[["1", 2]]
|
|
1267
|
+
numbers.class # => Farce::Vector
|
|
1268
|
+
numbers.to_a # => [1, 2]
|
|
1269
|
+
```
|
|
1270
|
+
|
|
1271
|
+
Check out the [dedicated documentation](docs/gems/dry-types.md) to learn more.
|
|
1272
|
+
|
|
1273
|
+
#### JSON, YAML, etc
|
|
1274
|
+
|
|
1275
|
+
It ships integrations for serialization (and some deserialization) with the following gems:
|
|
1276
|
+
|
|
1277
|
+
* For BSON support: `bson`
|
|
1278
|
+
* For CBOR support: `cbor`
|
|
1279
|
+
* For JSON support: `json` (from the Ruby standard library), `oj`, and `yajl`
|
|
1280
|
+
* For MessagePack: `msgpack` – see the [detailed documentation](docs/gems/msgpack.md)
|
|
1281
|
+
* For YAML: `psych` (from the Ruby standard library)
|
|
1282
|
+
|
|
1283
|
+
Example:
|
|
1284
|
+
|
|
1285
|
+
```ruby
|
|
1286
|
+
require "json"
|
|
1287
|
+
require "farce"
|
|
1288
|
+
|
|
1289
|
+
map = Farce::Map.new
|
|
1290
|
+
map[:x] = Farce::Vector[1, 2, 3]
|
|
1291
|
+
map.to_json # => '{"x":[1,2,3]}'
|
|
1292
|
+
```
|
|
1293
|
+
|
|
1294
|
+
#### Concurrent Ruby
|
|
1295
|
+
|
|
1296
|
+
The integration adds the following features if [concurrent-ruby](https://github.com/ruby-concurrency/concurrent-ruby) has been loaded:
|
|
1297
|
+
|
|
1298
|
+
* [Transaction support for `Concurrent::TVar`](#tvars)
|
|
1299
|
+
* Automatic conversion of `Concurrent::Map` instances to [maps](#maps) via [`Farce.enfarce`](#top-level-methods).
|
|
1300
|
+
|
|
1301
|
+
#### Ractor Sharing
|
|
1302
|
+
|
|
1303
|
+
The integration adds the following features if [ractor-sharing](https://github.com/ko1/ractor-sharing) has been loaded:
|
|
1304
|
+
|
|
1305
|
+
* [Transaction support for `Ractor::TVar`](#tvars)
|
|
1306
|
+
* Support for traversing and deep freezing `Ractor::TVar`, `Ractor::LockVar`, `Ractor::LockHash`, and `Ractor::KeyLockHash`
|
|
1307
|
+
* Support for converting `Ractor::LockHash` and `Ractor::KeyLockHash` to [maps](#maps) via [`Farce.enfarce`](#top-level-methods).
|
|
1308
|
+
|
|
1309
|
+
#### Additional Integrations
|
|
1310
|
+
|
|
1311
|
+
* [`sorted_set`](https://github.com/knu/sorted_set): Add support for walking and converting them.
|
|
1312
|
+
* [`ractor-tmvar`](https://github.com/yoshitsugu/ractor-tmvar): Adds transaction support.
|
|
1313
|
+
* [`weakref`](https://github.com/ruby/weakref): Add support for walking and converting them.
|
|
1314
|
+
|
|
1315
|
+
#### Disable Automatic loading
|
|
1316
|
+
|
|
1317
|
+
You can set the environment variable `FARCE_AUTOLOAD_INTEGRATIONS` to `false` or `0` to disable automatic integration loading.
|
|
1318
|
+
|
|
1319
|
+
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:
|
|
1320
|
+
|
|
1321
|
+
```ruby
|
|
1322
|
+
# This could go in an initializer
|
|
1323
|
+
require "farce/config"
|
|
1324
|
+
|
|
1325
|
+
Farce.configure do |config|
|
|
1326
|
+
config.autoload_integrations = false
|
|
1327
|
+
end
|
|
1328
|
+
|
|
1329
|
+
require "farce"
|
|
1330
|
+
|
|
1331
|
+
# you now need to load any integrations you might want explicitly
|
|
1332
|
+
# these are available via "farce/integrations/#{gem_name}"
|
|
1333
|
+
require "farce/integrations/json"
|
|
1334
|
+
require "farce/integrations/concurrent"
|
|
1335
|
+
```
|
|
1336
|
+
|
|
1337
|
+
### Miscellaneous
|
|
1338
|
+
|
|
1339
|
+
#### Top Level Methods
|
|
1340
|
+
|
|
1341
|
+
* `Farce.clock` returns the monotonic clock time in seconds as a Float.
|
|
1342
|
+
* `Farce.config` returns the global configuration object.
|
|
1343
|
+
* `Farce.configure` allows you to configure Farce.
|
|
1344
|
+
* `Farce.dedup` de-duplicates the given object based on a deduplication cache shared by all Ractors.
|
|
1345
|
+
* `Farce.enfarce` turns a vanilla data structure into its Farce equivalent.
|
|
1346
|
+
* `Farce.freeze_graph` recursively freezes an object graph.
|
|
1347
|
+
* `Farce.in_parallel`, `Farce.on_main`, and `Farce.schedule`, see [Scheduling Code](#scheduling-code)
|
|
1348
|
+
* `Farce.rebind` rebinds a proc or lambda while preserving its Ractor-shareability.
|
|
1349
|
+
* `Farce.transaction` creates and runs a [transaction](#transactions).
|
|
1350
|
+
|
|
1351
|
+
Some examples:
|
|
1352
|
+
|
|
1353
|
+
```ruby
|
|
1354
|
+
a = { a: [+"b"] }
|
|
1355
|
+
b = { a: [+"b"] }
|
|
1356
|
+
|
|
1357
|
+
# Farce.clock
|
|
1358
|
+
Farce.clock # => 0.017476999908685684
|
|
1359
|
+
Farce.clock(in: 10) # => 10.017520000003278
|
|
1360
|
+
|
|
1361
|
+
# Farce.dedup
|
|
1362
|
+
a.equal? b # => false
|
|
1363
|
+
Farce.dedup(a).equal? Farce.dedup(b) # => true
|
|
1364
|
+
|
|
1365
|
+
# Farce.enfarce
|
|
1366
|
+
Farce.enfarce(a) # => #<Farce::Map {a: #<Farce::Vector ["b"]>}>
|
|
1367
|
+
Farce::Strict.enfarce(a) # => #<Farce::Strict::Map {a: #<Farce::Strict::Vector ["b"]>}>
|
|
1368
|
+
|
|
1369
|
+
# Farce.freeze_graph
|
|
1370
|
+
Farce.freeze_graph(a)
|
|
1371
|
+
|
|
1372
|
+
# Farce.rebind
|
|
1373
|
+
callback = ->(add) { self + add }
|
|
1374
|
+
rebound = Farce.rebind(callback, self: 42)
|
|
1375
|
+
rebound.call(18) # => 50
|
|
1376
|
+
```
|
|
1377
|
+
|
|
1378
|
+
#### Additional Classes
|
|
1379
|
+
|
|
1380
|
+
Other classes Farce provides include:
|
|
1381
|
+
* `Config`: Configuration class, see [Third-Party Fiber Schedulers](#third-party-fiber-schedulers) example.
|
|
1382
|
+
* `ClassMirror`: inheritance-aware registry for classes
|
|
1383
|
+
* `Clock`: Timing functions based on a monotonic clock rather than on `Time`.
|
|
1384
|
+
* `Deduper`: Create your own deduplication cache. Direct usage isn't recommended, use [`Farce.dedup`](#top-level-methods) instead.
|
|
1385
|
+
* `Exchanger`: A synchronization point for two-way data swapping between Threads, Ractors, and/or Fibers. Drop-in replacement for concurrent-ruby's exchanger.
|
|
1386
|
+
* `Lazy`: Lazily initialized value.
|
|
1387
|
+
* `LazyRef`: A [reference](#references) for a lazily initialized value.
|
|
1388
|
+
* `ThreadScheduler`: An alternative [scheduler](#scheduling-code) creating a new thread for each unit of work. Used on TruffleRuby.
|
|
1389
|
+
* `Walker`: A tool for walking a Ruby object tree.
|
|
1390
|
+
* `WeakValue`: A value object version of [`WeakRef`](#weakref). Allows handling references more explicitly, without automatic method delegation.
|
|
1391
|
+
|
|
1392
|
+
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.
|
|
1393
|
+
|
|
1394
|
+
#### Shareability Mixins
|
|
1395
|
+
|
|
1396
|
+
Farce includes mixins to help you make your custom classes shareable:
|
|
1397
|
+
|
|
1398
|
+
* `Shareable` automatically marks objects as shareable (via `Ractor.make_shareable`) after initialization.
|
|
1399
|
+
* `Shareable::Delegated` delegates `freeze` and `frozen?` to another object holding your object's state.
|
|
1400
|
+
* `Shareable::Immutable` instances are always immutable, being frozen after initialization.
|
|
1401
|
+
* `Shareable::Native` is for objects implemented in a native extension, which allows setting the frozen and shareable state separately.
|
|
1402
|
+
* `Shareable::Tracked` for objects implementing frozen tracking (via an internal [flag](#flags)).
|
|
1403
|
+
* `Shareable::Unfreezable` for objects that cannot be frozen (like [queues](#queues)).
|
|
1404
|
+
|
|
1405
|
+
And also mixins to prevent them from being shareable:
|
|
1406
|
+
|
|
1407
|
+
* `Unshareable`: Instances aren't shareable and cannot be made shareable. By default, they also cannot be copied or moved between Ractors.
|
|
1408
|
+
* `Unshareable::Copyable`: Instances aren't shareable but may be copied to another Ractor.
|
|
1409
|
+
* `Unshareable::Movable`: Instances aren't shareable but may be moved to another Ractor.
|
|
1410
|
+
|
|
1411
|
+
`Unshareable::Copyable` and `Unshareable::Movable` may be combined.
|
|
1412
|
+
|
|
1413
|
+
#### Constants
|
|
1414
|
+
|
|
1415
|
+
* `Farce::MODES`: List of supported [sharing modes](#sharing-modes).
|
|
1416
|
+
* `Farce::SCOPES`: List of [provided scopes](#provided-scopes).
|
|
1417
|
+
* `Farce::VERSION`: The current version.
|
|
1418
|
+
|
|
1419
|
+
## Compatibility and Dependencies
|
|
1420
|
+
|
|
1421
|
+
Farce has no mandatory dependencies beyond Ruby itself.
|
|
1422
|
+
|
|
1423
|
+
### Ruby
|
|
1424
|
+
|
|
1425
|
+
Each Farce release is expected to be compatible with:
|
|
1426
|
+
|
|
1427
|
+
* 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/).
|
|
1428
|
+
* Ruby's [master branch](https://github.com/ruby/ruby/tree/master) at the time of release (i.e., the upcoming major version of CRuby).
|
|
1429
|
+
* The latest stable release of [JRuby](https://www.jruby.org/) and [TruffleRuby](https://truffleruby.dev/) (both in native and GraalVM modes).
|
|
1430
|
+
|
|
1431
|
+
Moreover:
|
|
1432
|
+
|
|
1433
|
+
* Dropping support for a CRuby version is only done in major releases.
|
|
1434
|
+
* 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/).
|
|
1435
|
+
|
|
1436
|
+
### Similar Projects
|
|
1437
|
+
|
|
1438
|
+
* [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.
|
|
1439
|
+
* [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.
|
|
1440
|
+
* [ratomic](https://mperham.github.io/ratomic/) has overlapping functionality with Farce for basic data structures like maps, counters, and queues.
|
|
1441
|
+
* [ractor_safe](https://github.com/jhawthorn/ractor_safe/) has overlapping functionality with Farce for basic data structures like maps, counters, and queues.
|
|
1442
|
+
* [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.
|
|
1443
|
+
|
|
1444
|
+
All of the above projects can safely be used alongside Farce in the same application.
|
|
1445
|
+
|
|
1446
|
+
## Installation
|
|
1447
|
+
|
|
1448
|
+
### Globally
|
|
1449
|
+
|
|
1450
|
+
To install Farce globally, you can use the following command:
|
|
1451
|
+
|
|
1452
|
+
```console
|
|
1453
|
+
$ gem install farce
|
|
1454
|
+
```
|
|
1455
|
+
|
|
1456
|
+
### As a project dependency
|
|
1457
|
+
|
|
1458
|
+
If you want to use Farce directly in your project, it is recommended to do so via [Bundler](https://bundler.io).
|
|
1459
|
+
Add Farce to your `Gemfile`:
|
|
1460
|
+
|
|
1461
|
+
```ruby
|
|
1462
|
+
source "https://gem.coop" # or "https://rubygems.org"
|
|
1463
|
+
|
|
1464
|
+
gem "farce"
|
|
1465
|
+
```
|
|
1466
|
+
|
|
1467
|
+
Then run `bundle install` to install the dependencies.
|
|
1468
|
+
|
|
1469
|
+
### As a library dependency
|
|
1470
|
+
|
|
1471
|
+
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:
|
|
1472
|
+
|
|
1473
|
+
```ruby
|
|
1474
|
+
Gem::Specification.new do |spec|
|
|
1475
|
+
# ...
|
|
1476
|
+
spec.add_dependency "farce"
|
|
1477
|
+
end
|
|
1478
|
+
```
|
|
1479
|
+
|
|
1480
|
+
### Local setup
|
|
1481
|
+
|
|
1482
|
+
If you want to work on Farce itself, you can clone the repository and use [mise](https://mise.jdx.dev) to set everything up:
|
|
1483
|
+
|
|
1484
|
+
For more details, or if you aren't using mise, check the [contribution guidelines](CONTRIBUTING.md).
|
|
1485
|
+
|
|
1486
|
+
```console
|
|
1487
|
+
$ git clone https://github.com/rkh/farce.git # prefix with `jj` if you're using Jujutsu
|
|
1488
|
+
$ cd farce
|
|
1489
|
+
$ mise run
|
|
1490
|
+
```
|
|
1491
|
+
|
|
1492
|
+
### Loading Farce
|
|
1493
|
+
You should always require `farce`, rather than any other files in `lib`. Other files are not intended as entry points.
|
|
1494
|
+
|
|
1495
|
+
```ruby
|
|
1496
|
+
require "farce"
|
|
1497
|
+
```
|
|
1498
|
+
|
|
1499
|
+
Constants (classes, modules, etc.) under the `Farce` namespace are loaded lazily (thread- and ractor-safe), so there is no
|
|
1500
|
+
need to specifically load any particular file.
|
|
1501
|
+
|
|
1502
|
+
## Known Issues and Limitations
|
|
1503
|
+
|
|
1504
|
+
### Possible discrepancy regarding frozen state in Ruby and C
|
|
1505
|
+
|
|
1506
|
+
> 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.
|
|
1507
|
+
> — *Yukihiro Matsumoto* (Ruby Issue [#22291](https://bugs.ruby-lang.org/issues/22291#note-4), emphasis added)
|
|
1508
|
+
|
|
1509
|
+
In Ruby, Farce objects reflect their frozen state accurately. If a map returns `true` for `frozen?`, you cannot add, remove, or replace its entries.
|
|
1510
|
+
|
|
1511
|
+
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.
|
|
1512
|
+
|
|
1513
|
+
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.
|
|
1514
|
+
|
|
1515
|
+
This means:
|
|
1516
|
+
|
|
1517
|
+
* `frozen?` and `freeze` will behave as expected for all Farce classes.
|
|
1518
|
+
* `Kernel.instance_method(:frozen?).bind_call(object)` might report a different value from `object.frozen?`
|
|
1519
|
+
* C-level checks for frozen state, such as `RB_OBJ_FROZEN`, might differ from what Ruby-level methods report.
|
|
1520
|
+
|
|
1521
|
+
## Housekeeping
|
|
1522
|
+
|
|
1523
|
+
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.
|
|
1524
|
+
|
|
1525
|
+
Built with love in Berlin, by Konstantin Haase.
|