Skip to content

[Repo Assist] perf: cache JsonSerializerOptions and compile command-format Regex in WindowsNodeClient #65

Closed

Description

@github-actions

🤖 This is an automated PR from Repo Assist.

Summary

WindowsNodeClient was allocating a fresh JsonSerializerOptions instance on every outbound message and calling Regex.IsMatch with an ad-hoc pattern string on every inbound node.invoke / node.invoke.request message. Both are measurably expensive in hot paths.

Root cause

JsonSerializerOptions builds internal reflection/source-gen type-info caches on first use — creating a new one per message wastes memory and CPU. Regex.IsMatch with a literal pattern string re-compiles (or looks up in a small built-in cache) the pattern on every call.

Changes

File Change
WindowsNodeClient.cs Add using System.Text.Json.Serialization + using System.Text.RegularExpressions
Add _jsonOptionsIgnoreNull static readonly field (WhenWritingNull) — used in SendNodeInvokeResultAsync and SendInvokeResponseAsync
Add _jsonOptionsIndented static readonly field (WriteIndented = true) — used for debug registration log
Add _commandFormatRegex static compiled Regex — replaces two inline Regex.IsMatch calls
Fix double payload.ToString() in debug-log path

Performance impact

  • JsonSerializerOptions allocation: from per-message to once at startup (static field)
  • Regex compilation: from per-message to once at startup (compiled static field)
  • Debug log: one fewer JsonElement.ToString() call per response received

Trade-offs

None. These are pure caching changes with no behaviour difference. The Regex pattern is identical to what was used inline.

Test Status

All 501 tests run (483 passed, 18 skipped — same as baseline). Build succeeded with 0 errors, 0 warnings from the changed file.

Passed! - Failed: 0, Passed: 483, Skipped: 18, Total: 501

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/perf-cache-jsonserialization-2026-03-18-4b46357ab3fe579e.

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 (115 of 115 lines)
From 6c6533a473d7e95f4e68e69204a5477e7b4dabba Mon Sep 17 00:00:00 2001
From: "github-actions[bot]" <github-actions[bot]@users.noreply.github.com>
Date: Wed, 18 Mar 2026 12:54:29 +0000
Subject: [PATCH] perf: cache JsonSerializerOptions and compile command-format
 Regex in WindowsNodeClient

- Add two static readonly JsonSerializerOptions fields (_jsonOptionsIgnoreNull,
  _jsonOptionsIndented) shared across all serialization calls.  JsonSerializerOptions
  is expensive to construct because it builds internal reflection/source-gen caches;
  previously a new instance was allocated on every node.invoke and node.invoke.result
  message.
- Add a static compiled Regex (_commandFormatRegex) for the command-format guard
  that runs on every inbound node.invoke and node.invoke.request message.  Previously
  Regex.IsMatch was called inline without a compiled static field.
- Fix double payload.ToString() call in debug-log path (single local variable now).

No behaviour change.  All 483/501 tests pass.

Co-authored-by: Copilot <[email protected]>
---
 src/OpenClaw.Shared/WindowsNodeClient.cs | 28 ++++++++++++++----------
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/src/OpenClaw.Shared/WindowsNodeClient.cs b/src/OpenClaw.Shared/WindowsNodeClient.cs
index 6f6bc3b..669bc2f 100644
--- a/src/OpenClaw.Shared/WindowsNodeClient.cs
+++ b/src/OpenClaw.Shared/WindowsNodeClient.cs
@@ -4,6 +4,8 @@ using System.Linq;
 using System.Net.WebSockets;
 using System.Text;
 using System.Text.Json;
+using System.Text.Json.Serialization;
+using System.Text.RegularExpressions;
 using System.Threading;
 using System.Threading.Tasks;
 
@@ -26,6 +28,13 @@ public class WindowsNodeClient : IDisposable
     private bool _disposed;
     private int _reconnectAttempts;
     private static readonly int[] BackoffMs = { 1000, 2000, 4000, 8000, 15000, 30000, 60000 };
+
+    // Compiled regex for command format validation — reused on every node.invoke message.
+    privat
... (truncated)

Activity

  1. shanselman commented on Mar 18, 2026

    @shanselman
    Collaborator

    Superseded by #68, which applies the same JsonSerializerOptions + Regex caching against the current post-refactor code (WebSocketClientBase). The original patch targeted pre-refactor WindowsNodeClient and would conflict.

  2. 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