Very interesting post (From Octobre 23) about object shape in a Rails context.

TL;DR (cf bottom of the article): Define your instance_variable in the constructor if you use memoization to avoid cache miss during ObjectShape inspection

https://railsatscale.com/2023-10-24-memoization-pattern-and-object-shapes/

#ruby #rails #RailsPerformance

Performance impact of the memoization idiom on modern Ruby

Ruby 3.2 saw the introduction of object shapes which speedup instance variables access in most cases, but can be slower in some pathological cases.

Rails at Scale