Repository navigation
Resolve sibling traits by reference, not by copy - #1857
Merged
Merged
Conversation
This was referenced Nov 3, 2026
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
force-pushed
the
nc/fix-trait-definition-growth
branch
from
November 27, 2026 14:24
7315251 to
671d3f0
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Every
FactoryBot.build(:user, :some_trait)(any strategy, one or more traits) permanently copied every trait defined on the factory into thedefined_traitsSet of each trait named in the call.FactoryRunner#runcompiles a clone of the factory, the clone shares the registeredTraitobjects, andDefinition#compilere-ran the sibling propagation on that clone: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 thecompile_factoryinstrumentation payload fall through that pointer, so:Builds no longer mutate trait definitions at all. Deep-copying the traits on the clone was rejected:
Trait#clonere-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.rbruns a factory with a trait twice and asserts the trait'sdefined_traitsdoes not grow. It fails onmain(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_traitsSets of the factory's traits.mainThe 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 inObjectSpace.memsize_of_allafter the suite and four full GCs. "Allocated" isGC.stat(:total_allocated_objects)per build.Random suite: 2,000 examples, each one build with 0 to 3 random traits.
main/ this branch)main/ this branch)main/ this branch)Repeat suite: 2,000 examples, each ten builds with the same two traits (
create_liststyle), 20,000 builds in total.main/ this branch)main/ this branch)main/ this branch)Many distinct combinations: 100 traits, 20,000 builds touching 8,038 distinct trait combinations.
mainRetained 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_specsandbundle exec standardrbon the default GemfileBUNDLE_GEMFILE=gemfiles/7.0.gemfile bundle exec rake all_specsBUNDLE_GEMFILE=gemfiles/main.gemfile bundle exec rake all_specsFound while benchmarking the #1847 to #1853 stack, which removes this mechanism entirely. This fix is independent of that stack.