DmitryTsepelev / ar_lazy_preload

Lazy loading associations for the ActiveRecord models
MIT License
677 stars 24 forks source link

Fix high allocated memory issue #18

Closed PikachuEXE closed 5 years ago

PikachuEXE commented 5 years ago

Full report

memory_report.txt

_Originally posted by @PikachuEXE in https://github.com/DmitryTsepelev/ar_lazy_preload/pull/16/comment#issuecomment-443129049_

PikachuEXE commented 5 years ago

Mainly these places as seen in report

    352360  /users/pikachuexe/projects/ruby/gems/ar_lazy_preload/lib/ar_lazy_preload/context.rb:29
    213320  /users/pikachuexe/projects/ruby/gems/ar_lazy_preload/lib/ar_lazy_preload/associated_context_builder.rb:27
    161600  /users/pikachuexe/projects/ruby/gems/ar_lazy_preload/lib/ar_lazy_preload/active_record/association.rb:7

Note that the report is for my branch of code

https://github.com/PikachuEXE/ar_lazy_preload/blob/improve-benchmark/lib/ar_lazy_preload/context.rb#L29: Why compact is required here?

https://github.com/PikachuEXE/ar_lazy_preload/blob/improve-benchmark/lib/ar_lazy_preload/associated_context_builder.rb#L27: I guess it's impossible to throw away this... I will think about optimizing it at last

https://github.com/PikachuEXE/ar_lazy_preload/blob/improve-benchmark/lib/ar_lazy_preload/active_record/association.rb#L7: I am confused here, what is allocated here? Association#owner & Association#reflection should be allocated & stored as attribute already same for Reflection#name too

Edit 1: Fix link

DmitryTsepelev commented 5 years ago

Why compact is required here?

Imagine that we're loading customers with optional has_one accounts, when we call preload we can get some nils which we want to exclude from the context

I guess it's impossible to throw away this...

I have an idea how to avoid the reinitialization of a context

I am confused here, what is allocated here?

#try_preload_lazily initializes the context

PikachuEXE commented 5 years ago

Why compact is required here?

I have just made a change which eliminates the need of compact and memory allocation from that location is reduced The whole file (not location) now only allocates 1624

Edit: Formatting again

DmitryTsepelev commented 5 years ago

Makes sense! Are we ready to merge #16 ?

PikachuEXE commented 5 years ago

Sure I might have other optimizations later, but I am still trying...

PikachuEXE commented 5 years ago

Failed attempt: https://github.com/PikachuEXE/ar_lazy_preload/compare/fix-merge-conflict...PikachuEXE:failure-example/enumerator

Got even more memory allocated 💩

---------------------------------------------------
AR lazy preloading w/o auto_preload w/ 100% usage:
---------------------------------------------------

Total allocated: 50848485 bytes (606462 objects)
Total retained:  15933 bytes (121 objects)

allocated memory by gem
-----------------------------------
  29310026  activerecord-5.2.1.1
  14969048  activemodel-5.2.1.1
   4875464  sqlite3-1.3.13
   1027699  activesupport-5.2.1.1
    425248  ar_lazy_preload
    125224  arel-9.0.0
    115264  set
       512  other

allocated memory by file
-----------------------------------
   7927208  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activemodel-5.2.1.1/lib/active_model/attribute_set/builder.rb
   7041600  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activemodel-5.2.1.1/lib/active_model/attribute.rb
   6742088  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations.rb
   6324960  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/result.rb
   4875104  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb
   4286276  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations/preloader/association.rb
   4227840  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/persistence.rb
   3668824  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/core.rb
   1042000  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/reflection.rb
    880800  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/aggregations.rb
    880000  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.1.1/lib/active_support/core_ext/numeric/conversions.rb
    261000  /Users/pikachuexe/projects/ruby/gems/ar_lazy_preload/lib/ar_lazy_preload/associated_context_builder.rb
    258960  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/relation/predicate_builder.rb
    243504  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/relation.rb
    242760  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/scoping.rb
    242400  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations/collection_association.rb
    226544  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/relation/delegation.rb
    220408  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/connection_adapters/sqlite3_adapter.rb
    177880  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/connection_adapters/abstract/quoting.rb
    177456  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/querying.rb
    161600  /Users/pikachuexe/projects/ruby/gems/ar_lazy_preload/lib/ar_lazy_preload/active_record/association.rb
    140523  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.1.1/lib/active_support/dependencies.rb
    115264  /Users/pikachuexe/.rvm/rubies/ruby-2.5.3/lib/ruby/2.5.0/set.rb
     81592  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/arel-9.0.0/lib/arel/visitors/to_sql.rb
     80800  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations/association.rb
     80800  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations/collection_proxy.rb
     80800  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/scoping/named.rb
     68768  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/relation/predicate_builder/array_handler.rb
     21978  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations/preloader.rb
     17984  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/arel-9.0.0/lib/arel/collectors/bind.rb
     16320  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/arel-9.0.0/lib/arel/predications.rb
      6504  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/arel-9.0.0/lib/arel/collectors/plain_string.rb
      5056  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.1.1/lib/active_support/inflector/methods.rb
      3312  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/legacy_yaml_adapter.rb
      1920  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/inheritance.rb
      1664  /Users/pikachuexe/projects/ruby/gems/ar_lazy_preload/lib/ar_lazy_preload/context.rb
      1502  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations/has_many_association.rb
      1344  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/connection_adapters/abstract_adapter.rb
      1280  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/relation/query_methods.rb
      1152  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/connection_adapters/abstract/query_cache.rb
      1008  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.1.1/lib/active_support/core_ext/hash/except.rb
       880  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/model_schema.rb
       720  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/arel-9.0.0/lib/arel/nodes/select_core.rb
       704  /Users/pikachuexe/projects/ruby/gems/ar_lazy_preload/lib/ar_lazy_preload/association_tree_builder.rb
       638  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations/foreign_association.rb
       552  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/arel-9.0.0/lib/arel/nodes/select_statement.rb
       512  benchmark/memory.rb
       480  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.1.1/lib/active_support/core_ext/module/redefine_method.rb
       448  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/arel-9.0.0/lib/arel/visitors/visitor.rb
       432  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/relation/spawn_methods.rb

allocated memory by location
-----------------------------------
   6147840  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/result.rb:134
   4855680  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:108
   4227840  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activemodel-5.2.1.1/lib/active_model/attribute_set/builder.rb:25
   4227840  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations.rb:265
   4227840  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/persistence.rb:71
   3521600  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activemodel-5.2.1.1/lib/active_model/attribute.rb:9
   3520320  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations/preloader/association.rb:36
   2787904  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/core.rb:144
   2514248  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations.rb:237
   1937768  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activemodel-5.2.1.1/lib/active_model/attribute_set/builder.rb:16
   1760000  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activemodel-5.2.1.1/lib/active_model/attribute.rb:13
   1760000  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activemodel-5.2.1.1/lib/active_model/attribute.rb:71
    960960  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/reflection.rb:116
    880800  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activemodel-5.2.1.1/lib/active_model/attribute_set/builder.rb:115
    880800  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activemodel-5.2.1.1/lib/active_model/attribute_set/builder.rb:17
    880800  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/aggregations.rb:25
    880800  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/core.rb:541
    880000  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.1.1/lib/active_support/core_ext/numeric/conversions.rb:107
    479648  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations/preloader/association.rb:100
    240000  /Users/pikachuexe/projects/ruby/gems/ar_lazy_preload/lib/ar_lazy_preload/associated_context_builder.rb:146
    226544  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/relation/delegation.rb:102
    219560  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/connection_adapters/sqlite3_adapter.rb:222
    208816  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations/preloader/association.rb:97
    177760  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/relation/predicate_builder.rb:60
    176280  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/querying.rb:52
    176280  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/result.rb:129
    161840  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/scoping.rb:16
    161600  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations/collection_association.rb:35
    161600  /Users/pikachuexe/projects/ruby/gems/ar_lazy_preload/lib/ar_lazy_preload/active_record/association.rb:7
    115264  /Users/pikachuexe/.rvm/rubies/ruby-2.5.3/lib/ruby/2.5.0/set.rb:94
    109410  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.1.1/lib/active_support/dependencies.rb:287
     81392  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/arel-9.0.0/lib/arel/visitors/to_sql.rb:820
     80920  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/relation.rb:25
     80920  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/relation.rb:29
     80920  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/scoping.rb:85
     80880  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/relation.rb:195
     80800  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations/association.rb:131
     80800  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations/collection_association.rb:75
     80800  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations/collection_proxy.rb:1118
     80800  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/connection_adapters/abstract/quoting.rb:161
     80800  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/connection_adapters/abstract/quoting.rb:193
     80800  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/reflection.rb:589
     80800  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/relation/predicate_builder.rb:61
     80800  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/scoping/named.rb:58
     58368  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations/preloader/association.rb:60
     18104  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/relation/predicate_builder/array_handler.rb:14
     18104  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/relation/predicate_builder/array_handler.rb:15
     18072  /Users/pikachuexe/projects/ruby/gems/ar_lazy_preload/lib/ar_lazy_preload/associated_context_builder.rb:60
     17984  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/arel-9.0.0/lib/arel/collectors/bind.rb:7
     17984  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:37

allocated memory by class
-----------------------------------
  21079024  Hash
   9961368  Array
   5845757  String
   3521600  ActiveModel::Attribute::FromDatabase
   2400000  Comment
   2288000  ActiveRecord::Associations::BelongsToAssociation
   1937768  ActiveModel::LazyAttributeHash
   1760000  ActiveModel::Attribute::FromUser
    880800  ActiveModel::AttributeSet
    384032  Post
    226256  ActiveRecord::Associations::CollectionProxy
    226248  ActiveRecord::Associations::HasManyAssociation
    177760  ActiveRecord::Relation::QueryAttribute
     80800  Arel::Nodes::BindParam
     42120  File
     11920  RubyVM::InstructionSequence
      6280  Class
      3872  User
      3360  MatchData
      2128  Thread::Backtrace
      2000  Module
      1360  Proc
      1024  Enumerator
       720  ActiveRecord::Relation
       312  Arel::Nodes::SelectCore
       264  Arel::Nodes::SelectStatement
       256  ArLazyPreload::AssociatedContextBuilder::AssociationArrayLikeEnumerator
       240  ActiveRecord::Associations::Preloader::Association
       240  SQLite3::Statement
       216  ActiveRecord::Result
       216  Method
       200  Arel::Attributes::Attribute
       176  NameError
       160  ActiveRecord::Relation::WhereClause
       160  Set
       152  ArLazyPreload::Context
       144  Enumerator::Yielder
       120  ActiveModel::AttributeSet::Builder
       120  ActiveModel::AttributeSet::YAMLEncoder
       120  Arel::Collectors::Bind
       120  Arel::Collectors::Composite
       120  Arel::Collectors::SQLString
       120  Arel::Nodes::JoinSource
       120  Arel::Nodes::SqlLiteral
       120  Arel::SelectManager
       112  Enumerator::Generator
        80  ActiveRecord::Associations::Preloader
        80  ActiveRecord::Relation::WhereClauseFactory
        80  ArLazyPreload::AssociatedContextBuilder
        80  ArLazyPreload::AssociationTreeBuilder

allocated objects by gem
-----------------------------------
    306888  activerecord-5.2.1.1
    198106  activemodel-5.2.1.1
     66104  sqlite3-1.3.13
     23146  activesupport-5.2.1.1
     10109  ar_lazy_preload
      2100  arel-9.0.0
         5  other
         4  set

allocated objects by file
-----------------------------------
    110020  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activemodel-5.2.1.1/lib/active_model/attribute.rb
     90064  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations/preloader/association.rb
     88080  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activemodel-5.2.1.1/lib/active_model/attribute_set/builder.rb
     66098  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb
     46040  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations.rb
     44043  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/core.rb
     26050  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/reflection.rb
     22042  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/result.rb
     22020  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/aggregations.rb
     22020  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/persistence.rb
     22000  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.1.1/lib/active_support/core_ext/numeric/conversions.rb
      6080  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/relation.rb
      6069  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/scoping.rb
      6060  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations/collection_association.rb
      6037  /Users/pikachuexe/projects/ruby/gems/ar_lazy_preload/lib/ar_lazy_preload/associated_context_builder.rb
      4050  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/relation/predicate_builder.rb
      4043  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/connection_adapters/abstract/quoting.rb
      4040  /Users/pikachuexe/projects/ruby/gems/ar_lazy_preload/lib/ar_lazy_preload/active_record/association.rb
      2031  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/arel-9.0.0/lib/arel/visitors/to_sql.rb
      2023  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/relation/delegation.rb
      2020  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations/association.rb
      2020  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations/collection_proxy.rb
      2020  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/scoping/named.rb
      1069  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.1.1/lib/active_support/dependencies.rb
        50  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.1.1/lib/active_support/inflector/methods.rb
        32  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/relation/query_methods.rb
        28  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations/preloader.rb
        24  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/inheritance.rb
        21  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/querying.rb
        18  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/model_schema.rb
        18  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/relation/predicate_builder/array_handler.rb
        18  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/arel-9.0.0/lib/arel/nodes/select_core.rb
        17  /Users/pikachuexe/projects/ruby/gems/ar_lazy_preload/lib/ar_lazy_preload/context.rb
        15  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/connection_adapters/abstract_adapter.rb
        13  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/connection_adapters/sqlite3_adapter.rb
        10  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/relation/where_clause.rb
         9  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/connection_adapters/abstract/database_statements.rb
         9  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/legacy_yaml_adapter.rb
         9  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.1.1/lib/active_support/core_ext/hash/except.rb
         9  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/arel-9.0.0/lib/arel/nodes/select_statement.rb
         8  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/arel-9.0.0/lib/arel/visitors/visitor.rb
         8  /Users/pikachuexe/projects/ruby/gems/ar_lazy_preload/lib/ar_lazy_preload/association_tree_builder.rb
         7  /Users/pikachuexe/projects/ruby/gems/ar_lazy_preload/lib/ar_lazy_preload/active_record/relation.rb
         6  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activemodel-5.2.1.1/lib/active_model/attribute_set.rb
         6  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/connection_adapters/abstract/query_cache.rb
         6  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/relation/where_clause_factory.rb
         6  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.1.1/lib/active_support/core_ext/module/redefine_method.rb
         6  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/arel-9.0.0/lib/arel/select_manager.rb
         6  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/sqlite3-1.3.13/lib/sqlite3/database.rb
         5  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/arel-9.0.0/lib/arel/table.rb

allocated objects by location
-----------------------------------
     88008  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations/preloader/association.rb:36
     66063  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:108
     44020  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activemodel-5.2.1.1/lib/active_model/attribute.rb:9
     44000  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activemodel-5.2.1.1/lib/active_model/attribute.rb:71
     24024  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/reflection.rb:116
     24020  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations.rb:237
     22020  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activemodel-5.2.1.1/lib/active_model/attribute_set/builder.rb:115
     22020  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activemodel-5.2.1.1/lib/active_model/attribute_set/builder.rb:16
     22020  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activemodel-5.2.1.1/lib/active_model/attribute_set/builder.rb:17
     22020  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activemodel-5.2.1.1/lib/active_model/attribute_set/builder.rb:25
     22020  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/aggregations.rb:25
     22020  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations.rb:265
     22020  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/core.rb:144
     22020  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/core.rb:541
     22020  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/persistence.rb:71
     22020  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/result.rb:134
     22000  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activemodel-5.2.1.1/lib/active_model/attribute.rb:13
     22000  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.1.1/lib/active_support/core_ext/numeric/conversions.rb:107
      6000  /Users/pikachuexe/projects/ruby/gems/ar_lazy_preload/lib/ar_lazy_preload/associated_context_builder.rb:146
      4046  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/scoping.rb:16
      4040  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations/collection_association.rb:35
      4040  /Users/pikachuexe/projects/ruby/gems/ar_lazy_preload/lib/ar_lazy_preload/active_record/association.rb:7
      2026  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/arel-9.0.0/lib/arel/visitors/to_sql.rb:820
      2023  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/relation.rb:25
      2023  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/relation.rb:29
      2023  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/relation/delegation.rb:102
      2023  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/scoping.rb:85
      2022  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations/preloader/association.rb:100
      2022  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/relation.rb:195
      2020  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations/association.rb:131
      2020  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations/collection_association.rb:75
      2020  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations/collection_proxy.rb:1118
      2020  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/connection_adapters/abstract/quoting.rb:161
      2020  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/connection_adapters/abstract/quoting.rb:193
      2020  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/reflection.rb:589
      2020  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/relation/predicate_builder.rb:60
      2020  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/relation/predicate_builder.rb:61
      2020  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/scoping/named.rb:58
       961  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.1.1/lib/active_support/dependencies.rb:287
        84  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.1.1/lib/active_support/dependencies.rb:545
        16  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/inheritance.rb:185
        16  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/result.rb:128
        13  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:137
        13  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/sqlite3-1.3.13/lib/sqlite3/statement.rb:140
        12  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/model_schema.rb:329
        12  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.1.1/lib/active_support/inflector/methods.rb:273
        12  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.1.1/lib/active_support/inflector/methods.rb:72
        10  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1.1/lib/active_record/associations/preloader/association.rb:31
         9  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.1.1/lib/active_support/core_ext/hash/except.rb:13
         8  /Users/pikachuexe/.rvm/gems/ruby-2.5.3/gems/activesupport-5.2.1.1/lib/active_support/dependencies.rb:419

allocated objects by class
-----------------------------------
    190610  Array
    138253  Hash
    115297  String
     44020  ActiveModel::Attribute::FromDatabase
     22020  ActiveModel::AttributeSet
     22020  ActiveModel::LazyAttributeHash
     22000  ActiveModel::Attribute::FromUser
     22000  ActiveRecord::Associations::BelongsToAssociation
     20000  Comment
      2020  ActiveRecord::Associations::CollectionProxy
      2020  ActiveRecord::Associations::HasManyAssociation
      2020  ActiveRecord::Relation::QueryAttribute
      2020  Arel::Nodes::BindParam
      2000  Post
        20  User
        17  Proc
        12  MatchData
         8  Class
         8  Enumerator
         6  ActiveRecord::Relation
         5  Arel::Attributes::Attribute
         5  File
         4  ActiveRecord::Relation::WhereClause
         4  Module
         4  Set
         3  ActiveModel::AttributeSet::Builder
         3  ActiveModel::AttributeSet::YAMLEncoder
         3  ActiveRecord::Result
         3  ArLazyPreload::AssociatedContextBuilder::AssociationArrayLikeEnumerator
         3  Arel::Collectors::Bind
         3  Arel::Collectors::Composite
         3  Arel::Collectors::SQLString
         3  Arel::Nodes::JoinSource
         3  Arel::Nodes::SelectCore
         3  Arel::Nodes::SelectStatement
         3  Arel::Nodes::SqlLiteral
         3  Arel::SelectManager
         3  Enumerator::Yielder
         3  Method
         3  SQLite3::Statement
         2  ActiveRecord::Associations::Preloader
         2  ActiveRecord::Associations::Preloader::Association
         2  ActiveRecord::Relation::WhereClauseFactory
         2  ArLazyPreload::AssociatedContextBuilder
         2  ArLazyPreload::AssociationTreeBuilder
         2  ArLazyPreload::Context
         2  Arel::Nodes::And
         2  Arel::Nodes::In
         2  Enumerator::Generator
         2  NameError
PikachuEXE commented 5 years ago

Fixed by #20 And I don't see any obvious that can be optimized