Skip to content

[Repo Assist] perf: eliminate ToLowerInvariant() allocations in shell-type comparison hot paths - #217

Closed
github-actions[bot] wants to merge 1 commit into
masterfrom
repo-assist/perf-avoid-tolowerinvariant-2026-04-27-86c85715a9d6c828
Closed

github-actions[bot] wants to merge 1 commit into
masterfrom
repo-assist/perf-avoid-tolowerinvariant-2026-04-27-86c85715a9d6c828

Conversation

@github-actions

Copy link
Copy Markdown
Contributor

🤖 This is an automated PR from Repo Assist.

Eliminates three per-call ToLowerInvariant() string allocations from the command-execution and notification-classification paths.

What changed

LocalCommandRunner.BuildProcessArgs

Replaced (request.Shell ?? "powershell").ToLowerInvariant() + switch on the lowercased string with case-insensitive if-else comparisons (StringComparison.OrdinalIgnoreCase). One heap allocation eliminated per system.run invocation.

ExecApprovalPolicy.Evaluate

Replaced (shell ?? "powershell").ToLowerInvariant() with a ReadOnlySpan<char> captured once, then compared using .Equals(shellSpan, StringComparison.OrdinalIgnoreCase) in the per-rule shell-filter loop. One allocation eliminated per policy evaluation.

NotificationCategorizer.Classify

Replaced the two-step rule.Category.ToLowerInvariant() + CategoryTitles.GetValueOrDefault() lookup with a single CategoryTypeMap FrozenDictionary<string, (title, type)> (case-insensitive) that stores both title and lowercase type pre-baked. For known categories (the common case) ToLowerInvariant() is never called; unknown categories still fall through to ToLowerInvariant(). Removed CategoryTitles (superseded by CategoryTypeMap).

Why this matters

All three sites are on call paths that fire for every gateway command or notification event. While each allocation is small, they accumulate over a long-running tray session and contribute to GC pressure. The changes are semantically equivalent and carry no risk of behaviour change.

Trade-offs

  • if-else in BuildProcessArgs is marginally more lines than the original switch, but eliminates an allocation that the switch inherently requires.
  • CategoryTypeMap duplicates some data already present in IntentMap / ChannelMap, but these maps serve different lookup keys; the duplication is intentional and documented.

Test Status

  • ./build.ps1: ❌ requires Windows (Linux CI environment)
  • dotnet test OpenClaw.Shared.Tests: ✅ 663 passed, 20 skipped, 0 failed
  • dotnet test OpenClaw.Tray.Tests: ✅ 122 passed, 0 failed

Generated by 🌈 Repo Assist, see workflow run. Learn more.

To install this agentic workflow, run

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

…on hot paths

- LocalCommandRunner.BuildProcessArgs: replace ToLowerInvariant()+switch
  with OrdinalIgnoreCase if-else; avoids one string allocation per command run
- ExecApprovalPolicy.Evaluate: replace ToLowerInvariant() with span-based
  OrdinalIgnoreCase comparison per shell filter rule; avoids one string
  allocation per policy evaluation
- NotificationCategorizer.Classify: replace CategoryTitles FrozenDictionary<string,string>
  + ToLowerInvariant() with CategoryTypeMap FrozenDictionary<string,(title,type)>;
  avoids ToLowerInvariant() allocation for known categories on every matching
  user-rule notification

All three changes are semantically equivalent; covered by existing test suites.

Co-authored-by: Copilot <[email protected]>
@shanselman

Copy link
Copy Markdown
Collaborator

Superseded by #218, which includes the behavior-preserving allocation cleanup from this PR.

@shanselman shanselman closed this Apr 27, 2026
@shanselman
shanselman deleted the repo-assist/perf-avoid-tolowerinvariant-2026-04-27-86c85715a9d6c828 branch April 27, 2026 02:48
@clawsweeper clawsweeper Bot mentioned this pull request Jun 12, 2026
26 tasks
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.

1 participant