Skip to content

Commit

Permalink
Improve performance of color_name function in log subscriber
Browse files Browse the repository at this point in the history
Move runtime version comparison into file-load comparison, since
the gem version does not change while the process is running.

This removes two Gem::Version.new allocations (which in turn does some
regex scans and string allocations), as well as a Hash allocation (on
Rails 7.1+) per `color_name` call.
  • Loading branch information
natematykiewicz committed Jun 6, 2023
1 parent 4e31057 commit b6625ed
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lib/flipper/instrumentation/log_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,17 @@ def logger

private

# Rails 7.1 changed the signature of this function.
# Checking if > 7.0.99 rather than >= 7.1 so that 7.1 pre-release versions are included.
COLOR_OPTIONS = if Rails.gem_version > Gem::Version.new('7.0.99')
{ bold: true }.freeze
else
true
end
private_constant :COLOR_OPTIONS

def color_name(name)
# Rails 7.1 changed the signature of this function.
# Checking if > 7.0.99 rather than >= 7.1 so that 7.1 pre-release versions are included.
if Rails.gem_version > Gem::Version.new('7.0.99')
color(name, CYAN, bold: true)
else
color(name, CYAN, true)
end
color(name, CYAN, COLOR_OPTIONS)
end
end
end
Expand Down

0 comments on commit b6625ed

Please sign in to comment.