Skip to content

Resolve sibling traits by reference, not by copy - #1857

Merged
neilvcarvalho merged 2 commits into
mainfrom
nc/fix-trait-definition-growth
Nov 27, 2026
Merged

neilvcarvalho merged 2 commits into
mainfrom
nc/fix-trait-definition-growth

Conversation

@neilvcarvalho

@neilvcarvalho neilvcarvalho commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Why

Every FactoryBot.build(:user, :some_trait) (any strategy, one or more traits) permanently copied every trait defined on the factory into the defined_traits Set of each trait named in the call. FactoryRunner#run compiles a clone of the factory, the clone shares the registered Trait objects, and Definition#compile re-ran the sibling propagation on that clone:

defined_traits.each do |defined_trait|
  base_trait_names.each { |bt| bt.define_trait defined_trait }
  additional_trait_names.each { |at| at.define_trait defined_trait }
end

Memory grew with (traits on the factory) times (distinct traits the suite uses) and was never released. Every build with traits also spent O(number of traits) repeating the copy.

How

Each definition now gets a pointer to the definition it was compiled under (enclosing_definition). Trait name lookup, the "Registered traits" list in error messages, and the compile_factory instrumentation payload fall through that pointer, so:

  • trait bodies still resolve sibling traits by name, including chains through several traits
  • global traits still resolve traits of the factory that uses them, both as base traits and at build time
  • error messages, did-you-mean suggestions and the instrumentation payload are unchanged

Builds no longer mutate trait definitions at all. Deep-copying the traits on the clone was rejected: Trait#clone re-evaluates the block, which would reset inline sequences and register a new sequence on every build.

A regression spec in spec/factory_bot/factory_spec.rb runs a factory with a trait twice and asserts the trait's defined_traits does not grow. It fails on main (0 to 2 entries) and passes here.

Numbers

Set growth

Reproduction: one factory with 1,000 traits, then 2,000 builds with 0 to 3 random traits each, measuring the defined_traits Sets of the factory's traits.

after one build after 2,000 builds time per build
main 1,000 entries, 160 KB 938,000 entries, 18,842 KB 490 us
this branch 0 entries, 140 KB 0 entries, 140 KB 169 us

The 140 KB is the baseline of 1,000 empty Sets.

Simulated suites

Each cell is a fresh process on Ruby 4.0.1: a factory with N traits, each setting one attribute on a plain Ruby class, built with build. "Retained" is the growth in ObjectSpace.memsize_of_all after the suite and four full GCs. "Allocated" is GC.stat(:total_allocated_objects) per build.

Random suite: 2,000 examples, each one build with 0 to 3 random traits.

traits retained (main / this branch) allocated objects per build (main / this branch) time (main / this branch)
10 150 KB / 148 KB 327 / 302 61 / 58 ms
50 218 KB / 166 KB 450 / 305 93 / 67 ms
100 396 KB / 180 KB 612 / 309 130 / 79 ms
250 1.4 MB / 221 KB 1,116 / 311 267 / 130 ms
500 5.2 MB / 287 KB 2,062 / 316 483 / 213 ms
1000 19.1 MB / 401 KB 4,280 / 323 1,057 / 373 ms

Repeat suite: 2,000 examples, each ten builds with the same two traits (create_list style), 20,000 builds in total.

traits retained (main / this branch) allocated objects per build (main / this branch) time (main / this branch)
10 1.8 MB / 1.8 MB 410 / 375 0.75 / 0.64 s
100 1.7 MB / 1.9 MB 775 / 380 1.80 / 1.06 s
500 6.4 MB / 2.0 MB 2,401 / 381 6.16 / 2.63 s
1000 21.5 MB / 2.2 MB 4,473 / 381 11.98 / 4.76 s

Many distinct combinations: 100 traits, 20,000 builds touching 8,038 distinct trait combinations.

main this branch
retained 1.8 MB 1.6 MB
time 1.45 s 0.88 s

Retained memory and allocations per build are now flat in the trait count. A per-build cost proportional to the trait count remains because the clone's compile still walks the trait list once; that is a separate, smaller follow-up.

Verification

  • bundle exec rake all_specs and bundle exec standardrb on the default Gemfile
  • BUNDLE_GEMFILE=gemfiles/7.0.gemfile bundle exec rake all_specs
  • BUNDLE_GEMFILE=gemfiles/main.gemfile bundle exec rake all_specs

Found while benchmarking the #1847 to #1853 stack, which removes this mechanism entirely. This fix is independent of that stack.

Compiling a definition copied every trait defined on it into the
definition of each base and additional trait, so that a trait body
could reference its siblings by name. FactoryRunner compiles a clone
of the factory for every build with traits, and the clone shares the
registered Trait objects, so each build with a trait not used before
added all N traits of the factory to that trait's defined_traits Set.
Memory grew with N times the number of distinct traits a suite uses
and was never released, and every build spent O(N) time repeating the
copy.

This commit gives each definition a pointer to the definition it was
compiled under instead. Trait lookup and the error-message trait list
fall through that pointer, and the compile_factory payload reports the
same traits as before, so trait bodies still resolve sibling traits,
global traits still resolve traits of the factory that uses them, and
error messages and instrumentation are unchanged. Builds no longer
mutate trait definitions.

Deep-copying the traits on the clone was rejected because Trait#clone
re-evaluates the block, which would reset inline sequences on every
build.

With 1,000 traits on one factory, the defined_traits Sets of the
factory's traits held 938,000 entries (18.8 MB) after 2,000 builds
with random traits; they now hold none. Build time with traits went
from 490 us to 169 us per build.
The standard job let setup-ruby read the Ruby version from
.tool-versions, which pins 4.0.1. The ubuntu-latest image moved to
Ubuntu 26.04, and setup-ruby only builds the latest patch release of
each series for newer images, so the job now fails on every pull
request before running standard at all. The matrix jobs already ask
for the "4.0" series and pass.

This commit pins the standard job to the "4.0" series as well, so it
always gets the latest patch release that the image provides.
.tool-versions is unchanged, so local development keeps 4.0.1.
@neilvcarvalho
neilvcarvalho force-pushed the nc/fix-trait-definition-growth branch from 7315251 to 671d3f0 Compare November 27, 2026 14:24
@neilvcarvalho
neilvcarvalho merged commit 0d06d64 into main Nov 27, 2026
39 checks passed
@neilvcarvalho
neilvcarvalho deleted the nc/fix-trait-definition-growth branch November 27, 2026 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant