ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: marcandre-ruby-core@marc-andre.ca
To: ruby-core@ruby-lang.org
Subject: [ruby-core:100282] [Ruby master Feature#17145] Ractor-aware `Object#deep_freeze`
Date: Fri, 02 Oct 2020 16:10:25 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-87868.20201002161024.182@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-17145.20200903184227.182@ruby-lang.org

Issue #17145 has been updated by marcandre (Marc-Andre Lafortune).


Maybe we can start with `Ractor.shareable(obj)`?

The major reason why we need it is that there is no way to create a Ractor sharable recursive structure currently.

```ruby
child = {type: :foo}
node = {type: bar, children: [child].freeze}.freeze
child[:parent] = node
child.freeze
```

At this point, everything is frozen, but neither child nor node are Ractor sharable.

`Ractor.shareable(node)` would check for this, and set both `node` and `child`'s "shareable" flag; after that `Ractor.shareable?(node)` would return true.

API-wise, `Ractor.shareable(node)` might be a good start, or `node.freeze(shareable: true)`, or `node.deep_freeze`

----------------------------------------
Feature #17145: Ractor-aware `Object#deep_freeze`
https://bugs.ruby-lang.org/issues/17145#change-87868

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
----------------------------------------
I'd like to propose `Object#deep_freeze`:

Freezes recursively the contents of the receiver (by calling `deep_freeze`) and
then the receiver itself (by calling `freeze`).
Values that are shareable via `Ractor` (e.g. classes) are never frozen this way.

```ruby
# freezes recursively:
ast = [:hash, [:pair, [:str, 'hello'], [:sym, :world]]].deep_freeze
ast.dig(1, 1) # => [:str, 'hello']
ast.dig(1, 1).compact! # => FrozenError

# does not freeze classes:
[[String]].deep_freeze
String.frozen? # => false

# calls `freeze`:
class Foo
  def freeze
    build_cache!
    puts "Ready for freeze"
    super
  end
  # ...
end
[[[Foo.new]]].deep_freeze # => Outputs "Ready for freeze"
```


I think a variant `deep_freeze!` that raises an exception if the result isn't Ractor-shareable would be useful too:

```ruby
class Fire
  def freeze
    # do not call super
  end
end

x = [Fire.new]
x.deep_freeze! # => "Could not be deeply-frozen: #<Fire:0x00007ff151994748>"
```



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

  parent reply	other threads:[~2020-10-02 16:10 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-03 18:42 [ruby-core:99885] [Ruby master Feature#17145] Ractor-aware `Object#deep_freeze` marcandre-ruby-core
2020-09-03 19:06 ` [ruby-core:99888] " eregontp
2020-09-03 19:20 ` [ruby-core:99889] " ko1
2020-09-03 19:27 ` [ruby-core:99890] " eregontp
2020-09-03 19:34 ` [ruby-core:99892] " marcandre-ruby-core
2020-09-03 19:39 ` [ruby-core:99893] " marcandre-ruby-core
2020-09-03 20:44 ` [ruby-core:99900] " ko1
2020-09-04  2:55 ` [ruby-core:99912] " duerst
2020-09-20 17:57 ` [ruby-core:100055] " eregontp
2020-09-21 13:50 ` [ruby-core:100060] " marcandre-ruby-core
2020-10-02  8:44 ` [ruby-core:100277] " shatrov
2020-10-02 16:10 ` marcandre-ruby-core [this message]
2020-10-19 20:48 ` [ruby-core:100429] " ko1
2020-10-19 20:53 ` [ruby-core:100430] " ko1
2020-10-19 21:24 ` [ruby-core:100431] " marcandre-ruby-core
2020-10-20  0:43 ` [ruby-core:100438] " ko1
2020-10-20 10:00 ` [ruby-core:100444] " marcandre-ruby-core
2020-10-20 20:23 ` [ruby-core:100452] " eregontp
2020-10-20 20:42 ` [ruby-core:100454] " eregontp
2020-10-20 21:42 ` [ruby-core:100460] " marcandre-ruby-core
2020-10-21  5:23 ` [ruby-core:100464] " marcandre-ruby-core
2020-10-21 13:28 ` [ruby-core:100474] " daniel
2020-10-21 19:03 ` [ruby-core:100478] " eregontp
2020-10-23 20:00 ` [ruby-core:100514] " ko1
2020-10-25 13:45 ` [ruby-core:100533] " eregontp
2020-10-25 19:33 ` [ruby-core:100535] " marcandre-ruby-core
2020-10-25 21:30 ` [ruby-core:100539] " daniel
2020-10-26  5:28 ` [ruby-core:100554] " marcandre-ruby-core
2020-10-26  7:46 ` [ruby-core:100559] " eregontp
2020-10-26  7:58 ` [ruby-core:100563] " matz
2020-10-26  9:02 ` [ruby-core:100565] " matz
2020-10-26 13:36 ` [ruby-core:100570] " daniel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-87868.20201002161024.182@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).