-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Avoid using Regex for matching URL paths when possible as doing so causes more of the request time to be spent in regex then doing the API call itself. #24873
New issue
Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “No Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? No Sign in to your account
Comments
Hi, regex matching will be skipped, if the requested location equals the mapping string. Can you provide a sample please? |
In general most URLs looks like:
In some cases when
The other complex case we have is where the From memory when I last looked deep into the Antmatcher stuff I think in both of those cases it makes a regex and does a regex match. |
I did a small research and it is possible to increase from
However, I can't say whether it make sense to implement solution like this, need judgement from someone from spring team |
Do you mind sharing the code you did to test that? |
Sure, I will prepare the PR later |
As a side note, I'd like to point out that we're aware that This implementation is not currently the default in Spring MVC as it's a breaking change - but it's a change we're considering for future versions. We'll gladly consider improvements in
Thanks! |
Code style issues fixed :) I left just one sample in |
I've benchmarked the proposed PR and also variants of these changes. I've reused a benchmark that we used for testing the Judging from the results, I don't think this is worth pursuing right now. We'll gain way more by switching to the new implementation. Thanks all! |
Is it at least possible to switch to the new implementation? |
@LukeButters this is not possible right now as there is a behavior and configuration change. We’ll consider a migration path in a future Spring version. |
Can you share your test scenario please? What I see with JMH is |
There is an issue for that now #24945. |
Affects: I don't know of a version this does not affect.
Hi, profiling reveals that a bunch of time is spent in in this stack trace:
I think this is trying to work out which API method to call and to do that it is matching the URL path against a regex. Regex is extremely slow, by using regex here it makes the APIs spend most of their time in this rather than doing the actual work. This is not good especially when we want to use spring to slice up an application served over many APIs e.g. micro services. I think a case in which this gets especially bad is when many APIs exist, I think this does a linear search :(.
One step towards not having all time spent in this regex matching would be to create simple matchers for some of the more trivial and common cases like those where the API method is not presenting a complex regex. I think some thought might need to go into this, you may want to collection the
value
of a bunch of@RequestMapping
s to find out what common styles of request mappings are in use.I think this is looking at all
@RequestMapping
trying to find which ones match, perhaps what might also work is having the matcher defined as two sections a quick match which is fast to exclude (e.g. path length, starts with X etc) and if that matches then go on to the slow regex matching.The text was updated successfully, but these errors were encountered: