Open
perf(core,auto_harness): 预编译热路径正则,消除循环内重复 Pattern.compile #564
perf(core,auto_harness): 预编译热路径正则,消除循环内重复 Pattern.compile #564
Open
linghbAdd on 2 days ago
linghb
linghb
2 days ago

Paired: GitHub #700 ↔ GitCode !564

概述

修复 #336:三处热路径正则重复编译,改为预编译/按 pattern 缓存,匹配语义不变:

位置 原问题 修复
auto_harness/rails/SecurityRail beforeToolCall 每次文件写操作对每个 pattern 重新 Pattern.compile + 拼接正则 构造时预编译为 List<Pattern>(对齐同包 SUSPICIOUS_PATTERNS 惯例),未使用的原始字符串字段一并移除
multiagent/team_runtime/SubscriptionManager getSubscribers 每条消息对每个订阅 pattern 重新编译 新增 matchCompiledPattern,computeIfAbsent 按 pattern 缓存;包私有静态 matchPattern 保持原实现供既有测试调用
multiagent/runtime/SubscriptionManager 同上 同样处理;缓存字段仅在 synchronized 方法内访问,无并发风险

验证

  • PR 涉及的全部相关测试 71 tests,0 failures(SecurityRail 系列 18、SubscriptionManagerTest 24、CompatibilityTest 2、集成 parity 等)
  • 针对性验证 Pr5Check:通配 */?、精确命中、未命中、静态 matchPattern、重复调用缓存路径全部 PASS
  • 行为不变性:匹配语义与测试断言均未改动;SecurityRail 的两个配置列表构造后无修改入口(无 setter、无 getter)
  • 基于上游 develop 最新(d4028b861)
  • git diff --check 通过

Closes #336

Linked Closing Issues:

likedislike
Merge blocked
linghblinghb
2 days ago Associated issue: [perf] 热路径正则重复编译:SecurityRail 每次文件写操作、SubscriptionManager 每条消息均重新 Pattern.compile
atomgit-bot
atomgit-bot
2 days ago Comments:

变更摘要

本次变更针对 #336 中的热路径性能问题,消除了 SecurityRail、multiagent/runtime/SubscriptionManager、multiagent/team_runtime/SubscriptionManager 三处在循环或每条消息处理中重复编译正则的开销:SecurityRail 在构造阶段完成正则预编译,两个 SubscriptionManager 则引入按 pattern 缓存的编译结果。匹配语义(通配符 */?、精确命中、未命中)与既有测试调用入口保持不变。

主要改动

  • SecurityRail 字段改为预编译正则: 将 immutableFiles、highImpactPrefixes 两个 List<String> 字段替换为 immutableFilePatterns、highImpactPrefixPatterns(List<Pattern>),构造函数内不再拷贝原始字符串列表。
  • 新增 compileFnmatchPatterns 构造期编译: 在构造时对每个配置项先做 normalizePath 再 Pattern.compile(fnmatchRegex(...)),空列表直接返回 List.of(),结果以 List.copyOf 固定。
  • matchesAny 调整为 matchesAnyPattern: beforeToolCall 中改用预编译后的 Pattern 直接 matcher(...).matches(),仅对入参路径做一次 normalizePath,不再在循环内编译正则。
  • multiagent/runtime/SubscriptionManager 新增 pattern 缓存: 增加 compiledPatterns(LinkedHashMap<String, Pattern>)字段与 matchesCached,先判断精确相等、再判断是否含通配符,最后用 computeIfAbsent 缓存编译结果;原包私有静态 matchPattern 保留以兼容现有测试。
  • team_runtime/SubscriptionManager 同步缓存实现: 增加 compiledPatterns 字段与 matchCompiledPattern,getSubscribers 改走该路径,缓存仅在被 synchronized 的方法内访问;原 matchPattern 保留。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
2 days ago Comments:

代码审查

✅ 未发现问题

likedislike
Incorrect?
openJiuwen-botopenJiuwen-botCollaborators
2 days ago Set shenwei21,tianmingyong as approver
atomgit-bot
atomgit-bot
2 days ago Comments:

AtomGit AI 助手使用指南

在 PR 评论中使用命令即可触发。支持在普通评论和代码行评论中使用。

