apache / apache/shenyu

[BUG] Condition predicate judges recompile regex/parse pattern on every request

Open
#6,564 1 comment 0 reactions 0 assignees View on GitHub
plugin: divide priority: high type: performance
Dominant language
Java
Stars
8.8k
Forks
3.1k
Avg merge
7d 1h
Merged PRs (30d)
85

Description

## Description
The three predicate judges invoked from `PredicateJudgeFactory.judge` (once per condition per rule/selector per request, on cache miss) compile/parse their pattern fresh on every call. `RegexPredicateJudge` calls `com.google.re2j.Pattern.matches(regex, input)` (no compiled-pattern cache); `PathPatternPredicateJudge` calls `PathPatternParser.defaultInstance.parse(...)` per call; `MatchPredicateJudge` runs `AntPathMatcher.doMatch` which builds an `AntPathStringMatcher` (compiling a `java.util.regex.Pattern`) per call when `cacheToPatternThreshold=0` (default). The L1 `MatchDataCache` only short-circuits rules whose conditions are all `uri`-type, so any rule mixing header/query/cookie conditions pays the full compile cost every request.

## Location
```
shenyu-plugin-base/.../condition/judge/RegexPredicateJudge.java:33
PathPatternPredicateJudge.java:39 -> PathMatchUtils.java:66
MatchPredicateJudge.java:36 -> PathMatchUtils.java:54-55
```

## Impact
`req/s x (#regex/pathPattern/match conditions in matched rules)` pattern compiles per second. A rule with 3 such conditions at 10k req/s = 30k compiles/s of CPU on the Netty event loop. Regex compilation is the dominant per-request CPU cost on the match path for non-URI-only rules.

## Suggested fix
Cache a compiled `Pattern`/`PathPattern` keyed by the condition's `paramValue` (bounded Caffeine, or precompile at rule-sync time and store the compiled pattern on `ConditionData`). For `AntPathMatcher`, enable `setCachePatterns(true)` or migrate URI matching to `PathPattern`.

## Related existing issue(s)
#6404 is about regex timeout protection, not compilation cost — distinct.

_Identified during the 2026-08-02 audit; full list in [`docs/issue-candidates-2026-08-02.md`](docs/issue-candidates-2026-08-02.md)._

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with PredicateJudgeFactory.judge and inspect RegexPredicateJudge.java, PathPatternPredicateJudge.java, MatchPredicateJudge.java, and the referenced PathMatchUtils.java call paths. Compare the current per-call compilation behavior with the proposed cache or rule-sync approach, then verify that matching behavior is unchanged and patterns are not repeatedly compiled for each request.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend-api-design, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.