Skip to content

Commit

Permalink
Lazily add Aggregations to composed_of models
Browse files Browse the repository at this point in the history
`composed_of` is a fairly rare method to call on models.  This commit
adds the `Aggregations` module to models that call `composed_of` so that
models that *don't* call `composed_of` don't need to instantiate the
`aggregation_cache` hash.  This saves one hash allocation per model
instance that doesn't use `composed_of`
  • Loading branch information
tenderlove committed Jun 25, 2018
1 parent 8324791 commit 657060b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
4 changes: 4 additions & 0 deletions activerecord/lib/active_record/aggregations.rb
Expand Up @@ -225,6 +225,10 @@ module ClassMethods
def composed_of(part_id, options = {})
options.assert_valid_keys(:class_name, :mapping, :allow_nil, :constructor, :converter)

unless self < Aggregations
include Aggregations
end

name = part_id.id2name
class_name = options[:class_name] || name.camelize
mapping = options[:mapping] || [ name, name ]
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/base.rb
Expand Up @@ -288,6 +288,7 @@ class Base
extend Enum
extend Delegation::DelegateCache
extend CollectionCacheKey
extend Aggregations::ClassMethods

include Core
include DatabaseConfigurations
Expand All @@ -314,7 +315,6 @@ class Base
include ActiveModel::SecurePassword
include AutosaveAssociation
include NestedAttributes
include Aggregations
include Transactions
include TouchLater
include NoTouching
Expand Down

0 comments on commit 657060b

Please sign in to comment.