Improving ActiveRecord::Core find by cache hit rate

I noticed that in ActiveRecord::Core, when we cache the StatementCache values (here and there), we potentially duplicate cache entries in the case where both Model.find(1) and Model.find_by(id: 1) are called, this results in two entries in the cache, with the respective keys "id" and ["id"]

I was playing with it and wrote a small patch that prevents that by extracting the first (and only) value from the array of keys, if there’s only one key, in the find_by case.

https://github.com/rails/rails/compare/main...pjambet:pj/reduce-find-by-cache-misses?expand=1

1 Like

Created a PR: ActiveRecord::Core: prevent duplicate entries in find by cache by pjambet · Pull Request #43960 · rails/rails · GitHub

1 Like