From e3306ba1f01188bace9f635179f09ea5ba31cbd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Luka=C5=A1=C3=ADk?= Date: Mon, 18 Sep 2023 11:53:01 +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/log.rb | 2 +- lib/fluent/plugin/in_tail.rb | 2 +- lib/fluent/plugin/parser.rb | 2 +- lib/fluent/test/driver/base.rb | 2 +- lib/fluent/test/output_test.rb | 2 +- 8 files changed, 9 insertions(+), 9 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/log.rb b/lib/fluent/log.rb index f8175fd911..0fcf4fb935 100644 --- a/lib/fluent/log.rb +++ b/lib/fluent/log.rb @@ -584,7 +584,7 @@ def event(level, args) if @log_event_enabled && !@threads_exclude_events.include?(Thread.current) record = map.dup - record.keys.each {|key| + record.each_key {|key| record[key] = record[key].inspect unless record[key].respond_to?(:to_msgpack) } record['message'] = message.dup diff --git a/lib/fluent/plugin/in_tail.rb b/lib/fluent/plugin/in_tail.rb index 54fd900039..83e4bffec3 100644 --- a/lib/fluent/plugin/in_tail.rb +++ b/lib/fluent/plugin/in_tail.rb @@ -488,7 +488,7 @@ def stop_watchers(targets_info, immediate: false, unwatched: false, remove_watch end def close_watcher_handles - @tails.keys.each do |path| + @tails.each_key do |path| tw = @tails.delete(path) if tw tw.close diff --git a/lib/fluent/plugin/parser.rb b/lib/fluent/plugin/parser.rb index 0854129f0e..4fe7ca8ae6 100644 --- a/lib/fluent/plugin/parser.rb +++ b/lib/fluent/plugin/parser.rb @@ -43,7 +43,7 @@ def start until @flag.wait_for_set(0.5) now = Time.now @mutex.synchronize { - @map.keys.each { |th| + @map.each_key { |th| time = @map[th] if now - time > @timeout th.raise UncatchableError, "parsing timed out" 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