Skip to content

Commit

Permalink
Calculate Bullet.app_root and IS_RUBY_19(former ruby_19?) once
Browse files Browse the repository at this point in the history
Gem::Version.new and Gem::Version#<=> took more than half of all the
execution time of Bullet::Detector::NPlusOneQuery.call_association, and
Bullet.app_root roughly 1/5 more. None of this is changed without the
app restart, so it's safe to calculate them once.
  • Loading branch information
s-mage committed Feb 22, 2021
1 parent 8213b69 commit f8202e3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/bullet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def enable?
end

def app_root
(defined?(::Rails.root) ? Rails.root.to_s : Dir.pwd).to_s
@app_root ||= (defined?(::Rails.root) ? Rails.root.to_s : Dir.pwd).to_s
end

def n_plus_one_query_enable?
Expand Down
9 changes: 3 additions & 6 deletions lib/bullet/stack_trace_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module Bullet
module StackTraceFilter
VENDOR_PATH = '/vendor'
IS_RUBY_19 = Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.0.0')

def caller_in_project
vendor_root = Bullet.app_root + VENDOR_PATH
Expand Down Expand Up @@ -47,19 +48,15 @@ def pattern_matches?(location, pattern)
end

def location_as_path(location)
ruby_19? ? location : location.absolute_path.to_s
IS_RUBY_19 ? location : location.absolute_path.to_s
end

def select_caller_locations
if ruby_19?
if IS_RUBY_19
caller.select { |caller_path| yield caller_path }
else
caller_locations.select { |location| yield location }
end
end

def ruby_19?
@ruby_19 ||= Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.0.0')
end
end
end

0 comments on commit f8202e3

Please sign in to comment.