-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Avoid using regex matching for static patterns #24887
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
Conversation
I've run these changes and variants as JMH benchmarks and I'm not seeing significant improvements (a bit better or worse than the current implementation depending on the seed data, but always within the error margin). I guess the JVM is already optimizing those bits for us; the performance test strategy used in this PR is flawed. I'd suggest to look at JMH for a proper benchmark harness. I'm closing this PR for now. Thanks! |
I don’t think JVM is able to do such optimization. Can you provide your test? |
Alright, the same test using JMH:
Result is |
Alright this is a very targeted improvement but an improvement still. Thanks! |
Great, thanks! I know it is in the core, so I'll double check it and maybe add more functional tests in the meanwhile. |
You can remove the performance test, as we're about to remove them anyway in #24830. I'll ping you if I need some input for contributing JMH tests but I think we should be good. |
c6e45c9
to
221386c
Compare
I've fixed one more issue, should be final now |
221386c
to
4d7dc2c
Compare
Prior to this commit (and the previous one), the `AntPathStringMatcher` (inner class of `AntPathmatcher`) would compile `Pattern` instances and use regex matching even for static patterns such as `"/home"`. This change introduces a shortcut in the string matcher algorithm to skip the `Pattern` creation and uses `String` equality instead. Static patterns are quite common in applications and this change can bring performance improvements, depending on the mix of patterns configured in the web application. In benchmarks (added with this commit), we're seeing +20% throughput and -40% allocation. This of course can vary depending on the number of static patterns configured in the application. Closes spring-projectsgh-24887
This fixes the issue #24873
As of now,
AntPathStringMatcher
uses regex matching even when:.*
. In such cases it is always a positive matchApplying such optimizations increased
PatternsRequestCondition
evaluation (on my machine) from ~440 ops/ms to ~700 ops/msAntPathStringMatcher
is used not only for URL matching, so it would be nice to double-check carefully