Refactor and Optimization of Match Resource Definition#3649
Open
manuelgdlvh wants to merge 10 commits intoactix:mainfrom
Open
Refactor and Optimization of Match Resource Definition#3649manuelgdlvh wants to merge 10 commits intoactix:mainfrom
manuelgdlvh wants to merge 10 commits intoactix:mainfrom
Conversation
JohnTitor
requested changes
Feb 6, 2026
Member
JohnTitor
left a comment
There was a problem hiding this comment.
Thanks for the PR!
While we'd have some wins for specific cases, I think we need more tests to validate changes and confirm there's no behavior change.
Also I'm happy to see clippy warnings are addressed.
| if let Some((_, path_conflicts)) = self | ||
| .path_conflicts | ||
| .iter_mut() | ||
| .find(|(route_idx, _)| rdef.eq(&self.routes.get(*route_idx).unwrap().0)) |
Member
There was a problem hiding this comment.
I think rdef.eq here doesn't work well for some code, e.g. when /yay and /{val} are registered at the same time. These have different patterns but should be routed both, i.e. treated as the same path when requesting /yay and we have to fall back into /{val} when /yay doesn't work.
actix-web/actix-router/src/resource.rs
Line 1086 in 2c0be64
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.
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