Skip to content

[Repo Assist] perf: hoist shell normalisation and cache notification regex objectsΒ #34

Closed

Description

@github-actions

πŸ€– This is an automated pull request from Repo Assist.

Summary

Two targeted performance improvements to hot-path code in OpenClaw.Shared.


1. ExecApprovalPolicy.Evaluate β€” hoist normalizedShell out of the loop

Before: normalizedShell was allocated inside the foreach loop, once per rule that carries a Shells filter. With the default 21-rule policy, this allocates a new string on every iteration that has a shell filter β€” wasted work since the value is identical for the lifetime of the call.

After: Compute (shell ?? "powershell").ToLowerInvariant() once before the loop. Zero behaviour change; one string allocation instead of N.


2. NotificationCategorizer.MatchesRule β€” cache compiled Regex instances

Before: The method used Regex.IsMatch(text, pattern, options, matchTimeout) β€” the overload that accepts a matchTimeout bypasses .NET's internal static regex cache (this is documented .NET behaviour). A new Regex object was compiled from scratch on every notification for every regex-pattern user rule.

After: A ConcurrentDictionary(string, Regex?) caches compiled instances keyed by pattern. Each Regex is still constructed with matchTimeout = 100 ms (ReDoS protection preserved), but subsequent notifications reuse the same compiled automaton. Invalid patterns are stored as null so RegexParseException is only thrown once.


Test Status

βœ… All 432 non-integration tests pass (dotnet test). Integration tests are skipped in this environment (Windows-only).

No behaviour changes β€” purely performance improvements.

Generated by Repo Assist Β· β—·

To install this agentic workflow, run

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

Note

This was originally intended as a pull request, but GitHub Actions is not permitted to create or approve pull requests in this repository.
The changes have been pushed to branch repo-assist/improve-exec-notify-perf-2026-03-17-04ada35e031dac93.

Click here to create the pull request

To fix the permissions issue, go to Settings β†’ Actions β†’ General and enable Allow GitHub Actions to create and approve pull requests.

Show patch preview (98 of 98 lines)
From 95bf71e4dbe13c2b447a9b10ced5a8c83a588ef0 Mon Sep 17 00:00:00 2001
From: "github-actions[bot]" <github-actions[bot]@users.noreply.github.com>
Date: Tue, 17 Mar 2026 00:42:37 +0000
Subject: [PATCH] perf: hoist shell normalisation and cache notification regex
 objects

ExecApprovalPolicy.Evaluate: normalizedShell was allocated inside the
foreach loop for every rule that carries a shell filter.  Moving the
computation before the loop avoids the repeated allocation on the hot
command-evaluation path.

NotificationCategorizer.MatchesRule: the static Regex.IsMatch overload
that accepts a matchTimeout bypasses .NET's internal regex cache, so a
new Regex object was compiled on every incoming notification for each
regex-pattern user rule.  Replace with a ConcurrentDictionary<string,
Regex?> that caches compiled instances (constructed with the 100 ms
timeout so ReDoS protection is preserved).  Subsequent notifications
reuse the same compiled automaton.
---
 src/OpenClaw.Shared/ExecApprovalPolicy.cs     |  4 ++-
 .../NotificationCategorizer.cs                | 25 +++++++++++++++----
 2 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/src/OpenClaw.Shared/ExecApprovalPolicy.cs b/src/OpenClaw.Shared/ExecApprovalPolicy.cs
index 3731ce4..3e1ad03 100644
--- a/src/OpenClaw.Shared/ExecApprovalPolicy.cs
+++ b/src/OpenClaw.Shared/ExecApprovalPolicy.cs
@@ -95,6 +95,9 @@ public class ExecApprovalPolicy
             };
         }
         
+        // Compute once; only used if any rule has shell filters.
+        var normalizedShell = (shell ?? "powershell").ToLowerInvariant();
+
         foreach (var rule in _rules)
         {
             if (!rule.Enabled) continue;
@@ -102,7 +105,6 @@ public class ExecApprovalPolicy
             // Check shell filter
             if (rule.Shells is { Length: > 0 })
             {
-                var normalizedShell = (shell ?? "powershell").ToLowerInvariant();
                 if (!rule.Shells.Any(s => s.Equals(normalizedShell, Strin
... (truncated)

Activity

  1. locked as resolved and limited conversation to collaborators on May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions