Repository navigation
Index sequences by URI and shrink UriManager - #1865
Draft
neilvcarvalho wants to merge 3 commits into
Draft
neilvcarvalho wants to merge 3 commits into
neilvcarvalho wants to merge 3 commits into
Conversation
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
marked this pull request as draft
November 27, 2026 20:15
This branch has not been deployed
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.
Step toward the smaller core: sequence lookup and URI handling, with no change to the public sequence API.
Commits
has_name?, the protectedprocreader andfor_factory?(it referenced an undefinedfactory_name).EnumeratorAdaptermemoizes its first value in one place, andseek_valueuses anuntilmodifier instead ofloopplus a manualStopIterationre-raise.Internalrecords each sequence under its URIs in a Hash onConfiguration, andSequence.find_by_urireads it instead of scanning every global and inline sequence.Sequence.findandhas_uri?are removed.UriManagerbecomes a module withbuild_uriandbuild_uris.Factory,TraitandDefinitionpass a plainuri_pathslist instead of aUriManagerobject.lib/factory_bot/sequence.rbgoes from 197 to 151 lines andlib/factory_bot/uri_manager.rbfrom 63 to 35.Not done, and why
Enumerator.producein place ofEnumeratorAdapter. 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 apeekplusnextcosts about 9 µs against 0.03 µs; MRI 4.0 adds about 48 KB of RSS per sequence. It would also make integerset_sequenceeither O(n) or need a second representation to jump, and it shifts whenvalue.nextis called. The adapter stays, slimmed.set_valuepaths.spec/acceptance/sequence_setting_spec.rbstubsSequence#can_set_value_by_index?underverify_partial_doubles, so the method and the seek fallback for enumerators have to exist. The paths also differ in behavior:find_indexhas 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_specson 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.gemfileon Ruby 3.2.11: green.BUNDLE_GEMFILE=gemfiles/main.gemfileon Ruby 4.0.10: green.