Skip to content

Commit

Permalink
Merge pull request rubocop#13248 from Earlopain/optimize-callbacks_ne…
Browse files Browse the repository at this point in the history
…eded

Optimize `Base.callbacks_needed` by reordering checks
  • Loading branch information
koic authored Sep 18, 2024
2 parents 9f2ffa2 + ed1aadb commit eb56d39
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/rubocop/cop/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,12 @@ def callbacks_needed
# @api private
def self.callbacks_needed
@callbacks_needed ||= public_instance_methods.select do |m|
m.start_with?(/on_|after_/) &&
!Base.method_defined?(m) # exclude standard "callbacks" like 'on_begin_investigation'
# OPTIMIZE: Check method existence first to make fewer `start_with?` calls.
# At the time of writing this comment, this excludes 98 of ~104 methods.
# `start_with?` with two string arguments instead of a regex is faster
# in this specific case as well.
!Base.method_defined?(m) && # exclude standard "callbacks" like 'on_begin_investigation'
m.start_with?('on_', 'after_')
end
end
# rubocop:enable Layout/ClassStructure
Expand Down

0 comments on commit eb56d39

Please sign in to comment.