Repository navigation
[Repo Assist] perf: eliminate ToLowerInvariant() allocations in shell-type comparison hot paths - #217
Closed
github-actions[bot] wants to merge 1 commit into
Conversation
…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]>
12 tasks
Collaborator
|
Superseded by #218, which includes the behavior-preserving allocation cleanup from this PR. |
shanselman
deleted the
repo-assist/perf-avoid-tolowerinvariant-2026-04-27-86c85715a9d6c828
branch
April 27, 2026 02:48
26 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤖 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.BuildProcessArgsReplaced
(request.Shell ?? "powershell").ToLowerInvariant()+switchon the lowercased string with case-insensitiveif-elsecomparisons (StringComparison.OrdinalIgnoreCase). One heap allocation eliminated persystem.runinvocation.ExecApprovalPolicy.EvaluateReplaced
(shell ?? "powershell").ToLowerInvariant()with aReadOnlySpan<char>captured once, then compared using.Equals(shellSpan, StringComparison.OrdinalIgnoreCase)in the per-rule shell-filter loop. One allocation eliminated per policy evaluation.NotificationCategorizer.ClassifyReplaced the two-step
rule.Category.ToLowerInvariant()+CategoryTitles.GetValueOrDefault()lookup with a singleCategoryTypeMapFrozenDictionary<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 toToLowerInvariant(). RemovedCategoryTitles(superseded byCategoryTypeMap).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-elseinBuildProcessArgsis marginally more lines than the originalswitch, but eliminates an allocation that theswitchinherently requires.CategoryTypeMapduplicates some data already present inIntentMap/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 faileddotnet test OpenClaw.Tray.Tests: ✅ 122 passed, 0 failed