rails icon indicating copy to clipboard operation
rails copied to clipboard

High object allocation count from `ActiveRecord::ConnectionAdapters::PoolManager#pool_configs `

Open amarchenkoshopify opened this issue 1 year ago • 2 comments

Steps to reproduce

While working on memory efficiency investigation I noticed that a very big bunch of object allocations comes from a single method ActiveRecord::ConnectionAdapters::PoolManager#pool_configs:

      def pool_configs(role = nil)
        if role
          @role_to_shard_mapping[role].values
        else
          @role_to_shard_mapping.flat_map { |_, shard_map| shard_map.values }
        end
      end 

This method is called on every activerecord operation and does not cache its result: could we think about a way of caching its result?

cc @eileencodes

Expected behavior

#pool_configs allocates configs once

Actual behavior

#pool_configs allocates configs every time it is executed

System configuration

Rails version: main branch

Ruby version: 3.1

amarchenkoshopify avatar Aug 30 '22 12:08 amarchenkoshopify

Thank you @amarchenkoshopify I'll take a look at this soon.

eileencodes avatar Aug 30 '22 13:08 eileencodes

I opened https://github.com/rails/rails/pull/45916 which will reduce calls to pool_configs since we're no longer calling them in complete in the query cache. We can't simply memoize all_connection_pools or pool_configs without test errors, although I didn't yet dig into why yet (it might be a test only thing due to how transactions work but that would still break a lot of apps).

eileencodes avatar Aug 31 '22 12:08 eileencodes