Skip to content

Commit

Permalink
Cache the result of is_sharded?
Browse files Browse the repository at this point in the history
It can't possibly change.... and it's eating ~2% of Classic's CPU?????
  • Loading branch information
KJ Tsanaktsidis committed Mar 23, 2023
1 parent ee5b2c7 commit c528413
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/active_record_shards/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ def not_sharded
self.sharded = false
end

class_attribute :_model_class_is_sharded, default: nil
def is_sharded? # rubocop:disable Naming/PredicateName
if self == ActiveRecord::Base
cached = self.class._model_class_is_sharded
return cached unless cached.nil?

cached = if self == ActiveRecord::Base
sharded != false && supports_sharding?
elsif self == base_class
if sharded.nil?
Expand All @@ -22,6 +26,9 @@ def is_sharded? # rubocop:disable Naming/PredicateName
else
base_class.is_sharded?
end

self.class._model_class_is_sharded = cached
cached
end

def on_replica_by_default?
Expand Down

0 comments on commit c528413

Please sign in to comment.