Skip to content

[Repo Assist] perf: cache HTML sanitize regex; replace ToLowerInvariant with FrozenDictionary; fix GetRawText double-call#105

Draft
github-actions[bot] wants to merge 1 commit intomasterfrom
repo-assist/perf-cache-regex-notiftype-20260324-92d92d801cb29591
Draft

[Repo Assist] perf: cache HTML sanitize regex; replace ToLowerInvariant with FrozenDictionary; fix GetRawText double-call#105
github-actions[bot] wants to merge 1 commit intomasterfrom
repo-assist/perf-cache-regex-notiftype-20260324-92d92d801cb29591

Conversation

@github-actions
Copy link
Contributor

🤖 This is an automated draft PR from Repo Assist.

Summary

Three targeted performance fixes across two hot-path methods and one debug statement.

1. CanvasWindow.SanitizeHtml — cache HTML sanitization regex patterns

Before: Two Regex.Replace() calls with inline pattern strings — the .NET Regex static method recompiles each pattern from scratch on every invocation.

After: Two static readonly compiled Regex fields (s_sanitizeBlock, s_sanitizeSelfClose). Pattern compilation happens once at startup; subsequent calls do a hash-table lookup and apply the pre-JIT'd automaton directly. Zero per-call allocation.

SanitizeHtml runs every time canvas content is loaded or navigated, so this avoids repeated Regex JIT work.

2. App.ShouldShowNotification — replace ToLowerInvariant() switch with FrozenDictionary

Before: notification.Type?.ToLowerInvariant() allocated a lowercase string copy on every incoming notification before the switch evaluation.

After: static readonly FrozenDictionary(string, Func<SettingsManager, bool)> with OrdinalIgnoreCase comparer — O(1) case-insensitive hash lookup, no per-call allocation. Matches the pattern already applied to ClassifyTool in PR #101.

ShouldShowNotification runs for every notification received from the gateway.

3. OpenClawGatewayClient.HandleChatEvent — eliminate duplicate GetRawText() call

Before: root.GetRawText().Substring(0, Math.Min(200, root.GetRawText().Length)) called GetRawText() twice, materialising the full JSON string twice.

After: Result stored in a local variable; second materialisation eliminated.

Test Status

✅ All 503 shared tests pass, 18 skipped (infrastructure-only). dotnet test tests/OpenClaw.Shared.Tests/
✅ All 93 Tray tests pass. dotnet test tests/OpenClaw.Tray.Tests/
⚠️ dotnet build src/OpenClaw.Tray.WinUI is not buildable on Linux (XAML compiler is a Windows-only PE binary) — same infrastructure limitation as all other WinUI PRs. Logic verified by inspection; changes are purely additive fields and method body updates.

Generated by Repo Assist ·

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@cbb46ab386962aa371045839fc9998ee4e97ca64

…Dictionary in ShouldShowNotification; fix GetRawText double-call

- CanvasWindow.SanitizeHtml: two anonymous Regex.Replace calls now use
  static readonly compiled fields (s_sanitizeBlock, s_sanitizeSelfClose).
  Previously each call to SanitizeHtml recompiled both patterns from
  scratch (Regex JIT + allocation on every canvas load).

- App.ShouldShowNotification: replaced 'Type?.ToLowerInvariant()' switch
  with a static FrozenDictionary<string, Func<SettingsManager, bool>>
  using OrdinalIgnoreCase. Runs for every incoming notification; old code
  allocated a lowercase string copy every call; new code does an O(1)
  hash-table lookup with zero allocation.

- OpenClawGatewayClient.HandleChatEvent: GetRawText() was called twice in
  the same debug expression, materialising the JSON string twice. Now
  called once and stored in a local.

All 503 shared tests pass, 18 skipped (infra). All 93 Tray tests pass.
WinUI build fails on Linux (XAML compiler is Windows-only PE) — same
infrastructure limitation as all other WinUI PRs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants