openhuman icon indicating copy to clipboard operation
openhuman copied to clipboard

Perf: TokenJuice regex_match/regex_replace recompile on every call

Open Liohtml opened this issue 6 months ago • 2 comments

Summary

src/openhuman/tokenjuice/reduce.rs — regex_match(), regex_replace(), and regex_captures() compile a new Regex via Regex::new(pattern) on every invocation. These are called in loops for every line of tool output. For large outputs (hundreds of lines × multiple patterns), this causes significant unnecessary overhead.

Impact

Medium — Measurable latency on large tool outputs. Regex compilation is O(pattern_length) and dominates per-line processing.

Fix Available

PR #1918 replaces these with a thread-local regex cache. All 74 existing tests pass unchanged.

Liohtml avatar May 16 '26 14:05 Liohtml

Status check against current origin/main: this appears resolved by #1918.

Evidence:

  • #1918 is merged: https://github.com/tinyhumansai/openhuman/pull/1918
  • Current src/openhuman/tokenjuice/reduce.rs has a thread_local! regex cache around the helper functions instead of compiling every call.
  • The call sites still go through regex_match, regex_replace, and regex_captures, but those helpers now reuse cached Regex instances.

I think this issue can be closed as fixed unless there is a separate remaining perf concern beyond the per-pattern compilation cache.

honor2030 avatar May 17 '26 15:05 honor2030

Confirmed resolved by #1918 (merged). The regex cache is now in place. Closing.

Liohtml avatar May 17 '26 15:05 Liohtml