Skip to content

Commit d80dc46

Browse files
authored
rubocop test: reduce redundant merge! (#4985)
**Which issue(s) this PR fixes**: Fixes # **What this PR does / why we need it**: This is cosmetic change, it does not change behavior at all. It was detected by the following rubocop configuration: ``` Performance/RedundantMerge: Enabled: true ``` Use straight forward way. Benchmark result: ``` ruby 3.2.8 (2025-03-26 revision 13f495dc2c) +YJIT [x86_64-linux] Warming up -------------------------------------- merge! 457.409k i/100ms directly assign without merge! 764.424k i/100ms Calculating ------------------------------------- merge! 4.825M (± 3.6%) i/s (207.27 ns/i) - 24.243M in 5.031775s directly assign without merge! 7.581M (± 1.7%) i/s (131.91 ns/i) - 38.221M in 5.043344s Comparison: directly assign without merge!: 7580919.7 i/s merge!: 4824567.8 i/s - 1.57x slower ``` Appendix: benchmark script ```ruby require 'bundler/inline' gemfile do source 'https://rubygems.org' gem 'benchmark-ips' end Benchmark.ips do |x| data = { "foo": 1 } x.report("merge!") { data.merge!({"config" => {"@id" => "foo"}}) } x.report("directly assign without merge!") { data["config"] = {"@id" => "foo"} } x.compare! end ``` **Docs Changes**: N/A **Release Note**: N/A Signed-off-by: Kentaro Hayashi <hayashi@clear-code.com>
1 parent 340aa7e commit d80dc46

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

test/plugin/test_in_monitor_agent.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def test_enable_input_metrics(with_config)
160160
"emit_records" => 0, # This field is not updated due to not to be assigned metric callback.
161161
"emit_size" => 0, # Ditto.
162162
}
163-
input_info.merge!("config" => {"@id" => "test_in_gen", "@type" => "test_in_gen", "num" => "10"}) if with_config
163+
input_info["config"] = {"@id" => "test_in_gen", "@type" => "test_in_gen", "num" => "10"} if with_config
164164
filter_info = {
165165
"output_plugin" => false,
166166
"plugin_category" => "filter",
@@ -170,7 +170,7 @@ def test_enable_input_metrics(with_config)
170170
"emit_records" => Integer,
171171
"emit_size" => Integer,
172172
}
173-
filter_info.merge!("config" => {"@id" => "test_filter", "@type" => "test_filter"}) if with_config
173+
filter_info["config"] = {"@id" => "test_filter", "@type" => "test_filter"} if with_config
174174
output_info = {
175175
"output_plugin" => true,
176176
"plugin_category" => "output",
@@ -186,7 +186,7 @@ def test_enable_input_metrics(with_config)
186186
"slow_flush_count" => Integer,
187187
"flush_time_count" => Integer,
188188
}
189-
output_info.merge!("config" => {"@id" => "test_out", "@type" => "test_out"}) if with_config
189+
output_info["config"] = {"@id" => "test_out", "@type" => "test_out"} if with_config
190190
error_label_info = {
191191
"buffer_queue_length" => 0,
192192
"buffer_timekeys" => [],
@@ -209,7 +209,7 @@ def test_enable_input_metrics(with_config)
209209
"slow_flush_count" => Integer,
210210
"flush_time_count" => Integer,
211211
}
212-
error_label_info.merge!("config" => {"@id"=>"null", "@type" => "null"}) if with_config
212+
error_label_info["config"] = {"@id"=>"null", "@type" => "null"} if with_config
213213
opts = {with_config: with_config}
214214
assert_equal(input_info, d.instance.get_monitor_info(@ra.inputs.first, opts))
215215
assert_fuzzy_equal(filter_info, d.instance.get_monitor_info(@ra.filters.first, opts))
@@ -301,7 +301,7 @@ def test_enable_input_metrics(with_config)
301301
"emit_records" => 0,
302302
"emit_size" => 0,
303303
}
304-
input_info.merge!("config" => {"@id" => "test_in", "@type" => "test_in"}) if with_config
304+
input_info["config"] = {"@id" => "test_in", "@type" => "test_in"} if with_config
305305
filter_info = {
306306
"output_plugin" => false,
307307
"plugin_category" => "filter",
@@ -311,7 +311,7 @@ def test_enable_input_metrics(with_config)
311311
"emit_records" => 0,
312312
"emit_size" => 0,
313313
}
314-
filter_info.merge!("config" => {"@id" => "test_filter", "@type" => "test_filter"}) if with_config
314+
filter_info["config"] = {"@id" => "test_filter", "@type" => "test_filter"} if with_config
315315
output_info = {
316316
"output_plugin" => true,
317317
"plugin_category" => "output",
@@ -327,7 +327,7 @@ def test_enable_input_metrics(with_config)
327327
"slow_flush_count" => Integer,
328328
"flush_time_count" => Integer,
329329
}
330-
output_info.merge!("config" => {"@id" => "test_out", "@type" => "test_out"}) if with_config
330+
output_info["config"] = {"@id" => "test_out", "@type" => "test_out"} if with_config
331331
error_label_info = {
332332
"buffer_queue_length" => 0,
333333
"buffer_timekeys" => [],
@@ -350,7 +350,7 @@ def test_enable_input_metrics(with_config)
350350
"slow_flush_count" => Integer,
351351
"flush_time_count" => Integer,
352352
}
353-
error_label_info.merge!("config" => {"@id"=>"null", "@type" => "null"}) if with_config
353+
error_label_info["config"] = {"@id"=>"null", "@type" => "null"} if with_config
354354
opts = {with_config: with_config}
355355
assert_equal(input_info, d.instance.get_monitor_info(@ra.inputs.first, opts))
356356
assert_fuzzy_equal(filter_info, d.instance.get_monitor_info(@ra.filters.first, opts))
@@ -556,7 +556,7 @@ def get(uri, header = {})
556556
"emit_records" => 0,
557557
"emit_size" => 0,
558558
}
559-
expected_test_in_response.merge!("config" => {"@id" => "test_in", "@type" => "test_in"}) if with_config
559+
expected_test_in_response["config"] = {"@id" => "test_in", "@type" => "test_in"} if with_config
560560
expected_null_response = {
561561
"buffer_queue_length" => 0,
562562
"buffer_timekeys" => [],
@@ -579,8 +579,8 @@ def get(uri, header = {})
579579
"slow_flush_count" => Integer,
580580
"flush_time_count" => Integer,
581581
}
582-
expected_null_response.merge!("config" => {"@id" => "null", "@type" => "null"}) if with_config
583-
expected_null_response.merge!("retry" => {}) if with_retry
582+
expected_null_response["config"] = {"@id" => "null", "@type" => "null"} if with_config
583+
expected_null_response["retry"] = {} if with_retry
584584
response = JSON.parse(get("http://127.0.0.1:#{@port}/api/plugins.json").body)
585585
test_in_response = response["plugins"][0]
586586
null_response = response["plugins"][5]
@@ -622,7 +622,7 @@ def get(uri, header = {})
622622
"emit_records" => 0,
623623
"emit_size" => 0,
624624
}
625-
expected_test_in_response.merge!("config" => {"@id" => "test_in", "@type" => "test_in"}) if with_config
625+
expected_test_in_response["config"] = {"@id" => "test_in", "@type" => "test_in"} if with_config
626626
expected_null_response = {
627627
"buffer_queue_length" => 0,
628628
"buffer_timekeys" => [],
@@ -644,8 +644,8 @@ def get(uri, header = {})
644644
"slow_flush_count" => Integer,
645645
"flush_time_count" => Integer,
646646
}
647-
expected_null_response.merge!("config" => {"@id" => "null", "@type" => "null"}) if with_config
648-
expected_null_response.merge!("retry" => {}) if with_retry
647+
expected_null_response["config"] = {"@id" => "null", "@type" => "null"} if with_config
648+
expected_null_response["retry"] = {} if with_retry
649649
response = JSON.parse(get("http://127.0.0.1:#{@port}/api/plugins.json#{query_param}").body)
650650
test_in_response = response["plugins"][0]
651651
null_response = response["plugins"][5]

0 commit comments

Comments
 (0)