Add ServerWebExchange a new attribute for PathContainer to avoid repeating parsePath in PathRoutePredicateFactory. #2884
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.
What I DO
During lookupRoute, serverWebExchange add a new attribute for PathContainer to avoid repeating parsePath in PathRoutePredicateFactory. Thereby reducing cpu load and increasing throughput.
1. Background:
We have an Spring Cloud Gateway online service with about 2k path predicate routes;
And we found that Spring Cloud Gateway has high cpu load when pressure test request of a route.
2. Flame Graph and Cause
The CPU flame graph shows that the PathContainer.parsePath in PathRoutePredicateFactory consumes high cpu.
From the code, It is normal that the large number of routes leads to frequent execution of Predicate.apply.
But an exchange’s PathContainer is unchanged during the lookupRoute, there is no need to parsePath every time when test predicate.
PathRoutePredicateFactory's test method.
3. Change Details
So in PathRoutePredicateFactory, I hope exchange add a new attribute as below, to store its pathContainer.
When there are many path routes, only parsePath one time for a request.
Below is the cpu flame graph after the update.
4. BenchMark
The following is the benchmark result of PathRoutePredicateFactory‘s test method. Throughput has been significantly improved after adding the new attribute.
Result of Test method with new attribute.

Result of Test method without new attribute.
