Aias00 opened a new issue, #7829:
URL: https://github.com/apache/shenyu/issues/7829

   ### Description
   
   `SensitiveWordPlugin.dictionary()` reparses the configured rule words on 
every inspected request, even when the cached dictionary is valid and the 
parsed words will not be used.
   
   Confirmed in Apache ShenYu 2.7.2-SNAPSHOT and upstream 
`master@7cc1818ace246f60a608e44b5fe985dff46908e7`. The inspected upstream file 
is identical to the current working copy.
   
   ### Code evidence and trigger
   
   In 
[`dictionary()`](https://github.com/apache/shenyu/blob/7cc1818ace246f60a608e44b5fe985dff46908e7/shenyu-plugin/shenyu-plugin-ai/shenyu-plugin-ai-sensitive-word/src/main/java/org/apache/shenyu/plugin/ai/sensitive/word/SensitiveWordPlugin.java#L195-L203),
 `parseWords(handle.getWords())` executes before looking up the cached 
dictionary and checking its expiry:
   
   ```java
   List<String> words = parseWords(handle.getWords());
   CachedDictionary cached = 
SensitiveWordPluginDataHandler.DICTIONARIES.get().obtainHandle(dictionaryKey);
   if (Objects.nonNull(cached) && 
!cached.isExpired(handle.getRefreshIntervalSeconds())) {
       automaton = Mono.just(cached.getAutomaton());
   }
   ```
   
   On that hit path, `words` is unused. 
[`parseWords()`](https://github.com/apache/shenyu/blob/7cc1818ace246f60a608e44b5fe985dff46908e7/shenyu-plugin/shenyu-plugin-ai/shenyu-plugin-ai-sensitive-word/src/main/java/org/apache/shenyu/plugin/ai/sensitive/word/SensitiveWordPlugin.java#L242-L253)
 splits and trims the configured string, allocates a list, and uses 
`List.contains()` for each token. For many distinct words, deduplication is 
O(n²) in the number of tokens. This work runs before the later scheduler 
handoff.
   
   Trigger: requests inspected by a configured sensitive-word rule with 
nonempty `words`, while its dictionary cache entry remains valid. Repeated 
requests with unchanged configuration continue doing the same parsing.
   
   ### Expected behavior / suggested direction
   
   Check the dictionary cache before parsing rule words. Parse only when a 
dictionary load/rebuild needs them, or keep a parsed representation tied to the 
rule configuration. Preserve cache invalidation and Redis-failure fallback 
behavior, including enforcement of configured rule words when no Redis 
dictionary is available.
   
   ### Task List
   
   - [ ] Avoid parsing rule words on valid cached-dictionary hits.
   - [ ] Preserve miss/expiry loading, rule-change invalidation, 
stale-dictionary fallback, and rule-word-only fallback.
   - [ ] Verify that repeated cache hits do not redo rule-word parsing.
   - [ ] Measure allocation/latency changes before claiming a performance gain.
   
   ### Validation limits
   
   The redundant work and its trigger are confirmed by source inspection. No 
throughput benchmark was run; the relative impact depends on configured word 
count and traffic. This is a performance optimization task, not a claim of 
incorrect moderation results.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to