factory_bot icon indicating copy to clipboard operation
factory_bot copied to clipboard

Fix shape variations with attribute assigner

Open bkuhlmann opened this issue 1 year ago • 0 comments

Description

:wave: Hello, I'm seeing the following warnings in my test suite when using Factory Bot:

/factory_bot-6.4.6/lib/factory_bot/attribute_assigner.rb:16: warning: The class User reached 8 shape variations, instance variables accesses will be slower and memory usage increased.
It is recommended to define instance variables in a consistent order, for instance by eagerly defining them all in the #initialize method.

Reproduction Steps

You should be able to reproduce this locally by using export RUBYOPT="-W:deprecated -W:performance" when running the entire Factory Bot test suite.

Expected behavior

I would expect not to see performance warnings when using Factory Bot.

Actual behavior

Here's what I'm seeing:

/factory_bot-6.4.6/lib/factory_bot/attribute_assigner.rb:16: warning: The class User reached 8 shape variations, instance variables accesses will be slower and memory usage increased.
It is recommended to define instance variables in a consistent order, for instance by eagerly defining them all in the #initialize method.

Suggestion

A quick fix would be to ensure all of your lazy initialized instance variables are defined as nil in your constructor. Example:

def initialize
  # Original impelmentation...
  @method_tracking_evaluator = nil
  ...etc...
end

System configuration

factory_bot version: 6.4.6 rails version: 7.1.3.4 ruby version: 3.3.1

bkuhlmann avatar Jul 19 '24 19:07 bkuhlmann