sin_deep_merge 1.0.1-java → 2.0.0-java

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: d0c1f294bf953936c294c496a3a116a9222505b60ea83fe910f0d6b70de0f409
4
- data.tar.gz: e0da19c5cc03fa03c639da9e99eb816620c48b52aff1ccfcfdde45fb84443a6c
3
+ metadata.gz: 89dc6c17288062af4f4ad5393b72d79c23d823125c0cf3a5bd72f914880cb575
4
+ data.tar.gz: 16b93dedc92c7e8b3ed0c1c3c0d02e1eba204f7e923f7dafb6753ddbd7162c64
5
5
  SHA512:
6
- metadata.gz: ba916aff9607573eea347b6b400b8b84fb4797810b557e4f6cb6f5704712828c06c575b0973e69c11664542635463307455b1abfa9ee000f2fb8acfee32c417b
7
- data.tar.gz: 2fe051716c036e3908eaa65051fede2bf57acaa8518636e86596d6df0881d5e7f76544492fb45611a600ab2495ec298e2665b5b9a75feaf703fed712a1acd53b
6
+ metadata.gz: e973f3173765871b89009aa1baf474c2533795660b9f448a53441aae10898c1205f449654b056d45bd104e565dccb4b7ec02b5fffc3b2ef5496662b7ad11e591
7
+ data.tar.gz: 664927d90b41856e71e2c47bad606d78835905b1705e6fc7513aad1c9f64908e92cc0e82cb61a0bbb1b00516ba83fb6812c88517d519e547b056ab2bb7493fd0
data/CHANGELOG.md ADDED
@@ -0,0 +1,124 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [2.0.0] - 2026-08-10
9
+
10
+ ### Added
11
+
12
+ - Add Ractor support (C, Java)
13
+ - Add `SinDeepMerge::Fallback` and route Hash subclasses that override `merge!` or `update` such as `ActiveSupport::HashWithIndifferentAccess` through it (C, Java)
14
+
15
+ ### Fixed
16
+
17
+ - Merge nested hashes into copies in `deep_merge!` so that a shared or frozen nested hash is never mutated in place (C)
18
+ - Raise `TypeError` for non-Hash argument (C)
19
+ - Raise `SystemStackError` instead of crashing on deeply nested hashes (Java)
20
+ - Prevent crash on repeated stack overflow (C)
21
+ - Fix `ClassCastException` for non-Symbol keys (Java)
22
+ - Align `deep_merge` and `deep_merge!` arity across C and Java
23
+ - Load Java extension without `JRuby::Util.load_ext`
24
+ - Include files under lib in packaged gem
25
+ - Install C extension under `sin_deep_merge` directory
26
+ - Keep stray compiled jar out of packaged gem
27
+
28
+ ### Changed
29
+
30
+ - Optimize block invocation by yielding directly instead of allocating a Proc (C)
31
+ - Skip `initialize_dup` when copying hashes (C)
32
+ - Simplify native extension loading
33
+ - Improve readability of C extension
34
+ - Remove unneeded json gem dependency
35
+ - Minimize gem version constraints
36
+ - Add JRuby build and test script
37
+ - Make test task depend on compile
38
+ - Recompile jar
39
+ - Rework benchmark to compare like for like and report measurement error
40
+ - Add Ractor, Hash subclass and key type test coverage
41
+ - Update Ruby and JRuby versions in CI matrix
42
+ - Verify committed jar matches Java source in CI
43
+ - Require tests to pass before releasing in CI
44
+ - Pin `google-java-format` version and fail on download errors in CI
45
+ - Update `actions/checkout` v6 to v7 in CI
46
+ - Remove assign workflow
47
+ - Update sponsor link to GitHub Sponsors
48
+ - Update README.md
49
+ - Update .gitignore
50
+ - Update .rubocop.yml
51
+
52
+ ## [1.0.2] - 2026-02-22
53
+
54
+ ### Changed
55
+
56
+ - Recompile jar
57
+
58
+ ## [1.0.1] - 2026-02-21
59
+
60
+ ### Fixed
61
+
62
+ - Add frozen check for `deep_merge!` to raise `FrozenError` on frozen Hash (C, Java)
63
+ - Fix `deep_merge!` in Java to modify nested hashes in-place instead of duplicating them
64
+
65
+ ### Changed
66
+
67
+ - Optimize block invocation in C by using `rb_funcallv` instead of `rb_proc_call` with temporary Array allocation
68
+ - Optimize hash lookup in Java by using `fastARef` instead of `op_aref`
69
+ - Skip `to_hash` conversion when argument is already a Hash (C)
70
+ - Add `Check_Type` validation after `to_hash` conversion to ensure type safety (C)
71
+ - Use previously unused `id_call` variable for block invocation via `rb_funcallv` (C)
72
+ - Add TruffleRuby compatibility (Gemfile, C)
73
+ - Pin gem versions in Gemfile
74
+ - Refactor benchmark scripts into a single unified runner
75
+ - Add `.clang-format` configuration
76
+ - Add C and Java lint to CI (`clang-format`, `google-java-format`)
77
+ - Update `actions/checkout` v4 to v6 in CI
78
+ - Update .gitignore
79
+ - Update README.md benchmark results
80
+
81
+ ## [1.0.0] - 2025-08-11
82
+
83
+ ### Changed
84
+
85
+ - Improve performance
86
+
87
+ ### Fixed
88
+
89
+ - Update tests
90
+ - Update .gitignore
91
+ - Improve lint CI
92
+ - Update README.md
93
+
94
+ ## [0.0.2] - 2025-03-18
95
+
96
+ ### Changed
97
+
98
+ - Update summary
99
+
100
+ ## [0.0.1] - 2025-03-12
101
+
102
+ ### Fixed
103
+
104
+ - Fix permission
105
+
106
+ ### Changed
107
+
108
+ - Update tests
109
+ - Update summary
110
+ - Update README.md
111
+
112
+ ## [0.0.0] - 2025-03-11
113
+
114
+ ### Added
115
+
116
+ - Initial release
117
+
118
+ [2.0.0]: https://github.com/cadenza-tech/sin_deep_merge/compare/v1.0.2...v2.0.0
119
+ [1.0.2]: https://github.com/cadenza-tech/sin_deep_merge/compare/v1.0.1...v1.0.2
120
+ [1.0.1]: https://github.com/cadenza-tech/sin_deep_merge/compare/v1.0.0...v1.0.1
121
+ [1.0.0]: https://github.com/cadenza-tech/sin_deep_merge/compare/v0.0.2...v1.0.0
122
+ [0.0.2]: https://github.com/cadenza-tech/sin_deep_merge/compare/v0.0.1...v0.0.2
123
+ [0.0.1]: https://github.com/cadenza-tech/sin_deep_merge/compare/v0.0.0...v0.0.1
124
+ [0.0.0]: https://github.com/cadenza-tech/sin_deep_merge/releases/tag/v0.0.0
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Masahiro
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,191 @@
1
+ # SinDeepMerge
2
+
3
+ [![License](https://img.shields.io/github/license/cadenza-tech/sin_deep_merge?label=License&labelColor=343B42&color=blue)](https://github.com/cadenza-tech/sin_deep_merge/blob/main/LICENSE.txt) [![Tag](https://img.shields.io/github/tag/cadenza-tech/sin_deep_merge?label=Tag&logo=github&labelColor=343B42&color=2EBC4F)](https://github.com/cadenza-tech/sin_deep_merge/blob/main/CHANGELOG.md) [![Release](https://github.com/cadenza-tech/sin_deep_merge/actions/workflows/release.yml/badge.svg)](https://github.com/cadenza-tech/sin_deep_merge/actions?query=workflow%3Arelease) [![Test](https://github.com/cadenza-tech/sin_deep_merge/actions/workflows/test.yml/badge.svg)](https://github.com/cadenza-tech/sin_deep_merge/actions?query=workflow%3Atest) [![Lint](https://github.com/cadenza-tech/sin_deep_merge/actions/workflows/lint.yml/badge.svg)](https://github.com/cadenza-tech/sin_deep_merge/actions?query=workflow%3Alint)
4
+
5
+ Merge deeply nested hashes faster than DeepMerge or ActiveSupport.
6
+
7
+ - [Installation](#installation)
8
+ - [Usage](#usage)
9
+ - [Hash#deep\_merge](#hashdeep_merge)
10
+ - [Hash#deep\_merge!](#hashdeep_merge-1)
11
+ - [Benchmark](#benchmark)
12
+ - [Changelog](#changelog)
13
+ - [Development](#development)
14
+ - [Building for JRuby](#building-for-jruby)
15
+ - [Contributing](#contributing)
16
+ - [License](#license)
17
+ - [Code of Conduct](#code-of-conduct)
18
+ - [Sponsor](#sponsor)
19
+
20
+ ## Installation
21
+
22
+ Install the gem and add to the application's Gemfile by executing:
23
+
24
+ ```bash
25
+ bundle add sin_deep_merge
26
+ ```
27
+
28
+ If bundler is not being used to manage dependencies, install the gem by executing:
29
+
30
+ ```bash
31
+ gem install sin_deep_merge
32
+ ```
33
+
34
+ ## Usage
35
+
36
+ Both methods can be called inside non-main Ractors on CRuby 3.0+.
37
+
38
+ Requiring ActiveSupport afterwards is safe from 7.1 on, where Hash reaches both methods through `ActiveSupport::DeepMergeable` and a method defined on Hash itself wins over an included module. Up to 7.0 ActiveSupport defines them on Hash directly and replaces these when `active_support/core_ext/hash/deep_merge` is loaded last, so require `sin_deep_merge` after ActiveSupport there. `Hash.instance_method(:deep_merge).source_location` is `nil` while the extension is the one in place.
39
+
40
+ ### Hash#deep_merge
41
+
42
+ SinDeepMerge's Hash#deep_merge is compatible with ActiveSupport's Hash#deep_merge.
43
+
44
+ Hash subclasses that override `merge!` or `update`, such as `ActiveSupport::HashWithIndifferentAccess`, are merged through that override, so they behave the same as with ActiveSupport. Such a merge runs in Ruby rather than in the extension, so it is no faster than ActiveSupport.
45
+
46
+ What SinDeepMerge recurses into is any pair of Hashes, where ActiveSupport asks its `deep_merge?` hook instead. Two cases follow from that: a value that includes `ActiveSupport::DeepMergeable` without being a Hash is replaced rather than merged into, and a Hash subclass that overrides `deep_merge?` to refuse a value is merged into all the same.
47
+
48
+ ```ruby
49
+ require 'sin_deep_merge'
50
+
51
+ hash1 = { a: 1, b: '2', c: :three, d: true, e: nil }
52
+ hash2 = { b: :two, c: nil, d: false, e: 0, f: 'f' }
53
+ hash1.deep_merge(hash2) # => { a: 1, b: :two, c: nil, d: false, e: 0, f: 'f' }
54
+
55
+ hash1 = { a: [1, 2], b: [3, 4] }
56
+ hash2 = { a: [3, 4], b: 5 }
57
+ hash1.deep_merge(hash2) # => { a: [3, 4], b: 5 }
58
+
59
+ hash1 = { a: { b: 1, c: 2 }, b: { c: 3 } }
60
+ hash2 = { a: { c: 3, d: 4 }, b: 5 }
61
+ hash1.deep_merge(hash2) # => { a: { b: 1, c: 3, d: 4 }, b: 5 }
62
+
63
+ hash1 = { a: 1, b: 2 }
64
+ hash2 = { b: 3, c: 4 }
65
+ hash1.deep_merge(hash2) { |_key, old_val, new_val| old_val + new_val } # => { a: 1, b: 5, c: 4 }
66
+
67
+ hash1 = { a: [1, 2], b: 3 }
68
+ hash2 = { a: [3, 4], b: 5 }
69
+ hash1.deep_merge(hash2) do |_key, old_val, new_val|
70
+ if old_val.is_a?(Array) && new_val.is_a?(Array)
71
+ old_val + new_val
72
+ else
73
+ new_val
74
+ end
75
+ end # => { a: [1, 2, 3, 4], b: 5 }
76
+ ```
77
+
78
+ ### Hash#deep_merge!
79
+
80
+ SinDeepMerge's Hash#deep_merge! is compatible with ActiveSupport's Hash#deep_merge!.
81
+
82
+ Hash#deep_merge! destructively updates self by merging new values into it. Like ActiveSupport, nested hashes are merged into copies, so a nested hash shared with another object or a frozen nested hash is never mutated in place.
83
+
84
+ One case differs: a plain Hash carrying a singleton `merge!` is written into by the extension, so that override never runs, where ActiveSupport reaches it. Hash#deep_merge is not affected, since ActiveSupport merges into a `dup` there, and a `dup` no longer carries the singleton method.
85
+
86
+ ## Benchmark
87
+
88
+ SinDeepMerge's Hash#deep_merge is about 2.5-2.7x faster than ActiveSupport's Hash#deep_merge, and SinDeepMerge's Hash#deep_merge! is about 4.4x faster than DeepMerge's Hash#deep_merge! and about 2.4x faster than ActiveSupport's Hash#deep_merge!.
89
+
90
+ The first four tables hold only methods that leave the hash they were called on alone, so every iteration of the timed loop repeats the work of the first. DeepMerge is absent from them because it has no such method: its Hash#deep_merge writes into the receiver and returns it, the same as its Hash#deep_merge!. It also reads its second argument as an options Hash rather than as a block, so it has no block form either.
91
+
92
+ The last table is the one DeepMerge belongs in. Its entry there is Hash#deep_merge! rather than Hash#deep_merge, because Hash#deep_merge leaves an existing value alone where the other two overwrite it, and on these inputs it would walk the hash without writing anything. Every row there merges into a receiver that already carries the merge from the second iteration on. These inputs keep their shape once merged, so that costs little, but an input that grew the receiver would flatter each library by a different amount. Read those ratios as the rough comparison they are rather than as the measurement the first four tables give.
93
+
94
+ ```bash
95
+ $ bundle exec rake benchmark
96
+ Benchmarking: Shallow Recursion...
97
+ Benchmarking: Shallow Recursion With Block...
98
+ Benchmarking: Deep Recursion...
99
+ Benchmarking: Deep Recursion With Block...
100
+ Benchmarking: Deep Recursion In Place...
101
+
102
+ +--------------------------------------------------------------------------+
103
+ | Benchmark Result (Shallow Recursion) |
104
+ +----------------------------+----------------------+--------+-------------+
105
+ | Name | Iteration Per Second | Error | Speed Ratio |
106
+ +----------------------------+----------------------+--------+-------------+
107
+ | SinDeepMerge - deep_merge | 2678917.7 | ±0.56% | Fastest |
108
+ | Scratch - deep_merge | 1251682.2 | ±0.55% | 2.1x slower |
109
+ | ActiveSupport - deep_merge | 1006374.4 | ±0.53% | 2.7x slower |
110
+ +----------------------------+----------------------+--------+-------------+
111
+
112
+ +---------------------------------------------------------------------------------------------------------+
113
+ | Benchmark Result (Shallow Recursion With Block) |
114
+ +-----------------------------------------------------------+----------------------+--------+-------------+
115
+ | Name | Iteration Per Second | Error | Speed Ratio |
116
+ +-----------------------------------------------------------+----------------------+--------+-------------+
117
+ | SinDeepMerge - deep_merge (Shallow Recursion With Block) | 2283373.8 | ±0.62% | Fastest |
118
+ | Scratch - deep_merge (Shallow Recursion With Block) | 1100891.3 | ±0.50% | 2.1x slower |
119
+ | ActiveSupport - deep_merge (Shallow Recursion With Block) | 913719.8 | ±0.68% | 2.5x slower |
120
+ +-----------------------------------------------------------+----------------------+--------+-------------+
121
+
122
+ +--------------------------------------------------------------------------+
123
+ | Benchmark Result (Deep Recursion) |
124
+ +----------------------------+----------------------+--------+-------------+
125
+ | Name | Iteration Per Second | Error | Speed Ratio |
126
+ +----------------------------+----------------------+--------+-------------+
127
+ | SinDeepMerge - deep_merge | 28403.8 | ±0.78% | Fastest |
128
+ | Scratch - deep_merge | 12948.1 | ±0.76% | 2.2x slower |
129
+ | ActiveSupport - deep_merge | 11271.6 | ±0.45% | 2.5x slower |
130
+ +----------------------------+----------------------+--------+-------------+
131
+
132
+ +------------------------------------------------------------------------------------------------------+
133
+ | Benchmark Result (Deep Recursion With Block) |
134
+ +--------------------------------------------------------+----------------------+--------+-------------+
135
+ | Name | Iteration Per Second | Error | Speed Ratio |
136
+ +--------------------------------------------------------+----------------------+--------+-------------+
137
+ | SinDeepMerge - deep_merge (Deep Recursion With Block) | 27810.9 | ±0.58% | Fastest |
138
+ | Scratch - deep_merge (Deep Recursion With Block) | 12943.0 | ±0.47% | 2.1x slower |
139
+ | ActiveSupport - deep_merge (Deep Recursion With Block) | 10803.1 | ±0.56% | 2.6x slower |
140
+ +--------------------------------------------------------+----------------------+--------+-------------+
141
+
142
+ +---------------------------------------------------------------------------+
143
+ | Benchmark Result (Deep Recursion In Place) |
144
+ +-----------------------------+----------------------+--------+-------------+
145
+ | Name | Iteration Per Second | Error | Speed Ratio |
146
+ +-----------------------------+----------------------+--------+-------------+
147
+ | SinDeepMerge - deep_merge! | 27075.8 | ±0.66% | Fastest |
148
+ | ActiveSupport - deep_merge! | 11284.2 | ±0.44% | 2.4x slower |
149
+ | DeepMerge - deep_merge! | 6089.0 | ±0.62% | 4.4x slower |
150
+ +-----------------------------+----------------------+--------+-------------+
151
+ ```
152
+
153
+ Each row's error is how much its own measurement moved within that run, which is not how far that run sits from the next one. A run taken while the machine was busy reported errors just as narrow as these and ratios well away from these, so the numbers above are from a quiet machine and from two runs that agreed to within 0.1.
154
+
155
+ The error is also what decides the Speed Ratio column: a row is labelled Fastest when its gap to the top row is narrower than the two errors together, because a gap that small is the measurement failing to separate them rather than a difference it found. More than one row can therefore read Fastest.
156
+
157
+ The benchmark was executed in the following environment:
158
+
159
+ - Ruby 4.0.6 (2026-07-14 revision 03b6d3f889) +YJIT +PRISM [arm64-darwin25]
160
+ - DeepMerge 1.2.2
161
+ - ActiveSupport 8.1.3.1
162
+
163
+ ## Changelog
164
+
165
+ See [CHANGELOG.md](https://github.com/cadenza-tech/sin_deep_merge/blob/main/CHANGELOG.md).
166
+
167
+ ## Development
168
+
169
+ ### Building for JRuby
170
+
171
+ To build the Java extension and run tests for JRuby support:
172
+
173
+ ```bash
174
+ ./script/jruby_build_and_test.sh
175
+ ```
176
+
177
+ ## Contributing
178
+
179
+ Bug reports and pull requests are welcome on GitHub at https://github.com/cadenza-tech/sin_deep_merge. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/cadenza-tech/sin_deep_merge/blob/main/CODE_OF_CONDUCT.md).
180
+
181
+ ## License
182
+
183
+ The gem is available as open source under the terms of the [MIT License](https://github.com/cadenza-tech/sin_deep_merge/blob/main/LICENSE.txt).
184
+
185
+ ## Code of Conduct
186
+
187
+ Everyone interacting in the SinDeepMerge project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/cadenza-tech/sin_deep_merge/blob/main/CODE_OF_CONDUCT.md).
188
+
189
+ ## Sponsor
190
+
191
+ You can sponsor this project on [GitHub Sponsors](https://github.com/sponsors/cadenza-tech).
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+ require 'rubocop/rake_task'
6
+ require 'rubygems/package_task'
7
+
8
+ gemspec = Gem::Specification.load("#{__dir__}/sin_deep_merge.gemspec")
9
+
10
+ if RUBY_ENGINE == 'jruby'
11
+ require 'rake/javaextensiontask'
12
+
13
+ Rake::JavaExtensionTask.new('sin_deep_merge', gemspec) do |task|
14
+ task.ext_dir = 'ext/java'
15
+ task.source_version = '1.8'
16
+ task.target_version = '1.8'
17
+ end
18
+ else
19
+ require 'rake/extensiontask'
20
+
21
+ Rake::ExtensionTask.new('sin_deep_merge', gemspec) do |task|
22
+ task.lib_dir = 'lib/sin_deep_merge'
23
+ end
24
+ end
25
+
26
+ Gem::PackageTask.new(gemspec)
27
+
28
+ Rake::TestTask.new(:test) do |task|
29
+ task.pattern = 'test/**/test_*.rb'
30
+ end
31
+
32
+ # Without this the suite happily runs against a stale binary, so a changed extension looks like it passed.
33
+ Rake::Task[:test].enhance([:compile])
34
+
35
+ RuboCop::RakeTask.new(:rubocop) do |task|
36
+ task.options = ['--format', ENV['RUBOCOP_FORMAT']] if ENV['RUBOCOP_FORMAT']
37
+ end
38
+
39
+ task benchmark: [:compile] do
40
+ RubyVM::YJIT.enable if defined?(RubyVM::YJIT) && !RubyVM::YJIT.enabled?
41
+
42
+ require_relative 'script/deep_merge_benchmark'
43
+
44
+ DeepMergeBenchmark.run
45
+ end
@@ -2,8 +2,10 @@ package sin_deep_merge;
2
2
 
3
3
  import org.jruby.Ruby;
4
4
  import org.jruby.RubyHash;
5
+ import org.jruby.RubyModule;
5
6
  import org.jruby.anno.JRubyMethod;
6
7
  import org.jruby.runtime.Block;
8
+ import org.jruby.runtime.Helpers;
7
9
  import org.jruby.runtime.ThreadContext;
8
10
  import org.jruby.runtime.builtin.IRubyObject;
9
11
  import org.jruby.runtime.load.Library;
@@ -17,53 +19,101 @@ public class SinDeepMergeLibrary implements Library {
17
19
  @JRubyMethod(name = "deep_merge", required = 1)
18
20
  public static IRubyObject deepMerge(
19
21
  ThreadContext context, IRubyObject self, IRubyObject other, Block block) {
20
- RubyHash selfHash = self.convertToHash();
21
- RubyHash dupedHash = (RubyHash) selfHash.dup();
22
+ if (!isPlainHash(context, self)) {
23
+ return fallback(context, "deep_merge", self, other, block);
24
+ }
22
25
  RubyHash otherHash = other.convertToHash();
23
- deepMergeHashes(context, dupedHash, otherHash, block, false);
26
+ RubyHash dupedHash = (RubyHash) ((RubyHash) self).dup();
27
+ deepMergeInto(context, otherHash, dupedHash, block);
24
28
  return dupedHash;
25
29
  }
26
30
 
27
31
  @JRubyMethod(name = "deep_merge!", required = 1)
28
32
  public static IRubyObject deepMergeBang(
29
33
  ThreadContext context, IRubyObject self, IRubyObject other, Block block) {
30
- RubyHash selfHash = self.convertToHash();
31
- if (selfHash.isFrozen()) {
32
- throw context.runtime.newFrozenError("Hash");
34
+ // Hand over before converting other: a subclass may treat a Hash and a to_hash convertible
35
+ // differently, as merge! is free to.
36
+ if (!isPlainHash(context, self)) {
37
+ return fallback(context, "deep_merge!", self, other, block);
33
38
  }
39
+ RubyHash selfHash = (RubyHash) self;
40
+ selfHash.modify();
34
41
  RubyHash otherHash = other.convertToHash();
35
- deepMergeHashes(context, selfHash, otherHash, block, true);
42
+ deepMergeInto(context, otherHash, selfHash, block);
36
43
  return selfHash;
37
44
  }
38
45
 
39
- private static void deepMergeHashes(
40
- ThreadContext context,
41
- RubyHash self,
42
- RubyHash other,
43
- Block block,
44
- boolean destructive) {
45
- for (Object k : other.keySet()) {
46
- IRubyObject key = (IRubyObject) k;
47
- IRubyObject currentVal = self.fastARef(key);
48
- IRubyObject otherVal = other.fastARef(key);
46
+ // A Hash subclass may override merge! or update to police what gets stored, as ActiveSupport's
47
+ // HashWithIndifferentAccess does to keep its keys strings. op_aset writes past that override,
48
+ // so those receivers go to SinDeepMerge::Fallback, ActiveSupport's own implementation, which
49
+ // does reach the override through merge!. getRealClass sees through a singleton class, so a
50
+ // plain hash carrying singleton methods still takes the fast path.
51
+ private static boolean isPlainHash(ThreadContext context, IRubyObject object) {
52
+ return object.getMetaClass().getRealClass() == context.runtime.getHash();
53
+ }
54
+
55
+ private static IRubyObject fallback(
56
+ ThreadContext context, String name, IRubyObject self, IRubyObject other, Block block) {
57
+ RubyModule sinDeepMerge = context.runtime.getModule("SinDeepMerge");
58
+ IRubyObject target = sinDeepMerge.getConstant("Fallback");
59
+ return Helpers.invoke(context, target, name, new IRubyObject[] {self, other}, block);
60
+ }
61
+
62
+ private static void deepMergeInto(
63
+ ThreadContext context, RubyHash other, RubyHash target, Block block) {
64
+ try {
65
+ other.visitAll(context, new DeepMergeVisitor(block), target);
66
+ } catch (StackOverflowError e) {
67
+ // Match the C extension, which raises SystemStackError rather than letting the machine
68
+ // stack overflow escape as is.
69
+ throw context.runtime.newSystemStackError("stack level too deep", e);
70
+ }
71
+ }
72
+
73
+ private static class DeepMergeVisitor extends RubyHash.VisitorWithState<RubyHash> {
74
+ private final Block block;
75
+
76
+ DeepMergeVisitor(Block block) {
77
+ this.block = block;
78
+ }
79
+
80
+ @Override
81
+ public void visit(
82
+ ThreadContext context,
83
+ RubyHash other,
84
+ IRubyObject key,
85
+ IRubyObject otherVal,
86
+ int index,
87
+ RubyHash target) {
88
+ IRubyObject currentVal = target.fastARef(key);
89
+ IRubyObject newVal = otherVal;
49
90
 
50
- if (currentVal == null) {
51
- self.op_aset(context, key, otherVal);
52
- } else if (currentVal instanceof RubyHash && otherVal instanceof RubyHash) {
53
- RubyHash currentHash = (RubyHash) currentVal;
54
- RubyHash otherHash = (RubyHash) otherVal;
55
- if (!destructive) {
56
- currentHash = (RubyHash) currentHash.dup();
91
+ // An absent key takes otherVal as is, which is what newVal already holds.
92
+ if (currentVal != null) {
93
+ if (currentVal instanceof RubyHash && otherVal instanceof RubyHash) {
94
+ // Merge into a copy like ActiveSupport so shared or frozen nested hashes are
95
+ // never mutated.
96
+ if (isPlainHash(context, currentVal)) {
97
+ RubyHash merged = (RubyHash) ((RubyHash) currentVal).dup();
98
+ ((RubyHash) otherVal).visitAll(context, this, merged);
99
+ newVal = merged;
100
+ } else {
101
+ // ActiveSupport recurses as this_val.deep_merge(other_val), so a nested
102
+ // subclass gets its own override too.
103
+ newVal =
104
+ Helpers.invoke(
105
+ context,
106
+ currentVal,
107
+ "deep_merge",
108
+ new IRubyObject[] {otherVal},
109
+ block);
110
+ }
111
+ } else if (block.isGiven()) {
112
+ newVal = block.call(context, new IRubyObject[] {key, currentVal, otherVal});
57
113
  }
58
- deepMergeHashes(context, currentHash, otherHash, block, destructive);
59
- self.op_aset(context, key, currentHash);
60
- } else if (block.isGiven()) {
61
- IRubyObject result =
62
- block.call(context, new IRubyObject[] {key, currentVal, otherVal});
63
- self.op_aset(context, key, result);
64
- } else {
65
- self.op_aset(context, key, otherVal);
66
114
  }
115
+
116
+ target.op_aset(context, key, newVal);
67
117
  }
68
118
  }
69
119
  }
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SinDeepMerge
4
+ # ActiveSupport's implementation, kept for Hash subclasses that override merge! or update, ActiveSupport's
5
+ # HashWithIndifferentAccess being the common one. The extensions write straight into the storage of the hash, which skips such an
6
+ # override, so they hand those receivers here instead. dup and merge! are called on the receiver so that its own overrides run.
7
+ module Fallback # :nodoc:
8
+ module_function
9
+
10
+ def deep_merge(hash, other_hash, &block)
11
+ deep_merge!(hash.dup, other_hash, &block)
12
+ end
13
+
14
+ def deep_merge!(hash, other_hash, &block)
15
+ hash.merge!(other_hash) do |key, this_val, other_val|
16
+ if this_val.is_a?(Hash) && other_val.is_a?(Hash)
17
+ this_val.deep_merge(other_val, &block)
18
+ elsif block
19
+ yield(key, this_val, other_val)
20
+ else
21
+ other_val
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
Binary file
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SinDeepMerge
4
+ VERSION = '2.0.0'
5
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Hash
4
+ # Explicitly undefine method before redefining to avoid Ruby warnings.
5
+ undef_method(:deep_merge) if method_defined?(:deep_merge)
6
+ undef_method(:deep_merge!) if method_defined?(:deep_merge!)
7
+ end
8
+
9
+ # Both extensions look this up by name when they meet a Hash subclass, so it has to be in place before they can be called.
10
+ require 'sin_deep_merge/fallback'
11
+
12
+ case RUBY_ENGINE
13
+ when 'jruby'
14
+ require 'jruby'
15
+ require 'sin_deep_merge/sin_deep_merge.jar'
16
+
17
+ Java::sin_deep_merge::SinDeepMergeLibrary.new.load(JRuby.runtime, false)
18
+ else
19
+ # The extension-less require lets Ruby resolve the platform-specific shared library suffix (.bundle on macOS, .so elsewhere) via DLEXT.
20
+ require 'sin_deep_merge/sin_deep_merge'
21
+ end
22
+
23
+ require 'sin_deep_merge/version'
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sin_deep_merge
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 2.0.0
5
5
  platform: java
6
6
  authors:
7
7
  - Masahiro
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2026-02-21 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies: []
13
12
  description: Merge deeply nested hashes faster than DeepMerge or ActiveSupport
14
13
  email:
@@ -17,20 +16,26 @@ executables: []
17
16
  extensions: []
18
17
  extra_rdoc_files: []
19
18
  files:
19
+ - CHANGELOG.md
20
+ - LICENSE.txt
21
+ - README.md
22
+ - Rakefile
20
23
  - ext/java/sin_deep_merge/SinDeepMergeLibrary.java
24
+ - lib/sin_deep_merge.rb
25
+ - lib/sin_deep_merge/fallback.rb
21
26
  - lib/sin_deep_merge/sin_deep_merge.jar
22
- homepage: https://github.com/cadenza-tech/sin_deep_merge/tree/v1.0.1
27
+ - lib/sin_deep_merge/version.rb
28
+ homepage: https://github.com/cadenza-tech/sin_deep_merge/tree/v2.0.0
23
29
  licenses:
24
30
  - MIT
25
31
  metadata:
26
- homepage_uri: https://github.com/cadenza-tech/sin_deep_merge/tree/v1.0.1
27
- source_code_uri: https://github.com/cadenza-tech/sin_deep_merge/tree/v1.0.1
28
- changelog_uri: https://github.com/cadenza-tech/sin_deep_merge/blob/v1.0.1/CHANGELOG.md
32
+ homepage_uri: https://github.com/cadenza-tech/sin_deep_merge/tree/v2.0.0
33
+ source_code_uri: https://github.com/cadenza-tech/sin_deep_merge/tree/v2.0.0
34
+ changelog_uri: https://github.com/cadenza-tech/sin_deep_merge/blob/v2.0.0/CHANGELOG.md
29
35
  bug_tracker_uri: https://github.com/cadenza-tech/sin_deep_merge/issues
30
- documentation_uri: https://rubydoc.info/gems/sin_deep_merge/1.0.1
31
- funding_uri: https://patreon.com/CadenzaTech
36
+ documentation_uri: https://rubydoc.info/gems/sin_deep_merge/2.0.0
37
+ funding_uri: https://github.com/sponsors/cadenza-tech
32
38
  rubygems_mfa_required: 'true'
33
- post_install_message:
34
39
  rdoc_options: []
35
40
  require_paths:
36
41
  - lib
@@ -45,8 +50,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
45
50
  - !ruby/object:Gem::Version
46
51
  version: '0'
47
52
  requirements: []
48
- rubygems_version: 3.2.33
49
- signing_key:
53
+ rubygems_version: 4.0.3
50
54
  specification_version: 4
51
55
  summary: Merge deeply nested hashes faster than DeepMerge or ActiveSupport
52
56
  test_files: []