From: "ko1 (Koichi Sasada)" Date: 2021-09-14T04:42:32+00:00 Subject: [ruby-core:105240] [Ruby master Feature#18148] Marshal.load freeze option Issue #18148 has been updated by ko1 (Koichi Sasada). `Marshal.load()` accepts `proc` which can manipulate the loaded object: ```ruby str = Marshal.dump(["a", 1, 10 ** 10, 1.0, :foo]) p Marshal.load(str, proc {|obj| [obj]}) #=> [[["a"], [1], [10000000000], [1.0], [:foo]]] ``` When `freeze:true` is specified, only returned value from proc will be frozen? ---------------------------------------- Feature #18148: Marshal.load freeze option https://bugs.ruby-lang.org/issues/18148#change-93647 * 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: