Skip to content

Commit

Permalink
perf: avoid creating an extra list when iterating through values or keys
Browse files Browse the repository at this point in the history
  • Loading branch information
isimluk committed Sep 18, 2023
1 parent 0111bfd commit 95c7603
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/fluent/config/configure_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/fluent/configurable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/counter/mutex_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/plugin/in_tail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/plugin/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/test/driver/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion lib/fluent/test/output_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 95c7603

Please sign in to comment.