From: "mame (Yusuke Endoh)" <noreply@...>
Date: 2021-09-14T04:41:37+00:00
Subject: [ruby-core:105239] [Ruby master Feature#18148] Marshal.load freeze	option

Issue #18148 has been updated by mame (Yusuke Endoh).


It would be helpful for us if you cloud add a simple code example for a feature proposal.

```ruby=
dump = Marshal.dump(["foo", "bar", "baz"])

ary = Marshal.load(dump, freeze: true)

p ary #=> ["foo", "bar", "baz"]

p ary.frozen? #=> true
p ary[0].frozen? #=> true
```

----------------------------------------
Feature #18148: Marshal.load freeze option
https://bugs.ruby-lang.org/issues/18148#change-93646

* Author: byroot (Jean Boussier)
* Status: Open
* Priority: Normal
----------------------------------------
### Behavior

If passed `freeze: true`, all the deserialized objects should be frozen, and if possible, strings should be deduped.

This is similar to the `freeze` option recently added to `JSON` (https://github.com/flori/json/pull/447), `Psych` (https://github.com/ruby/psych/pull/414) and `MessagePack` (https://github.com/msgpack/msgpack-ruby/pull/194).

### Use cases

This option is useful in many scenarios:

  - If the deserialized data is meant to stay on the heap for the lifetime of the program, the string deduplication reduce the memory overhead, and all objects being frozen improve copy on write and ensure that static data isn't accidentally mutated.
  - If the deserialized data is used in a memory cache or similar, deep freezing it protect against mutation and allow to return the value directly without first deep cloning it.
  - While not very performant, it can be used as a `deep_freeze` mechanism with `Marshal.load(Marshal.dump(object), freeze: true)`.









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

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>