From 9942113f0b2b88d8f729e937bb70a8890c129721 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Luka=C5=A1=C3=ADk?= Date: Tue, 26 Sep 2023 18:30:33 +0200 Subject: [PATCH] perf: avoid creating an extra list when iterating through values or keys MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Lukašík --- lib/fluent/config/configure_proxy.rb | 2 +- lib/fluent/configurable.rb | 4 ++-- lib/fluent/counter/mutex_hash.rb | 2 +- lib/fluent/test/driver/base.rb | 2 +- lib/fluent/test/output_test.rb | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/fluent/config/configure_proxy.rb b/lib/fluent/config/configure_proxy.rb index fbb6b63af8..ec2ce42040 100644 --- a/lib/fluent/config/configure_proxy.rb +++ b/lib/fluent/config/configure_proxy.rb @@ -201,7 +201,7 @@ def merge_for_finalized(other) def overwrite_defaults(other) # other is owner plugin's corresponding proxy self.defaults = self.defaults.merge(other.defaults) - self.sections.keys.each do |section_key| + self.sections.each_key do |section_key| if other.sections.has_key?(section_key) self.sections[section_key].overwrite_defaults(other.sections[section_key]) end diff --git a/lib/fluent/configurable.rb b/lib/fluent/configurable.rb index aa3ece1752..9131738c11 100644 --- a/lib/fluent/configurable.rb +++ b/lib/fluent/configurable.rb @@ -31,13 +31,13 @@ def initialize super # to simulate implicit 'attr_accessor' by config_param / config_section and its value by config_set_default proxy = self.class.merged_configure_proxy - proxy.params.keys.each do |name| + proxy.params.each_key do |name| next if name.to_s.start_with?('@') if proxy.defaults.has_key?(name) instance_variable_set("@#{name}".to_sym, proxy.defaults[name]) end end - proxy.sections.keys.each do |name| + proxy.sections.each_key do |name| next if name.to_s.start_with?('@') subproxy = proxy.sections[name] if subproxy.multi? diff --git a/lib/fluent/counter/mutex_hash.rb b/lib/fluent/counter/mutex_hash.rb index 9c66286458..22868fcdda 100644 --- a/lib/fluent/counter/mutex_hash.rb +++ b/lib/fluent/counter/mutex_hash.rb @@ -54,7 +54,7 @@ def synchronize(*keys) if mutex.try_lock locks[key] = mutex else - locks.values.each(&:unlock) + locks.each_value(&:unlock) locks = {} # flush locked keys break end diff --git a/lib/fluent/test/driver/base.rb b/lib/fluent/test/driver/base.rb index 9ba26edaf6..961df30593 100644 --- a/lib/fluent/test/driver/base.rb +++ b/lib/fluent/test/driver/base.rb @@ -87,7 +87,7 @@ def run(timeout: nil, start: true, shutdown: true, &block) sleep_with_watching_threads = ->(){ if @instance.respond_to?(:_threads) - @instance._threads.values.each{|t| t.join(0) } + @instance._threads.each_value{|t| t.join(0) } end sleep 0.1 } diff --git a/lib/fluent/test/output_test.rb b/lib/fluent/test/output_test.rb index b7ff3fd37a..bb18884dc2 100644 --- a/lib/fluent/test/output_test.rb +++ b/lib/fluent/test/output_test.rb @@ -139,7 +139,7 @@ def run(&block) assert_equal(@expected_buffer, buffer) end - lines.keys.each do |meta| + lines.each_key do |meta| chunk = @instance.buffer.generate_chunk(meta).staged! chunk.append(lines[meta]) begin