Repository navigation
Match resource templates in linear time - #760
Merged
koic merged 1 commit intoDec 3, 2026
Merged
Conversation
## Motivation and Context
`ResourceTemplate.match_uri` matched a URI with a pattern that gives each `{var}` the group `[^/]+`. Two or more variables
in one path segment, as in `file:///{name}.{ext}`, leave a backtracking engine many ways to split a long run of the separator,
and Ruby before 3.2, which the gem still supports, does not memoize the search. On Ruby 2.7, `file:///` followed by 40,000 dots and a `/`
took 5 seconds to fail against that template, and 800 separators took 0.4 seconds against one with three variables, growing with the cube
of the length.
`match_uri` now finds the same match without a search. A literal that holds a `/` can sit in one place only, since no variable holds one,
and inside a run of variables between two such places taking the last occurrence of each separator from the right gives the split
the pattern gives. The cost is linear in the length of the URI on every Ruby.
## How Has This Been Tested?
`test/mcp/resource_template_test.rb`, including a comparison with the previous pattern over generated templates and URIs.
Checked by hand on Ruby 2.7.8: the two inputs above return at once, and 1,200,000 generated inputs give the result of the pattern.
## Breaking Changes
None for a URI that is a valid String. `match_uri` returns `nil` for a String that is not valid in its encoding and for a value that is
neither a String nor `nil`, where the pattern raised `ArgumentError` or `TypeError` or, for a Symbol, matched it.
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.
Motivation and Context
ResourceTemplate.match_urimatched a URI with a pattern that gives each{var}the group[^/]+. Two or more variables in one path segment, as infile:///{name}.{ext}, leave a backtracking engine many ways to split a long run of the separator, and Ruby before 3.2, which the gem still supports, does not memoize the search. On Ruby 2.7,file:///followed by 40,000 dots and a/took 5 seconds to fail against that template, and 800 separators took 0.4 seconds against one with three variables, growing with the cube of the length.match_urinow finds the same match without a search. A literal that holds a/can sit in one place only, since no variable holds one, and inside a run of variables between two such places taking the last occurrence of each separator from the right gives the split the pattern gives. The cost is linear in the length of the URI on every Ruby.How Has This Been Tested?
test/mcp/resource_template_test.rb, including a comparison with the previous pattern over generated templates and URIs. Checked by hand on Ruby 2.7.8: the two inputs above return at once, and 1,200,000 generated inputs give the result of the pattern.Breaking Changes
None for a URI that is a valid String.
match_urireturnsnilfor a String that is not valid in its encoding and for a value that is neither a String nornil, where the pattern raisedArgumentErrororTypeErroror, for a Symbol, matched it.Types of changes
Checklist