Feature #21962
openAdd deep_freeze for recursive freezing
Description
Motivation¶
It is common to want some data structure to be immutable, e.g. to ensure it doesn't get mutated by multiple threads.
See the first section of #21665 for a more detailed motivation, the summary is there is long-standing demand for it (back to #17145) for many reasons.
One way/workaround to do this currently is Ractor.make_shareable but its name suggests a Ractor-specific purpose.
The functionality of deeply freezing is separate from Ractor and it is generally useful.
Ractor.make_shareable almost has the right semantics and has been used for years so this proposal largely follows its semantics rather than reinventing the wheel.
The semantics also match the ice_nine gem, which further illustrates these semantics work well in practice and are expected.
Examples¶
It freezes deeply:
config = {
db: { host: ENV["HOST"], ports: [5432] },
flags: [:a, :b]
}.deep_freeze
config.frozen? # => true
config[:db].frozen? # => true
config[:db][:host].frozen? # => true
config[:db][:ports].frozen? # => true
Modules and classes are not frozen by deep_freeze:
It works for cyclic references:
It calls .freeze on each object:
class Stats
def initialize(samples)
@samples = samples
@mean = nil
end
def mean
@mean ||= @samples.sum / @samples.size.to_f
end
def freeze
mean
super
end
end
s = Stats.new([1.0, 2.0, 3.0])
s.deep_freeze
s.mean # => 2.0, would raise FrozenError instead if overridden #freeze was not called
Background¶
#21665 relaunched the discussion about deep_freeze and it was discussed in a dev meeting.
@headius (Charles Nutter) has said he won't have time to look at it soon.
So I am proposing something concrete which hopefully can be approved as-is.
Design¶
- Proposed:
Kernel#deep_freeze. Alternative:Object.deep_freeze.Kernel#deep_freezeis consistent withKernel#freeze.[a, b, c].deep_freezeis nicer, more concise and more idiomatic thanObject.deep_freeze([a, b, c]).- The proposal focuses on semantics; the exact method name and owner can be decided separately.
- The return value is the receiver.
deep_freezenever freezes modules and classes and does not visit their internal references- Same as
Ractor.make_shareableandIceNine.deep_freeze - No unexpected breakage because a class, its ancestors, their constants, etc all got frozen.
- Very easy to explain in the documentation.
- If people want to freeze classes/modules (rare case) they can with
.freezeand have control and can decide whether to freeze the ancestors, constants, class variables, etc.
- Same as
deep_freezecallsfreezeand ensures every object is frozen- Same as
Ractor.make_shareableandIceNine.deep_freeze - The already well-known hook to e.g. compute lazy caches eagerly, etc
- We still check that
.freezedid freeze the object, and raise aRuntimeErrorif not (same behavior and error message asRactor.make_shareable).
- Same as
- Not a protocol of defining
deep_freezein many classes.- That does not work with circular references.
- No need to invent a complex protocol when there is an existing working one (calling
.freeze) which has proven to work well (by being used byRactor.make_shareable).
- Handling circular references by maintaining internally a set of visited objects to avoid walking circular references multiple times
- As done in
obj_traverse_iinractor.c. - No expectation for calling
deep_freezerepeatedly on the same object to be fast - An object flag wouldn't work for this, for the case that
freezeraises an exception.
- As done in
- If some
freezemethod raises an exception, the exception is propagated.- Same as
Ractor.make_shareableandIceNine.deep_freeze - Some objects visited before the exception may already be frozen, same as
Ractor.make_shareable(unavoidable because we need to callfreezeas we traverse the object graph).
- Same as
deep_freezedoes not stop at already-frozen objects, because they may still reference unfrozen objects.- No special treatment for shareable objects, they are frozen too.
I think this addresses all the points mentioned in the dev meeting discussion and in #21665 and in #17145.
The traversal and freezing behavior closely follows Ractor.make_shareable, without introducing new semantics.
Implementation¶
The implementation in CRuby would be shared with Ractor.make_shareable, ractor.c already provides rb_obj_traverse() which is a great fit to implement this.
From a high-level view the implementation works like this:
deep_freeze recursively traverses the reachable object graph from the receiver.
For each visited object:
- If it is a
ClassorModule, it is not frozen and its internal references are not traversed. - Otherwise, Ruby calls
.freezeon the object. - After calling
freeze, Ruby verifies that the object is frozen. If it is not frozen, an exception is raised (same behavior and error message asRactor.make_shareable). - The traversal keeps a visited set to avoid infinite recursion on cycles.
Non-goals¶
This proposal is focused on defining deep_freeze and nothing else:
- This proposal does not define a new protocol requiring classes to implement custom recursive logic.
- This proposal does not change
Ractor.make_shareable. - This proposal does not freeze classes or modules.
- This proposal does not attempt to define immutability as a broader language concept.
Compatibility and risk¶
- The proposal adds a new API and does not change existing
freezebehavior. - It intentionally reuses existing
freezeoverrides on user objects. - It intentionally does not freeze classes/modules to avoid surprising breakage.
- Its semantics are close to existing
Ractor.make_shareable, reducing implementation risk.
Relation to existing methods¶
freeze: freezes only the receiver (shallow).deep_freeze: recursively freezes reachable objects.Ractor.make_shareable: recursively freezes reachable objects except if they are already shareable. Special handling to make some objects shareable.
Updated by Eregon (Benoit Daloze) 9 months ago
- Related to Feature #21665: Revisit Object#deep_freeze to support non-Ractor use cases added
Updated by Eregon (Benoit Daloze) 9 months ago
- Related to Feature #17145: Ractor-aware `Object#deep_freeze` added
Updated by matheusrich (Matheus Richard) 9 months ago
I can't remember any Ruby methods with the deep_ pattern (looks like Prism defines deep_freeze and Gem::ConfigFile has deep_transform_config_keys), this is intuitive for me (probably because of Rails).
I'd love to see it in core!
Updated by matz (Yukihiro Matsumoto) 8 months ago
Ractor.make_shareable does more than deep freezing and exists for Ractor. Its presence does not by itself justify deep_freeze in core.
In my opinion, deep freezing is a rare need in practice, and the ice_nine gem seems to cover it well.
Could you share concrete use cases where ice_nine is not sufficient and core inclusion is necessary?
Matz.
Updated by headius (Charles Nutter) 8 months ago
matz (Yukihiro Matsumoto) wrote in #note-4:
Ractor.make_shareabledoes more than deep freezing and exists for Ractor. Its presence does not by itself justifydeep_freezein core.
But deep freezing is the most visible effect, and it has been requested for core Ruby for years, maybe more than a decade now.
Ractor provides make_shareable to ensure objects can be safely used in parallel across Ractors without copying. The exact same justification applies to safely using objects across Threads, which are much more commonly used and provide parallel execution on jruby and truffleruby.
To me it makes perfect sense that this freezing capability be made general purpose rather than only available when Ractor is defined.
You also say it doesn't make sense to put it in core, but presumably Ractor will be part of core in the future. Given that everyone agrees that deep freezing is the safest way to make an object graph parallel-safe, why not move that capability to a non-Ractor location right now?
An additional reason: If jruby and truffleruby want to provide make_shareable, We need to define the Ractor namespace. But that indicates to user code that defined?(Ractor) is true, even if we don't implement the rest of Ractor's features. We are in a very difficult position because we want to provide the deep freezing capability but may not fully support Ractors for some time. What would you suggest we do?
In my opinion, deep freezing is a rare need in practice, and the
ice_ninegem seems to cover it well.
Deep freezing with make_shareable is essentially the only way to share objects across Ractors, which means this capability is going to be used very heavily in the future. Currently, in order to use this feature with threads, you must call Ractor.make_shareable. The resulting objects are just as safe for threads. Why not make that capability a more general core feature for users that don't need Ractor to do parallel execution?
Could you share concrete use cases where
ice_nineis not sufficient and core inclusion is necessary?
The concrete use cases are any place you want to pass around a graph of objects and know that it will not be mutated somewhere else. That obviously covers parallel execution use cases, but also cases where you are passing a graph of objects to a third party API and want to ensure they are not modified.
As pure Ruby in ice_nine, it's much slower than a built-in core method could be, and since it has to re-check the graph if called again, this performance difference compounds.
I would personally like to see deep freeze also set a "deep frozen" bit to avoid future traversals, in the same way as make_shareable.
I would like to turn this question around: What is the justification for keeping this feature specifically tied to Ractor when we have shown many cases where it is generally useful without Ractor? If a user has no interest in using Ractor, why do they need to call a Ractor method to get a deep frozen object graph?
Updated by byroot (Jean Boussier) 8 months ago
I don't have a strong opinion here, but one argument I could see for inclusion in core is to optimize frozen constants, e.g.
Which is much nicer than:
Updated by Eregon (Benoit Daloze) 8 months ago
At the meeting @matz (Yukihiro Matsumoto) said he was positive that deep_freeze is "freeze but recursively".
More precisely:
String.deep_freeze=> freezesStringlikeString.freezebut not anything further likeObjector so.
(This might unexpectedly freeze String in cases likeFOO = { String => -> { ... }, Array => -> { ... } }but is deemed acceptable)Foo.new.deep_freeze=> freezes that object but not classFoo.- Fiber and Thread (brought up by @ko1 (Koichi Sasada)):
.deep_freezeis the same as.freezeon them. - Recursive for Array elements, Hash pairs, Set, Struct, Data, other collection-like types.
- Recursive for Object instance variable values.
Updated by Eregon (Benoit Daloze) about 1 month ago
PR to implement deep_freeze per the last comment & discussion at the dev meeting at RubyKaigi:
https://github.com/ruby/ruby/pull/19529
Updated by Eregon (Benoit Daloze) 23 days ago
Some concrete use cases:
Here are some real-world examples of deep freezing objects other than Array, Hash and primitives.
They are user-defined objects, Structs, ASTs, config objects and so on.
Value objects which deep-freeze themselves¶
- dry-struct:
Dry::Struct::Valueis defined asdef self.new(*) = ::IceNine.deep_freeze(super), so every instance (a user-defined struct, whose attributes may be other structs) is deeply frozen. - adamantium is the "immutable object" mixin used across many gems (anima, yardstick, sql, axiom-types, ...). Its default freezer is
IceNine.deep_freeze!(value), applied to instances and memoized values. - ddd_sample_app_ruby: domain value objects (
Itinerary,Leg,Location,RouteSpecification,Delivery, ...) all end theirinitializewithIceNine.deep_freeze(self).
Configuration objects shared between threads/Ractors¶
- amqp-client builds a
Configurationholding aMessageCodecRegistryof parser/coder objects and callsRactor.make_shareable(copy)on it. That is effectively adeep_freezeof a custom object graph. - modern-ruby:
@configuration = IceNine.deep_freeze(DeepDup.deep_dup(configuration)), whereconfigurationis aModern::Configurationobject. It does the same for the API descriptor object graph. - otto hand-rolls
Otto::Core::Freezable#deep_freeze!, recursing overinstance_variablesand Hash/Array/Set. It is used for its security config objects.
ASTs / trees of node objects¶
- Prism itself:
Prism.parse(source, freeze: true)deeply freezes the whole result (nodes,Location,Source, comments, tokens). Because there is nodeep_freezein core, this needs hand-writtenSource#deep_freezeandToken#deep_freezeplus freezing threaded through the whole deserializer (templates/lib/prism/serialize.rb.erb). - juno: hand-written
AST::Node#deep_freeze!so the AST "can be shared read-only across threads". - kumi: a
Structwith a custom recursivedeep_freeze!over itschildren. - mahuta:
Node#deep_freeze!, which actually only freezes one level of children. This shows how easy it is to get these manual implementations wrong.
Others¶
- x-ruby:
X::Responseand itsParsedBodyare explicitly designed to support being "frozen deeply, asRactor.make_shareablefreezes one". - etengine:
IceNine.deep_freeze!(area_attr.with_indifferent_access)on cached data. That is aHashWithIndifferentAccess, a Hash subclass.
The common pattern is that people either depend on ice_nine, abuse Ractor.make_shareable purely for deep freezing, or write their own incomplete deep_freeze! (otto, juno, kumi, mahuta, Prism).
A core deep_freeze would replace all of these with a single correct implementation that handles cycles, all core types and ivars.
Updated by Eregon (Benoit Daloze) 23 days ago
· Edited
Regarding "Why not just ice_nine?" one more reason besides performance and "it belongs in the language, we shouldn't need a gem dependency to make constant values immutable",
is ice_nine doesn't work for T_DATA and notably Set, Struct, Data, Enumerator and for those it should clearly work (ice_nine could special-case the first 3 but not Enumerator and other T_DATA).
Updated by Eregon (Benoit Daloze) 13 days ago
This was discussed at the last dev meeting: https://github.com/ruby/dev-meeting-log/blob/master/2026/DevMeeting-2026-11-12.md#feature-21962-add-deep_freeze-for-recursive-freezing-eregon
I'm confused because IIRC twice matz agreed to the exact semantics proposed by this issue description (freeze all references, but never freeze modules & classes because that just breaks things), or with tiny tweaks.
But somehow after that agreement somehow other things were discussed like freeze_rec and there was no clear conclusion at the dev meeting.
freeze_rec/freeze_elements is I believe a separate proposal, and doesn't address many of the general use case (freezing an object graph/making objects immutable) or concrete use cases listed in https://bugs.ruby-lang.org/issues/21962#note-9.
So I think we should let that be its own ticket, and not stand in the way of this one.
Similarly the idea of a protocol was brought back, I think we already established at the RubyKaigi dev meeting this is too complex, slow, error-prone (easy to forget freezing ivars), and doesn't bring enough benefits.
For cases which want "some objects frozen but not all" that's a use case outside deep_freeze, where manual freezing is required.
About T_DATA objects after further thought I see no reason to treat them specially, I believe we should just freeze all references from there (as if using ObjectSpace.reachable_objects_from).
We can optimize well-known builtin classes to use something more efficient.
Finally a use case I care a lot about and I believe @headius (Charles Nutter) too, see here, is we have seen people use Ractor.make_shareable to make an object graph / the value of a constant immutable, even though what they intended is unrelated to Ractor (for example, they want to make sure objects in a constant in a gem is never mutated, and it's safe to never recompute something depending on that).
They use Ractor.make_shareable because that's in core, and there is no deep_freeze yet in core.
But that's awkward for many reasons:
- The intention is "freeze deeply"/"make immutable", yet
Ractor.make_shareablehas neither concept in its name Ractor.make_shareablehas different semantics, notably for Proc's, which are undesirable for deep freezing.Ractor.make_shareableis very long,A = [...].deep_freezeis more idiomatic and natural..deep_freezeis still helpful for Ractor compatibility, as most objects graphs made immutable bydeep_freezewill work just fine with Ractors.
Updated by Eregon (Benoit Daloze) 13 days ago
matz: except classes and modules, I agree. Traverse every other reference, including instance variables, array elements, and hash keys and values. Please update the PR.
Done, I updated https://github.com/ruby/ruby/pull/19529 to traverse and freeze every reference, except modules & classes (never frozen), and code-related references (e.g. self & captured variables for Proc/Binding, objects on the stack for Fiber/Thread/Ractor).
Updated by Earlopain (Earlopain _) 10 days ago
- Related to Feature #12008: Immutable object graphs (a.k.a. deep freeze) added