Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix shape variations with attribute assigner #1679

Open
bkuhlmann opened this issue Jul 19, 2024 · 0 comments
Open

Fix shape variations with attribute assigner #1679

bkuhlmann opened this issue Jul 19, 2024 · 0 comments
Labels

Comments

@bkuhlmann
Copy link

Description

👋 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 bkuhlmann added the bug label Jul 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant