From: ko1@... Date: 2020-11-25T19:32:27+00:00 Subject: [ruby-core:101070] [Ruby master Feature#17322] Deprecate `Random::DEFAULT` and introduce `Random.default()` method to provide Ractor-supported default random generator Issue #17322 has been updated by ko1 (Koichi Sasada). At the last dev-meeting, there is an idea to replace `Random::DEFAULT` with an object which delegate per-ractor random generators. Fortunately, `Random` class object can be good placeholder because it already has `Random.rand`, `Random.bytes` and so on. ``` Random::DEFAULT = Random p Random::DEFAULT.rand(2) #=> 0 or 1 ``` The specification change is: * `Random::DEFAULT` isn't `Random` instance. * `Random.rand()`, `Random.bytes()` and so on use per-ractor random generator. Maybe this is implementation dependent. JRuby and so on can use thread-local random generators. * `rand()`, `srand()` use `Random.rand()`, Random.srand()` respectively (but it doesn't trace `Random::DEFAULT` replacement as current implementation deosn't) * `Random.seed()` is added to return current seed of per-ractor default random generator. Advantages: * We don't need to change most of code. * We can introduce per-ractor (per-thread and so on) semantics naturally. Disadvantages => It can break the compatibility because `Random::DEFAULT` is a `Class` instance. * can not save the state of `Random::DEFAULT` * We can not use `dup` for it (there is a test using `Random::DEFAULT.dup` in test/ruby/test_random.rb). * We can not use marshal protocol to save the random generator states of `Random::DEFAULT`. * I'm not sure how it is important. * can not use instance methods of `Random` class * If user defines `Random#foo`, but `Random::DEFAULT.foo` is not available. I checked `Random::DEFAULT` usage with gem-codesearch and most of case it uses `Random::DEFAULT.rand()` and so on. I can't trace how to use `Random::DEFAULT` for the following 4 cases. ``` /srv/gems/kmat-0.0.3/lib/kmat/random.rb: Random::DEFAULT.randn(*args) /srv/gems/kmat-0.0.3/lib/kmat/random.rb: Random::DEFAULT.randn(*args) # I can't find the `Random#randn` definition. /srv/gems/prop_check-0.14.1/lib/prop_check/property.rb: rng = Random::DEFAULT # I can't find how to use `rng` /srv/gems/util-0.4.0/lib/util/args.rb: Random => Random::DEFAULT, # I can't find how to use this information. ``` This is implementaiton: https://github.com/ruby/ruby/pull/3813 ---------------------------------------- Feature #17322: Deprecate `Random::DEFAULT` and introduce `Random.default()` method to provide Ractor-supported default random generator https://bugs.ruby-lang.org/issues/17322#change-88745 * Author: ko1 (Koichi Sasada) * Status: Open * Priority: Normal ---------------------------------------- `Random::DEFAULT` a default random generator used by `rand`, `srand`, `Array#shuffle` without a given random generator, and so on. Random generators are not thread-safe, so they are not ractor safe, and they are not shareable. So a program refer to `Random::DEFAULT` on non-main ractor, it causes an error. To provide per-ractor default random generator, this ticket propose the `Random.default()` method which returns per-ractor random generator. `Random::DEFAULT` is a result of `Random.default()` on main-ractor and it should be deprecated, or at least it should not be used on multi-ractor supporting apps and libraries. -- https://bugs.ruby-lang.org/ Unsubscribe: