fast-freeze 0.1.0-aarch64-linux-musl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cf73b718536c10ad422d41f40602230656d1f0e5316eaa531813a430f36f0ab5
4
+ data.tar.gz: 978fd405cf08ed96912cfb02c8595dd55446a345aebf2b21e97df5d63911d4e4
5
+ SHA512:
6
+ metadata.gz: 03d231637c9e4ff80ed8ebbcebaed2d0b1d3a6751e6b8899c4c705f40b4ae9ebb0561879e4a7540d4aca7ffe5ccbd551a68cc3c6fff336039d9d8615d7cfedbb
7
+ data.tar.gz: 65e00b9217db3362e8432dcb1236993e77860c7e065c40ca7c7235effba1e6aba2ea2f6ea3fc6f58c209ced65158ee87bfc94554e40a50bc31730a5a42ae17d5
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0 (2026-11-25)
4
+
5
+ - `Fast::Freeze.deep_freeze(value, trust_frozen: true, recurse_into_structs: false)`.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Josh Lauer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,160 @@
1
+ # fast-freeze
2
+
3
+ Deep freeze for Ruby values, written in C.
4
+
5
+ `Fast::Freeze.deep_freeze(value)` freezes a value and what it holds. It is one method. It adds nothing to `Object` or any other class, and it allocates nothing.
6
+
7
+ ```ruby
8
+ require "fast/freeze"
9
+
10
+ config = { "hosts" => ["a.example", "b.example"], "retry" => { "limit" => 3 } }
11
+ Fast::Freeze.deep_freeze(config)
12
+
13
+ config["hosts"] << "c.example" # FrozenError
14
+ config["retry"]["limit"] = 5 # FrozenError
15
+ config["hosts"][0] << ".test" # FrozenError
16
+ ```
17
+
18
+ ## Install
19
+
20
+ ```ruby
21
+ gem "fast-freeze"
22
+ ```
23
+
24
+ It needs Ruby 4.0 or later.
25
+
26
+ Precompiled gems are published for Linux on x86-64 and arm64, with glibc or musl, and for macOS on Apple silicon. On any other platform the gem compiles a small C extension when it is installed, which needs a C compiler.
27
+
28
+ ## What it freezes
29
+
30
+ | Found in the value | What happens |
31
+ |---|---|
32
+ | `nil`, `true`, `false`, a number, a Symbol | Nothing. It is frozen by nature. |
33
+ | A String | Frozen. |
34
+ | An Array | Frozen, and each element walked. |
35
+ | A Hash | Frozen, and each key and each value walked. |
36
+ | A Set | Frozen, and each element walked. |
37
+ | A class or a module | Left alone. |
38
+ | Any other object | `freeze` is called on it. It is not looked inside. |
39
+
40
+ A `Data` or a `Struct` is treated by where it is:
41
+
42
+ - **Passed to `deep_freeze`**, it is frozen and each of its members is walked. A `Data` is frozen from birth while the things it holds are not, and this is how you freeze those.
43
+ - **Found inside the value**, it is frozen and not looked inside, like any other object. `recurse_into_structs: true` changes that.
44
+
45
+ ```ruby
46
+ Point = Data.define(:name, :tags)
47
+
48
+ point = Point.new(name: +"origin", tags: [+"a"])
49
+ Fast::Freeze.deep_freeze(point)
50
+
51
+ point.name.frozen? # => true
52
+ point.tags.frozen? # => true
53
+ ```
54
+
55
+ Members are read from the object itself, not through its reader methods, so a reader that has been redefined changes nothing.
56
+
57
+ `deep_freeze` returns the value it was given.
58
+
59
+ ## Options
60
+
61
+ ### `trust_frozen:` (default `true`)
62
+
63
+ Whether a container found already frozen is taken to be frozen all the way down.
64
+
65
+ ```ruby
66
+ inner = +"text"
67
+ list = [inner].freeze
68
+
69
+ Fast::Freeze.deep_freeze(list) # returns at once
70
+ inner.frozen? # => false
71
+
72
+ Fast::Freeze.deep_freeze(list, trust_frozen: false) # walks it anyway
73
+ inner.frozen? # => true
74
+ ```
75
+
76
+ With the default, a value that `deep_freeze` has frozen before costs one check to pass again. Pass `false` when the value may hold a container that someone froze by hand without freezing its contents.
77
+
78
+ The option applies to an Array, a Hash and a Set. It does not apply to a `Data` or a `Struct` passed to `deep_freeze`, which is always opened.
79
+
80
+ ### `recurse_into_structs:` (default `false`)
81
+
82
+ Whether the members of every `Data` and `Struct` found inside the value are walked too.
83
+
84
+ ```ruby
85
+ Line = Struct.new(:text)
86
+
87
+ page = [Line.new(+"first")]
88
+ Fast::Freeze.deep_freeze(page)
89
+ page[0].frozen? # => true
90
+ page[0].text.frozen? # => false
91
+
92
+ page = [Line.new(+"first")]
93
+ Fast::Freeze.deep_freeze(page, recurse_into_structs: true)
94
+ page[0].text.frozen? # => true
95
+ ```
96
+
97
+ With this option a `Data` or a `Struct` found inside is opened whether or not it was already frozen. A `Data` is always frozen, so trusting it would make the option do nothing.
98
+
99
+ The two options are separate. Under `trust_frozen: true` a frozen Array is still passed over, along with any struct inside it. To reach every struct in a value that may already be partly frozen, set both.
100
+
101
+ ## What it does not reach
102
+
103
+ - **Inside any other object.** An object that is not one of the containers above has `freeze` called and nothing more. Its instance variables are not walked. If a class of yours holds things that should freeze with it, define `freeze` on the class to freeze them; `deep_freeze` calls it.
104
+ - **The default value of a Hash.** After `Fast::Freeze.deep_freeze(Hash.new([]))` the Hash is frozen and the Array it hands out for a missing key is not.
105
+ - **The ends of a Range.** A Range is frozen and not looked inside.
106
+ - **What a Proc or a Method can reach.** A Proc is frozen. The variables it closes over are not walked.
107
+
108
+ ## What you can rely on
109
+
110
+ - **Subclasses keep their own `freeze`.** A plain String, Array or Hash is frozen directly in C. A subclass of one, or an object with a singleton class, has its `freeze` method called.
111
+ - **Shared objects cost once.** A container reached by many paths is walked a bounded number of times, never once per path. The work grows with the size of the value, whatever it shares.
112
+ - **Cycles end.** A value that holds itself is frozen and the call returns, with every combination of options.
113
+ - **Any depth.** Nesting does not raise or overflow the stack, on a thread or on a fiber. Past 128 levels the walk continues from a list instead of going deeper on the stack.
114
+ - **No allocation.** A call allocates no objects. The exceptions are rare: nesting past 128 levels, and a call with either option set that opens more than 96 already-frozen containers.
115
+ - **Errors pass through.** If a `freeze` raises, the error reaches the caller. Whatever was frozen before it stays frozen.
116
+
117
+ ## Speed
118
+
119
+ Nanoseconds for one call, on Ruby 4.0.7 with YJIT on an Apple silicon laptop. `bundle exec rake benchmark` runs it on yours.
120
+
121
+ | Value | fast-freeze | The same walk in Ruby | `Ractor.make_shareable` | ice_nine `deep_freeze!` |
122
+ |---|---|---|---|---|
123
+ | Small nested value (4 containers, 1 String) | 84 | 213 | 198 | 461 |
124
+ | Document of about 90 objects | 719 | 2,366 | 2,798 | 5,323 |
125
+ | Array of 1,000 such documents | 675,500 | 2,326,400 | 3,025,100 | 5,302,750 |
126
+ | Document that is already frozen | 34 | 52 | 1,914 | 54 |
127
+ | `Data` with 16 members | 170 | 822 | 422 | 98, and freezes nothing inside it |
128
+
129
+ These do not all do the same work, so read the table as how fast each one is at its own idea of a deep freeze:
130
+
131
+ - **ice_nine** also walks the instance variables of every object, and it does not look inside a `Data`. `deep_freeze!` is its variant that skips what is already frozen. Plain `deep_freeze` walks everything again, frozen or not, and is slower on every row.
132
+ - **`Ractor.make_shareable`** also walks instance variables, and raises on a value that cannot be shared between Ractors.
133
+ - **The walk in Ruby** follows the same rules as `deep_freeze` with its defaults. It is in `benchmark/compare.rb`.
134
+
135
+ With `trust_frozen: false` the first three rows are unchanged, the frozen document takes 584 ns because it is walked again, and the `Data` takes 207 ns.
136
+
137
+ ## Development
138
+
139
+ ```
140
+ bundle install
141
+ bundle exec rake # compile, then run the specs
142
+ GC_STRESS=true bundle exec rake spec # the same, with a collection at every allocation
143
+ bundle exec rake benchmark
144
+ ```
145
+
146
+ CI runs both spec commands on Linux, macOS and Windows.
147
+
148
+ ## Releasing
149
+
150
+ Pushing a tag named for the version, such as `v0.1.0`, runs the release workflow. It builds the source gem and one precompiled gem for each platform, installs each precompiled gem on its own platform and runs it, and then publishes them all to RubyGems.
151
+
152
+ - Set `Fast::Freeze::VERSION` and add the version to `CHANGELOG.md` first. The workflow refuses a tag that does not match the version.
153
+ - Running the workflow by hand from the Actions tab builds and verifies the gems and publishes nothing.
154
+ - If publishing fails partway, run the failed job again. It skips the gems RubyGems already has and pushes the rest.
155
+ - Publishing uses RubyGems trusted publishing, so the repository holds no API key. RubyGems must list this repository and `release.yml` as a trusted publisher for the gem.
156
+ - `bundle exec rake gem:x86_64-linux-gnu` builds one precompiled gem locally. It needs Docker.
157
+
158
+ ## Licence
159
+
160
+ MIT. See `LICENSE.txt`.
@@ -0,0 +1,3 @@
1
+ require "mkmf"
2
+
3
+ create_makefile("fast/freeze/freeze")
@@ -0,0 +1,336 @@
1
+ #include <ruby.h>
2
+ #include <string.h>
3
+
4
+ /*
5
+ * How deep the walk goes on the C stack. A container nested deeper than this
6
+ * is put aside and opened afterwards from the top, so a value of any depth is
7
+ * frozen without the walk outgrowing the stack of the thread or fiber it runs
8
+ * on. A level costs up to about 530 bytes of stack (measured on arm64, a Hash
9
+ * reached through a key), so this many come to under 70 KB.
10
+ */
11
+ #define MAX_RECURSION 128
12
+
13
+ /*
14
+ * The record of what one call has opened lives on the C stack until it holds
15
+ * SEEN_INLINE_MAX objects, then moves to an identity Hash. SEEN_SLOTS must be
16
+ * a power of two.
17
+ */
18
+ #define SEEN_SLOTS 128
19
+ #define SEEN_INLINE_MAX 96
20
+
21
+ static ID id_freeze;
22
+ static ID id_compare_by_identity;
23
+ static VALUE cData;
24
+
25
+ struct walk {
26
+ int trust_frozen;
27
+ int recurse_into_structs;
28
+ int seen_count; /* objects in seen_inline; -1 until the record is first used */
29
+ VALUE seen_table; /* the identity Hash, once seen_inline is full */
30
+ VALUE deferred; /* an Array of the containers put aside at MAX_RECURSION */
31
+ VALUE seen_inline[SEEN_SLOTS];
32
+ };
33
+
34
+ struct level {
35
+ struct walk *w;
36
+ int depth;
37
+ };
38
+
39
+ static void visit(struct walk *w, VALUE v, int depth);
40
+
41
+ /* ---- the record of what this call has opened ---- */
42
+
43
+ static inline unsigned
44
+ seen_slot(VALUE v)
45
+ {
46
+ /* Objects are aligned, so the low bits of an address say nothing. */
47
+ size_t h = (size_t)v >> 3;
48
+ h ^= h >> 7;
49
+ h ^= h >> 14;
50
+ return (unsigned)(h & (SEEN_SLOTS - 1));
51
+ }
52
+
53
+ /*
54
+ * Records an object, and says whether it was recorded already.
55
+ *
56
+ * The objects in seen_inline are found by the garbage collector's scan of the
57
+ * C stack, which keeps each one alive and at its address. The identity Hash
58
+ * does the same for its keys. Neither holds an address that could come to
59
+ * mean a different object during the call.
60
+ */
61
+ static int
62
+ seen(struct walk *w, VALUE v)
63
+ {
64
+ unsigned i;
65
+
66
+ if (w->seen_table != Qfalse) {
67
+ if (rb_hash_lookup2(w->seen_table, v, Qfalse) != Qfalse) return 1;
68
+ rb_hash_aset(w->seen_table, v, Qtrue);
69
+ return 0;
70
+ }
71
+
72
+ if (w->seen_count < 0) {
73
+ memset(w->seen_inline, 0, sizeof(w->seen_inline));
74
+ w->seen_count = 0;
75
+ }
76
+
77
+ for (i = seen_slot(v); w->seen_inline[i]; i = (i + 1) & (SEEN_SLOTS - 1)) {
78
+ if (w->seen_inline[i] == v) return 1;
79
+ }
80
+
81
+ if (w->seen_count == SEEN_INLINE_MAX) {
82
+ w->seen_table = rb_funcallv(rb_hash_new(), id_compare_by_identity, 0, 0);
83
+ for (i = 0; i < SEEN_SLOTS; i++) {
84
+ if (w->seen_inline[i]) rb_hash_aset(w->seen_table, w->seen_inline[i], Qtrue);
85
+ }
86
+ rb_hash_aset(w->seen_table, v, Qtrue);
87
+ return 0;
88
+ }
89
+
90
+ w->seen_inline[i] = v;
91
+ w->seen_count++;
92
+ return 0;
93
+ }
94
+
95
+ /* ---- freezing one object ---- */
96
+
97
+ /* Calls the object's own freeze. A BasicObject has none, and is frozen directly. */
98
+ static void
99
+ call_freeze(VALUE v)
100
+ {
101
+ if (rb_check_funcall(v, id_freeze, 0, 0) == Qundef) rb_obj_freeze(v);
102
+ }
103
+
104
+ /*
105
+ * A plain String, Array or Hash is frozen here without a method call. A
106
+ * subclass, or an object with a singleton class, has its own freeze called.
107
+ */
108
+ static void
109
+ freeze_string(VALUE v)
110
+ {
111
+ if (RBASIC_CLASS(v) == rb_cString) rb_str_freeze(v);
112
+ else call_freeze(v);
113
+ }
114
+
115
+ static void
116
+ freeze_array(VALUE v)
117
+ {
118
+ if (RBASIC_CLASS(v) == rb_cArray) rb_ary_freeze(v);
119
+ else call_freeze(v);
120
+ }
121
+
122
+ static void
123
+ freeze_hash(VALUE v)
124
+ {
125
+ if (RBASIC_CLASS(v) == rb_cHash) rb_hash_freeze(v);
126
+ else call_freeze(v);
127
+ }
128
+
129
+ static void
130
+ freeze_object(VALUE v)
131
+ {
132
+ if (!RB_OBJ_FROZEN_RAW(v)) call_freeze(v);
133
+ }
134
+
135
+ /* ---- what counts as a container ---- */
136
+
137
+ /* A Range is laid out as a struct too, and is not one. */
138
+ static int
139
+ is_struct(VALUE v)
140
+ {
141
+ return RTEST(rb_obj_is_kind_of(v, cData)) || RTEST(rb_obj_is_kind_of(v, rb_cStruct));
142
+ }
143
+
144
+ static int
145
+ is_set(VALUE v)
146
+ {
147
+ return RTEST(rb_obj_is_kind_of(v, rb_cSet));
148
+ }
149
+
150
+ /*
151
+ * Whether to open a container that has just been found and, if it was not
152
+ * frozen, has just had its freeze called.
153
+ *
154
+ * A container found frozen is opened once per call at most, and not at all
155
+ * when frozen containers are trusted. One this call froze is not recorded,
156
+ * so a value that shares nothing never touches the record; if such a
157
+ * container is reached a second time it is found frozen, recorded then, and
158
+ * opened once more. That keeps the walk linear in the size of the value
159
+ * whatever it shares, and makes a cycle end.
160
+ */
161
+ static int
162
+ should_open(struct walk *w, VALUE v, int was_frozen, int trust)
163
+ {
164
+ if (was_frozen) return !trust && !seen(w, v);
165
+
166
+ /* Its own freeze left it unfrozen, so only the record can stop a second visit. */
167
+ if (!RB_OBJ_FROZEN_RAW(v)) return !seen(w, v);
168
+
169
+ return 1;
170
+ }
171
+
172
+ /* ---- opening a container ---- */
173
+
174
+ static int
175
+ hash_entry(VALUE key, VALUE value, VALUE arg)
176
+ {
177
+ struct level *level = (struct level *)arg;
178
+
179
+ visit(level->w, key, level->depth);
180
+ visit(level->w, value, level->depth);
181
+ return ST_CONTINUE;
182
+ }
183
+
184
+ static int
185
+ set_element(VALUE element, VALUE arg)
186
+ {
187
+ struct level *level = (struct level *)arg;
188
+
189
+ visit(level->w, element, level->depth);
190
+ return ST_CONTINUE;
191
+ }
192
+
193
+ /* Walks what an Array, a Hash, a Set, a Data or a Struct holds. */
194
+ static void
195
+ walk_into(struct walk *w, VALUE v, int depth)
196
+ {
197
+ struct level level;
198
+ long i, n;
199
+
200
+ if (depth > MAX_RECURSION) {
201
+ if (w->deferred == Qfalse) w->deferred = rb_ary_new();
202
+ rb_ary_push(w->deferred, v);
203
+ return;
204
+ }
205
+
206
+ level.w = w;
207
+ level.depth = depth;
208
+
209
+ switch (RB_BUILTIN_TYPE(v)) {
210
+ case T_ARRAY:
211
+ /* The length is read again each time: a freeze called below may run any code. */
212
+ for (i = 0; i < RARRAY_LEN(v); i++) visit(w, RARRAY_AREF(v, i), depth);
213
+ break;
214
+
215
+ case T_HASH:
216
+ rb_hash_foreach(v, hash_entry, (VALUE)&level);
217
+ break;
218
+
219
+ case T_STRUCT:
220
+ n = RSTRUCT_LEN(v);
221
+ for (i = 0; i < n; i++) visit(w, RSTRUCT_GET(v, (int)i), depth);
222
+ break;
223
+
224
+ default:
225
+ rb_set_foreach(v, set_element, (VALUE)&level);
226
+ break;
227
+ }
228
+
229
+ RB_GC_GUARD(v);
230
+ }
231
+
232
+ /* Freezes one value found during the walk, and opens it if it is a container. */
233
+ static void
234
+ visit(struct walk *w, VALUE v, int depth)
235
+ {
236
+ int frozen;
237
+
238
+ if (RB_SPECIAL_CONST_P(v)) return;
239
+
240
+ frozen = RB_OBJ_FROZEN_RAW(v) ? 1 : 0;
241
+
242
+ switch (RB_BUILTIN_TYPE(v)) {
243
+ case T_STRING:
244
+ if (!frozen) freeze_string(v);
245
+ return;
246
+
247
+ case T_ARRAY:
248
+ if (!frozen) freeze_array(v);
249
+ if (!should_open(w, v, frozen, w->trust_frozen)) return;
250
+ break;
251
+
252
+ case T_HASH:
253
+ if (!frozen) freeze_hash(v);
254
+ if (!should_open(w, v, frozen, w->trust_frozen)) return;
255
+ break;
256
+
257
+ case T_STRUCT:
258
+ if (!frozen) call_freeze(v);
259
+ if (!w->recurse_into_structs || !is_struct(v)) return;
260
+ /* A Data is always found frozen, so trust has no say over a struct. */
261
+ if (!should_open(w, v, frozen, 0)) return;
262
+ break;
263
+
264
+ case T_CLASS:
265
+ case T_MODULE:
266
+ return;
267
+
268
+ case T_OBJECT:
269
+ case T_DATA:
270
+ if (is_set(v)) {
271
+ if (!frozen) call_freeze(v);
272
+ if (!should_open(w, v, frozen, w->trust_frozen)) return;
273
+ break;
274
+ }
275
+ /* fall through */
276
+
277
+ default:
278
+ if (!frozen) call_freeze(v);
279
+ return;
280
+ }
281
+
282
+ walk_into(w, v, depth + 1);
283
+ }
284
+
285
+ /*
286
+ * Fast::Freeze.__deep_freeze__(value, trust_frozen, recurse_into_structs)
287
+ *
288
+ * The flags are positional here because a keyword argument read in C costs
289
+ * two objects a call. Fast::Freeze.deep_freeze reads them as keywords in Ruby,
290
+ * which costs none, and passes them on.
291
+ */
292
+ static VALUE
293
+ ff_deep_freeze(VALUE mod, VALUE value, VALUE trust_frozen, VALUE recurse_into_structs)
294
+ {
295
+ struct walk w;
296
+
297
+ w.trust_frozen = RTEST(trust_frozen);
298
+ w.recurse_into_structs = RTEST(recurse_into_structs);
299
+ w.seen_count = -1;
300
+ w.seen_table = Qfalse;
301
+ w.deferred = Qfalse;
302
+
303
+ if (!RB_SPECIAL_CONST_P(value) && RB_BUILTIN_TYPE(value) == T_STRUCT && is_struct(value)) {
304
+ /* A Data or a Struct passed in is always opened, frozen or not. */
305
+ freeze_object(value);
306
+ if (w.recurse_into_structs) seen(&w, value);
307
+ walk_into(&w, value, 1);
308
+ }
309
+ else {
310
+ visit(&w, value, 0);
311
+ }
312
+
313
+ while (w.deferred != Qfalse && RARRAY_LEN(w.deferred) > 0) {
314
+ walk_into(&w, rb_ary_pop(w.deferred), 1);
315
+ }
316
+
317
+ return value;
318
+ }
319
+
320
+ void
321
+ Init_freeze(void)
322
+ {
323
+ VALUE mFast, mFreeze;
324
+
325
+ rb_ext_ractor_safe(true);
326
+
327
+ id_freeze = rb_intern("freeze");
328
+ id_compare_by_identity = rb_intern("compare_by_identity");
329
+
330
+ cData = rb_path2class("Data");
331
+ rb_gc_register_mark_object(cData);
332
+
333
+ mFast = rb_define_module("Fast");
334
+ mFreeze = rb_define_module_under(mFast, "Freeze");
335
+ rb_define_private_method(rb_singleton_class(mFreeze), "__deep_freeze__", ff_deep_freeze, 3);
336
+ }
Binary file
@@ -0,0 +1,5 @@
1
+ module Fast
2
+ module Freeze
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,48 @@
1
+ require "fast/freeze/version"
2
+
3
+ # A precompiled gem built for several Ruby versions keeps one build of the
4
+ # extension for each, in a directory named for the version. A gem compiled on
5
+ # install, or built for one version, has a single build beside this file.
6
+ begin
7
+ require "fast/freeze/#{RUBY_VERSION[/\d+\.\d+/]}/freeze"
8
+ rescue LoadError
9
+ require "fast/freeze/freeze"
10
+ end
11
+
12
+ module Fast
13
+ # Deep freeze for Ruby values, written in C.
14
+ module Freeze
15
+ # Freezes +value+ and what it holds, and returns it.
16
+ #
17
+ # A String is frozen. An Array, a Hash and a Set are frozen and walked: the
18
+ # elements of an Array or a Set, the keys and the values of a Hash. A class
19
+ # or a module is left alone. Any other object has +freeze+ called on it and
20
+ # is not looked inside.
21
+ #
22
+ # A Data or a Struct passed as +value+ is frozen and its members are walked.
23
+ # One found inside +value+ is frozen and not looked inside, unless
24
+ # +recurse_into_structs+ says otherwise.
25
+ #
26
+ # [trust_frozen]
27
+ # When true, an Array, a Hash or a Set found already frozen is taken to be
28
+ # frozen all the way down and is not walked. When false it is walked like
29
+ # any other.
30
+ # [recurse_into_structs]
31
+ # When true, the members of every Data and Struct found inside +value+ are
32
+ # walked too, whether or not it was found frozen.
33
+ #
34
+ # If a +freeze+ raises, the error is passed on and what was frozen before it
35
+ # stays frozen.
36
+ def self.deep_freeze(value, trust_frozen: true, recurse_into_structs: false)
37
+ __deep_freeze__(value, trust_frozen, recurse_into_structs)
38
+ end
39
+
40
+ # The walk reads the members of a Data from its slots. This confirms at load
41
+ # that it finds them on this Ruby.
42
+ probe = Data.define(:first, :second).new(first: +"a", second: [+"b"])
43
+ deep_freeze(probe)
44
+ unless probe.first.frozen? && probe.second.frozen? && probe.second.first.frozen?
45
+ raise LoadError, "fast-freeze cannot read the members of a Data on #{RUBY_DESCRIPTION}"
46
+ end
47
+ end
48
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fast-freeze
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: aarch64-linux-musl
6
+ authors:
7
+ - Josh Lauer
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: 'Freezes a value and what it holds: Strings, Arrays, Hashes (keys and
13
+ values), Sets, and the members of a Data or a Struct. One method, no monkey patches,
14
+ no allocation.'
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - CHANGELOG.md
20
+ - LICENSE.txt
21
+ - README.md
22
+ - ext/fast/freeze/extconf.rb
23
+ - ext/fast/freeze/freeze.c
24
+ - lib/fast/freeze.rb
25
+ - lib/fast/freeze/freeze.so
26
+ - lib/fast/freeze/version.rb
27
+ homepage: https://github.com/josh-lauer/fast-freeze
28
+ licenses:
29
+ - MIT
30
+ metadata:
31
+ bug_tracker_uri: https://github.com/josh-lauer/fast-freeze/issues
32
+ changelog_uri: https://github.com/josh-lauer/fast-freeze/blob/main/CHANGELOG.md
33
+ homepage_uri: https://github.com/josh-lauer/fast-freeze
34
+ source_code_uri: https://github.com/josh-lauer/fast-freeze
35
+ rubygems_mfa_required: 'true'
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '4.0'
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: 4.1.dev
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: 3.3.22
52
+ requirements: []
53
+ rubygems_version: 4.0.6
54
+ specification_version: 4
55
+ summary: Deep freeze for Ruby values, written in C.
56
+ test_files: []