fast-freeze 0.1.0.pre1 → 0.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7ff72f65bddd89af80a089b3c5c3135565da8a1952e0e93f507398a386441783
4
- data.tar.gz: aa1e795f7c1aca3f46bf8f96dfe82d87972292f551cd8ac7780bfa38c6e23d20
3
+ metadata.gz: 454750ba55b98bfcee0cb18a2e3b392515cae0bcdc99f8883134afef6e147ed1
4
+ data.tar.gz: 0ab6bb221a5cd8d0d219bebd790b19080fba9289ddff8f9d2986b69f40c67098
5
5
  SHA512:
6
- metadata.gz: b1dd44681c7c2dc2ce7361f8f1652d76ec39818bab5f3d07565f86704258bc7d9547585964d3a11f79949725dafeae953362ddfcff1002e70d8b5821ef3c3c2f
7
- data.tar.gz: 20081b61ecf00b58820fcf9b6947c22be6530012ac4c72b43361321ada6f937dc98cf9554a9a99368c9dc596591a72d9b7f00541fb7e5dd81daf39e852609f04
6
+ metadata.gz: f474a4ca5e48de5b99cd1e88bac96bf57d4669c9ba21ff58b5a2250683b6e10947f851794cad0997e16d2a273a62227b7021506f8b1d3861d3f708769813cf70
7
+ data.tar.gz: 24943f5b4cc64e77930a50aad42148e1f353a2f7808160a4480ab61958c98a764a053d69307ef3d8a7195e5e711e5ffb55d9c9c12bbced28d7140290b04078cd
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
- ## 0.1.0 (unreleased)
3
+ ## 0.2.0 (2026-11-25)
4
+
5
+ - `trust_frozen:` now defaults to `false`. A container found already frozen is walked like any other, so one that was frozen by hand with unfrozen things in it is frozen through. Pass `trust_frozen: true` for the behaviour of 0.1.0, where a frozen container is skipped.
6
+
7
+ ## 0.1.0 (2026-11-25)
4
8
 
5
9
  - `Fast::Freeze.deep_freeze(value, trust_frozen: true, recurse_into_structs: false)`.
data/README.md CHANGED
@@ -58,7 +58,7 @@ Members are read from the object itself, not through its reader methods, so a re
58
58
 
59
59
  ## Options
60
60
 
61
- ### `trust_frozen:` (default `true`)
61
+ ### `trust_frozen:` (default `false`)
62
62
 
63
63
  Whether a container found already frozen is taken to be frozen all the way down.
64
64
 
@@ -66,14 +66,14 @@ Whether a container found already frozen is taken to be frozen all the way down.
66
66
  inner = +"text"
67
67
  list = [inner].freeze
68
68
 
69
- Fast::Freeze.deep_freeze(list) # returns at once
70
- inner.frozen? # => false
69
+ Fast::Freeze.deep_freeze(list, trust_frozen: true) # returns at once
70
+ inner.frozen? # => false
71
71
 
72
- Fast::Freeze.deep_freeze(list, trust_frozen: false) # walks it anyway
73
- inner.frozen? # => true
72
+ Fast::Freeze.deep_freeze(list) # walks it
73
+ inner.frozen? # => true
74
74
  ```
75
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.
76
+ By default a frozen container is walked like any other, so one that someone froze by hand without freezing its contents is frozen through. Pass `true` when every frozen container in the value is known to be frozen all the way down, such as one `deep_freeze` froze before: passing it again then costs one check.
77
77
 
78
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
79
 
@@ -96,7 +96,7 @@ page[0].text.frozen? # => true
96
96
 
97
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
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.
99
+ The two options are separate. With `trust_frozen: true` a frozen Array is passed over, along with any struct inside it.
100
100
 
101
101
  ## What it does not reach
102
102
 
@@ -111,7 +111,7 @@ The two options are separate. Under `trust_frozen: true` a frozen Array is still
111
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
112
  - **Cycles end.** A value that holds itself is frozen and the call returns, with every combination of options.
113
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.
114
+ - **No allocation.** A call allocates no objects. The exceptions are rare: nesting past 128 levels, and a call that opens more than 96 containers or structs that were already frozen.
115
115
  - **Errors pass through.** If a `freeze` raises, the error reaches the caller. Whatever was frozen before it stays frozen.
116
116
 
117
117
  ## Speed
@@ -120,19 +120,19 @@ Nanoseconds for one call, on Ruby 4.0.7 with YJIT on an Apple silicon laptop. `b
120
120
 
121
121
  | Value | fast-freeze | The same walk in Ruby | `Ractor.make_shareable` | ice_nine `deep_freeze!` |
122
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 |
123
+ | Small nested value (4 containers, 1 String) | 81 | 254 | 195 | 457 |
124
+ | Document of about 90 objects | 740 | 2,571 | 2,760 | 5,206 |
125
+ | Array of 1,000 such documents | 643,550 | 2,515,650 | 3,014,750 | 5,162,250 |
126
+ | Document that is already frozen | 533 | 2,253 | 1,898 | 59 |
127
+ | `Data` with 16 members | 169 | 937 | 407 | 102, and freezes nothing inside it |
128
128
 
129
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
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.
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, which is why it passes the frozen document at once. Plain `deep_freeze` walks everything again, frozen or not, and is slower on every row.
132
132
  - **`Ractor.make_shareable`** also walks instance variables, and raises on a value that cannot be shared between Ractors.
133
133
  - **The walk in Ruby** follows the same rules as `deep_freeze` with its defaults. It is in `benchmark/compare.rb`.
134
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.
135
+ With `trust_frozen: true` fast-freeze skips the frozen document too, in 56 ns. The other rows are unchanged by it.
136
136
 
137
137
  ## Development
138
138
 
@@ -1,5 +1,5 @@
1
1
  module Fast
2
2
  module Freeze
3
- VERSION = "0.1.0.pre1"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
data/lib/fast/freeze.rb CHANGED
@@ -24,16 +24,16 @@ module Fast
24
24
  # +recurse_into_structs+ says otherwise.
25
25
  #
26
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.
27
+ # When false, an Array, a Hash or a Set found already frozen is walked
28
+ # like any other. When true it is taken to be frozen all the way down and
29
+ # is not walked.
30
30
  # [recurse_into_structs]
31
31
  # When true, the members of every Data and Struct found inside +value+ are
32
32
  # walked too, whether or not it was found frozen.
33
33
  #
34
34
  # If a +freeze+ raises, the error is passed on and what was frozen before it
35
35
  # stays frozen.
36
- def self.deep_freeze(value, trust_frozen: true, recurse_into_structs: false)
36
+ def self.deep_freeze(value, trust_frozen: false, recurse_into_structs: false)
37
37
  __deep_freeze__(value, trust_frozen, recurse_into_structs)
38
38
  end
39
39
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fast-freeze
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.pre1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Lauer