ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:100466] [Ruby master Feature#17273] shareable_constant_value pragma
@ 2020-10-21  5:56 ko1
  2020-10-21 18:58 ` [ruby-core:100477] " eregontp
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: ko1 @ 2020-10-21  5:56 UTC (permalink / raw)
  To: ruby-core

Issue #17273 has been reported by ko1 (Koichi Sasada).

----------------------------------------
Feature #17273: shareable_constant_value pragma
https://bugs.ruby-lang.org/issues/17273

* Author: ko1 (Koichi Sasada)
* Status: Open
* Priority: Normal
----------------------------------------
This proposal is to introduce `# shareable_constant_value: true` pragma to make constant values shareable objects.
With this pragma, you don't need to add `freeze` to access from non-main ractors.

```ruby
# shareable_constant_value: true

A = [1, [2, [3, 4]]]
H = {a: "a"}

Ractor.new do
  p A
  p H
end.take
```

## Background

Now, we can not access constants which contains a unshareable object from the non-main Ractor.

```ruby
A = [1, [2, [3, 4]]]
H = {a: "a"}

Ractor.new do
  p A #=> can not access non-sharable objects in constant Object::A by non-main Ractor. (NameError)
  p H
end.take
```

If `H` is frozen object, we can freeze them.


```ruby
A = [1, [2, [3, 4].freeze].freeze].freeze
H = {a: "a".freeze}.freeze

Ractor.new do
  p A #=> [1, [2, [3, 4]]]
  p H #=> {:a=>"a"}
end.take
```

Adding nesting data structure, we need many `.freeze` method.
Recently, I added `Ractor.make_shareable(obj)` makes `obj` shareable with depply freezing object (I'll file a ticket soon).
We only need to introduce this method for each constant.

```ruby
A = Ractor.make_shareable( [1, [2, [3, 4]]] )
H = Ractor.make_shareable( {a: "a"} )

Ractor.new do
  p A #=> [1, [2, [3, 4]]]
  p H #=> {:a=>"a"}
end.take
```

However, if we have 100 constants, it is troublesome.

## Proposal

With `# shareable_constant_value: true`, you can specify all constants are shareable.

```ruby
# shareable_constant_value: true

A = [1, [2, [3, 4]]]
# compiled with: A = Ractor.make_shareable( [1, [2, [3, 4]]] )
H = {a: "a"}
# compiled with: H = Ractor.make_shareable( {a: "a"} )

Ractor.new do
  p A
  p H
end.take
```

(Strictly speaking, don't call `Ractor.make_shareable`, but apply same effect. This means rewriting `Ractor.make_shareable` doesn't affect this behavior)

You can specify `# shareable_constant_value: false` in the middle of the place.

```ruby
# shareable_constant_value: true

S1 = 'str' #
p S1.frozen? #=> true

# shareable_constant_value: false

S2 = 'str' #
p S2.frozen? #=> false
```

`Ractor.make_shareable(obj)` doesn't affect shareable objects.


```ruby
# shareable_constant_value: true
class C; end

D = C
p D.frozen? #=> false
```

Some objects can not become shareable objects, so it raises an exception:

```ruby
# shareable_constant_value: true

T = Thread.new{}
#=> `make_shareable': can not make shareable object for #<Thread:0x000055952e40ffb0 /home/ko1/ruby/src/trunk/test.rb:3 run> (Ractor::Error)
```

## Implementation

https://github.com/ruby/ruby/pull/3681/files



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2020-12-16 18:36 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-21  5:56 [ruby-core:100466] [Ruby master Feature#17273] shareable_constant_value pragma ko1
2020-10-21 18:58 ` [ruby-core:100477] " eregontp
2020-10-22  0:46 ` [ruby-core:100484] " ko1
2020-10-22  0:52 ` [ruby-core:100485] " ko1
2020-10-23 21:23 ` [ruby-core:100517] " marcandre-ruby-core
2020-10-25 12:57 ` [ruby-core:100528] " eregontp
2020-10-26 16:29 ` [ruby-core:100581] " ko1
2020-10-29 16:14 ` [ruby-core:100647] " ko1
2020-10-29 19:26 ` [ruby-core:100652] " eregontp
2020-10-29 19:29 ` [ruby-core:100653] " eregontp
2020-10-29 19:33 ` [ruby-core:100654] " eregontp
2020-10-29 19:47 ` [ruby-core:100657] " ko1
2020-10-29 19:53 ` [ruby-core:100659] " merch-redmine
2020-12-16 18:36 ` [ruby-core:101473] " marcandre-ruby-core

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).