Skip to content

Commit

Permalink
Use memoization on refined class
Browse files Browse the repository at this point in the history
Avoid recreating strings within n_plus_one_query
  • Loading branch information
vprigent committed Jan 16, 2025
1 parent ad7d4c8 commit b10ed99
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
10 changes: 5 additions & 5 deletions lib/bullet/detector/n_plus_one_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def call_association(object, associations)
)
if !excluded_stacktrace_path? && conditions_met?(object, associations)
Bullet.debug('detect n + 1 query', "object: #{object.bullet_key}, associations: #{associations}")
create_notification caller_in_project(object.bullet_key), object.class.to_s, associations
create_notification(caller_in_project(object.bullet_key), object.class.to_s, associations)
end
end

Expand All @@ -38,16 +38,16 @@ def add_possible_objects(object_or_objects)
objects = Array.wrap(object_or_objects)
class_names_match_regex = true
primary_key_values_are_empty = true
keys_joined = ""
objects.each do |obj|

keys_joined = objects.map do |obj|
unless obj.class.name =~ /^HABTM_/
class_names_match_regex = false
end
unless obj.bullet_primary_key_value.nil?
primary_key_values_are_empty = false
end
keys_joined += "#{(keys_joined.empty? ? '' : ', ')}#{obj.bullet_key}"
end
obj.bullet_key
end.join(", ")
unless class_names_match_regex || primary_key_values_are_empty
Bullet.debug('Detector::NPlusOneQuery#add_possible_objects', "objects: #{keys_joined}")
objects.each { |object| possible_objects.add object.bullet_key }
Expand Down
20 changes: 9 additions & 11 deletions lib/bullet/ext/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@ module Bullet
module Ext
module Object
refine ::Object do
attr_writer :bullet_key, :bullet_primary_key_value

def bullet_key
"#{self.class}:#{bullet_primary_key_value}"
@bullet_key ||= "#{self.class}:#{bullet_primary_key_value}"
end

def bullet_primary_key_value
return if respond_to?(:persisted?) && !persisted?

if self.class.respond_to?(:primary_keys) && self.class.primary_keys
primary_key = self.class.primary_keys
elsif self.class.respond_to?(:primary_key) && self.class.primary_key
primary_key = self.class.primary_key
else
primary_key = :id
end
@bullet_primary_key_value ||= begin
return if respond_to?(:persisted?) && !persisted?

bullet_join_potential_composite_primary_key(primary_key)
primary_key = self.class.try(:primary_keys) || self.class.try(:primary_key) || :id

bullet_join_potential_composite_primary_key(primary_key)
end
end

private
Expand Down
7 changes: 6 additions & 1 deletion lib/bullet/ext/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ module Bullet
module Ext
module String
refine ::String do
attr_reader :bullet_class_name

def bullet_class_name
sub(/:[^:]*?$/, '')
@bullet_class_name ||= begin
last_colon = self.rindex(':')
last_colon ? self[0...last_colon] : self
end
end
end
end
Expand Down

0 comments on commit b10ed99

Please sign in to comment.