Skip to content

[Repo Assist] perf: eliminate string allocations in ClassifyByKeywords and ClassifyTool#101

Draft
github-actions[bot] wants to merge 1 commit intomasterfrom
repo-assist/perf-eliminate-string-alloc-hotpaths-640d82512de5602b
Draft

[Repo Assist] perf: eliminate string allocations in ClassifyByKeywords and ClassifyTool#101
github-actions[bot] wants to merge 1 commit intomasterfrom
repo-assist/perf-eliminate-string-alloc-hotpaths-640d82512de5602b

Conversation

@github-actions
Copy link
Contributor

🤖 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.ClassifyByKeywords

Before: var lower = text.ToLowerInvariant() then lower.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 optimised OrdinalIgnoreCase path.

OpenClawGatewayClient.ClassifyTool

Before: toolName.ToLowerInvariant() in a switch expression — allocates a lowercase copy of the tool name on every tool-use event.

After: FrozenDictionary(string, ActivityKind) with OrdinalIgnoreCase — O(1) case-insensitive lookup, no per-call allocation. FrozenDictionary is optimised for read-heavy, write-never workloads (available since .NET 8).

Why it matters

Both methods sit on the hot path:

  • ClassifyByKeywords runs for every incoming notification.
  • ClassifyTool runs 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.

Generated by Repo Assist ·

To install this agentic workflow, run

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

…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>
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