Refactor and Optimization of Match Resource Definition #3649
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.
PR Type
Feature / Refactor
PR Checklist
Overview
Current Behavior
Currently, to check if a resource matches a definition using capture_match_info_fn, we always need to pass a function to perform additional checks.
This causes two problems:
New Behavior
The capture_match_info function was refactored into the following components:
Additionally, the Router Builder has been enhanced with a new mechanism to track path conflicts:
When a resource definition is added, it increments a conflict counter if its path pattern matches one previously added (ignoring guards and other configurations).
At the end of the build process, it computes the maximum number of path conflicts.
This enables an early return optimization in recognize_fn()—if the number of matching paths reaches the recorded conflict maximum, further checks can be skipped.
Performance Test
Old Implementation
New Implementation
After multiple test suite executions, the following performance improvements were observed:
The performance gain in the "no guard check failed" scenario is primarily due to the elimination of unnecessary data structure initializations (specifically, segment allocations) when performing static resource path matching.
Closes #3645