功能 命令 说明
代码审查 /ai review 检查代码质量、潜在问题、安全风险
PR 摘要 /ai summary 生成 PR 变更内容的结构化摘要
代码解释 /ai explain 解释代码变更的逻辑和意图
自由提问 @atomgit-bot <问题> 询问关于本 PR 的任何问题
帮助 /ai help 显示此帮助信息

免责声明

AI 助手可能存在误判,请结合自身判断。可以对评论点 👍 或 👎 帮助我们改进。

likedislike
Incorrect?
openJiuwen-botopenJiuwen-botCollaborators
2 days ago Added label: sig/sig-agent-core-java
openJiuwen-bot
openJiuwen-botCollaborators
2 days ago Comments:

欢迎来到 openJiuwen 社区

Hey @linghb , 感谢你对社区的贡献.

机器人使用手册

有关指令的使用,可以点击 此处 查看详情。开发人员可以在每个PR或Issue下方评论特定指令来触发机器人任务。

联系指引

有疑问可以联系 SIG: sig-agent-core-java ,
维护者是: @shenwei21, @tianmingyong ,
审核者是: @foreach1991, @gcw_xJSJ32Ge, @jeffrey_qiao, @kangquansheng_makeIT, @xiongjie_makeit .

likedislike
openJiuwen-botopenJiuwen-botCollaborators
2 days ago Added label: openJiuwen-cla/yes
openJiuwen-bot
openJiuwen-botCollaborators
2 days ago Comments:

CLA 签署成功

linghb, 感谢您的代码贡献。 所有的代码贡献者都已完成了 CLA 签署。 👍

likedislike
openjiuwen-syncopenjiuwen-syncCollaborators
2 days ago Modify description of pull request
openjiuwen-syncopenjiuwen-syncCollaborators
2 days ago Modify description of pull request
openJiuwen-bot
openJiuwen-botCollaborators
2 days ago Comments:
TASK STATUS DETAILS
CodeCheck ✅SUCCESS Click here
Blacklist Scan N/A N/A
AntiPoison ✅SUCCESS Click here
Software Composition Analysis ✅SUCCESS Click here
Maven Build ❌FAILED See CHECK tab
likedislike
openJiuwen-botopenJiuwen-botCollaborators
2 days ago Added label: ci-failed
openJiuwen-botopenJiuwen-botCollaborators
2 days ago Removed label: ci-failed
openJiuwen-bot
openJiuwen-botCollaborators
2 days ago Comments:
TASK STATUS DETAILS
CodeCheck ✅SUCCESS Click here
Blacklist Scan N/A N/A
AntiPoison ✅SUCCESS Click here
Software Composition Analysis ✅SUCCESS Click here
Maven Build ✅SUCCESS See CHECK tab
likedislike
openJiuwen-botopenJiuwen-botCollaborators
2 days ago Added label: ci-successful
thesky58
thesky5817 hours agoStart code review1
core/src/main/java/com/openjiuwen/core/multiagent/team_runtime/SubscriptionManager.java
@@ -124,0 +125,4 @@
125+ return true;
126+ }
127+ if (pattern.indexOf('*') >= 0 || pattern.indexOf('?') >= 0) {
128+ Pattern compiled = compiledPatterns.computeIfAbsent(pattern,
thesky58
thesky5817 hours agoComments:

这里在只读方法 getSubscribers 里往 compiledPatterns 写:computeIfAbsent 命中新 pattern 时会对这个普通 LinkedHashMap 做 put(可能触发 resize)。调用路径是 MessageRouter.routePubsubMessage(第 73 行)→ MessageBus.publish,而 publish 在各 agent 自己的线程上同步执行,多个 teammate 同时发消息、agent 启停时 subscribe/unsubscribe 并发改动都会碰在一起。这个 PR 之前 getSubscribers 只读 subscriptions,现在它自己也成了写者,风险面变大。HashMap/LinkedHashMap 并发写没有任何语义保证,常见表现是条目丢失或桶结构错乱,缓存丢了会重算,但在后续任意一次读上都可能出现异常行为。同 PR 的 core/multiagent/runtime/SubscriptionManager.getSubscribers 是 synchronized 的,这个类漏了。

建议把这行换成 private final Map<String, Pattern> compiledPatterns = new ConcurrentHashMap<>();(computeIfAbsent 原子,最小改动),或者给 getSubscribers/subscribe/unsubscribe 补锁,与另一个 SubscriptionManager 保持一致。

likedislike