Skip to content

Index sequences by URI and shrink UriManager - #1865

Draft
neilvcarvalho wants to merge 3 commits into
mainfrom
nc/simplify-sequence-internals-a89371
Draft

neilvcarvalho wants to merge 3 commits into
mainfrom
nc/simplify-sequence-internals-a89371

Conversation

@neilvcarvalho

Copy link
Copy Markdown
Member

Step toward the smaller core: sequence lookup and URI handling, with no change to the public sequence API.

Commits

  1. Trim unused and redundant Sequence internals. Removes has_name?, the protected proc reader and for_factory? (it referenced an undefined factory_name). EnumeratorAdapter memoizes its first value in one place, and seek_value uses an until modifier instead of loop plus a manual StopIteration re-raise.
  2. Index sequences by URI at registration. Internal records each sequence under its URIs in a Hash on Configuration, and Sequence.find_by_uri reads it instead of scanning every global and inline sequence. Sequence.find and has_uri? are removed.
  3. Reduce UriManager to URI building functions. UriManager becomes a module with build_uri and build_uris. Factory, Trait and Definition pass a plain uri_paths list instead of a UriManager object.

lib/factory_bot/sequence.rb goes from 197 to 151 lines and lib/factory_bot/uri_manager.rb from 63 to 35.

Not done, and why

  • Enumerator.produce in place of EnumeratorAdapter. External enumeration needs a Fiber per sequence. Measured with 2000 enumerators: JRuby 9.4 holds one native thread per sequence (1 to 2003 threads) and a peek plus next costs about 9 µs against 0.03 µs; MRI 4.0 adds about 48 KB of RSS per sequence. It would also make integer set_sequence either O(n) or need a second representation to jump, and it shifts when value.next is called. The adapter stays, slimmed.
  • Collapsing the set_value paths. spec/acceptance/sequence_setting_spec.rb stubs Sequence#can_set_value_by_index? under verify_partial_doubles, so the method and the seek fallback for enumerators have to exist. The paths also differ in behavior: find_index has no timeout and does not move the enumerator, while seeking does both.

Behavior note

The first sequence registered under a URI keeps it, which matches the old scan order among global sequences and among inline sequences. The one difference: a global sequence whose normalized name equals an inline URI (for example a global sequence literally named "user/email") used to always win, and now whichever was registered first wins.

Verification

  • bundle exec rake all_specs on Ruby 4.0.10 with the default Gemfile: 309 unit and 457 acceptance examples, 4 scenarios, 0 failures.
  • bundle exec standardrb: no offenses.
  • BUNDLE_GEMFILE=gemfiles/7.0.gemfile on Ruby 3.2.11: green.
  • BUNDLE_GEMFILE=gemfiles/main.gemfile on Ruby 4.0.10: green.

Sequence carried three members nothing calls: has_name?, a protected
proc reader, and for_factory?, which referenced an undefined
factory_name and could only raise. EnumeratorAdapter resolved its
initial value through three methods that each wrote both instance
variables, and seek_value looped with a manual StopIteration re-raise
because Kernel#loop swallows it.

This commit deletes the unused members, memoizes the adapter's first
value in one place, and seeks with an until modifier so StopIteration
reaches the rescue on its own. The adapter still calls a lazy initial
value once and keeps it across rewinds. There are no side effects.
Every generate, generate_list, rewind_sequence and set_sequence call
resolved its sequence by scanning all global sequences and then all
inline sequences, asking each one whether its UriManager included the
URI. The cost of a lookup grew with the number of sequences defined.

This commit has Internal record each sequence under its URIs when it
is registered, in a Hash held by Configuration, and Sequence.find_by_uri
reads that Hash. Sequence exposes its URI list as uris and no longer
needs has_uri?. Sequence.find only served set_sequence, which already
holds a built URI, so it now calls find_by_uri and find is removed
together with its unit examples.

The first sequence registered under a URI keeps it, which matches the
old scan order within global and within inline sequences. The one
difference is a global sequence whose normalized name equals an inline
URI, for example a global sequence named "user/email": the scan always
preferred the global one, and the index prefers whichever was
registered first.
Each Factory, Trait and Sequence built a UriManager object, and
Definition carried it, only so DefinitionProxy could read its URI list
back as the paths for nested traits and sequences. With lookups served
by the index, nothing queries a UriManager after construction.

This commit turns UriManager into a module with two functions,
build_uri and build_uris, where build_uris holds the endpoint and path
expansion the constructor used to do. Factory and Trait compute their
URI list once and hand it to Definition as uri_paths. The unit examples
for the constructor are rewritten against build_uris; the one example
for a constructor called with no endpoints is dropped with it.

The generated URIs and their order are unchanged, so sequence error
messages still name the same URI.
@neilvcarvalho
neilvcarvalho marked this pull request as draft November 27, 2026 20:15

This branch has not been deployed

No deployments
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