[Repo Assist] perf: eliminate string allocations in ClassifyByKeywords and ClassifyTool#101
Draft
github-actions[bot] wants to merge 1 commit intomasterfrom
Conversation
…Tool NotificationCategorizer.ClassifyByKeywords allocated a full lowercase copy of the notification text (text.ToLowerInvariant()) on every call. Replace with Contains/IndexOf overloads that accept StringComparison, eliminating the heap allocation entirely. OpenClawGatewayClient.ClassifyTool allocated a lowercase copy of the tool name on every tool-use event via ToLowerInvariant() used in a switch expression. Replace with a FrozenDictionary<string, ActivityKind> using OrdinalIgnoreCase, which gives O(1) case-insensitive lookup with no per-call allocation. Both code paths are on the hot path: ClassifyByKeywords is called for every incoming notification, and ClassifyTool for every tool event in an active gateway session. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This was referenced Mar 24, 2026
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 draft PR from Repo Assist.
Summary
Two hot-path methods were allocating unnecessary strings on every invocation. This PR eliminates those allocations.
NotificationCategorizer.ClassifyByKeywordsBefore:
var lower = text.ToLowerInvariant()thenlower.Contains(...)— allocates a full lowercase copy of the notification text for every classification.After:
text.Contains(x, StringComparison.OrdinalIgnoreCase)— zero allocation, same semantics, slightly faster on .NET 10's optimisedOrdinalIgnoreCasepath.OpenClawGatewayClient.ClassifyToolBefore:
toolName.ToLowerInvariant()in a switch expression — allocates a lowercase copy of the tool name on every tool-use event.After:
FrozenDictionary(string, ActivityKind)withOrdinalIgnoreCase— O(1) case-insensitive lookup, no per-call allocation.FrozenDictionaryis optimised for read-heavy, write-never workloads (available since .NET 8).Why it matters
Both methods sit on the hot path:
ClassifyByKeywordsruns for every incoming notification.ClassifyToolruns for every tool event in an active gateway session (potentially many per second during active AI sessions).Eliminating these allocations reduces GC pressure incrementally, which helps keep notification latency low.
Test Status
✅ All 503 tests pass, 18 skipped (infrastructure-only). Run:
dotnet test tests/OpenClaw.Shared.Tests/No behaviour changes — existing tests cover case-insensitivity and all keyword/intent/channel paths.