Skip to content

[Repo Assist] Cache JsonSerializerOptions and extract colour Lerp helperΒ #39

Description

@github-actions

πŸ€– This is an automated PR from Repo Assist.

Summary

Two small but clean performance / code-quality improvements found while looking for allocation hot-spots.

1 β€” Cache JsonSerializerOptions in AppSettings (avoids repeated construction)

JsonSerializerOptions is explicitly flagged in the .NET docs as expensive to construct because it builds internal converter caches on first use. Previously Load() and Save() each allocated a fresh instance on every call:

// Before β€” new object every Save() / Load()
var json = JsonSerializer.Serialize(this, new JsonSerializerOptions { WriteIndented = true });

The two option objects are now static readonly fields, so construction happens exactly once per process lifetime.

2 β€” Extract static LerpColor helper and colour constants

SetColorTemperature and UpdateAdditionalMonitorWindows both defined an identical local Lerp function and the same cool / warm Color literals inline. That means:

  • The same magic colour values (R220 G235 B255 / R255 G220 B180) existed in two places β€” a maintenance hazard.
  • Each call site defined a new local-function object (minor, but avoidable).

They are now a single static helper and two static readonly Color fields.

Backwards Compatibility

No behaviour changes. All logic is identical; only the allocation and definition sites have moved.

Test Status

Built with dotnet build /p:EnableWindowsTargeting=true β€” 0 errors, 0 warnings.
Full functional testing requires Windows; no automated test suite exists for this WPF app.

Note

πŸ”’ Integrity filter blocked 2 items

The following items were blocked because they don't meet the GitHub integrity level.

To allow these resources, lower min-integrity in your GitHub frontmatter:

tools:
  github:
    min-integrity: approved  # merged | approved | unapproved | none

Generated by Repo Assist Β· ● 2.3M Β· β—·

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-static-json-options-color-lerp-2026-06-10-72c8512de0f82bcf.

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. See also: gh-aw FAQ

Show patch preview (139 of 139 lines)
From 9c754075c251a4c8ef7d4bcbf399eff947db38d0 Mon Sep 17 00:00:00 2001
From: "github-actions[bot]" <github-actions[bot]@users.noreply.github.com>
Date: Wed, 10 Jun 2026 01:36:14 +0000
Subject: [PATCH] Cache JsonSerializerOptions and extract color Lerp helper

JsonSerializerOptions are expensive to construct (they hold internal
converter caches). Previously AppSettings.Load() and AppSettings.Save()
each created a fresh instance on every call. Promoting them to static
readonly fields means the objects are built once and reused.

The Lerp colour helper and the CoolColor/WarmColor constants were
duplicated in two methods (SetColorTemperature and
UpdateAdditionalMonitorWindows). Extract them to static members so
the values are defined in exactly one place, reducing maintenance
risk and eliminating the repeated local-function definitions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---
 WindowsEdgeLight/AppSettings.cs     | 23 +++++++++--------
 WindowsEdgeLight/MainWindow.xaml.cs | 40 ++++++++++-------------------
 2 files changed, 26 insertions(+), 37 deletions(-)

diff --git a/WindowsEdgeLight/AppSettings.cs b/WindowsEdgeLight/AppSettings.cs
index bf12831..3c9a582 100644
--- a/WindowsEdgeLight/AppSettings.cs
+++ b/WindowsEdgeLight/AppSettings.cs
@@ -20,6 +20,17 @@ public class AppSettings
     /// </summary>
     public bool ExcludeFromCapture { get; set; } = true;
 
+    private static readonly JsonSerializerOptions ReadOptions = new JsonSerializerOptions
+    {
+        AllowTrailingCommas = true,
+        ReadCommentHandling = JsonCommentHandling.Skip
+    };
+
+    private static readonly JsonSerializerOptions WriteOptions = new JsonSerializerOptions
+    {
+        WriteIndented = true
+    };
+
     /// <summary>
     /// Load settings from disk
     /// </summary>
@@ -30,12 +41,7 @@ public class AppSettings
             if (File.Exists(SettingsFilePath))
             {
                 var json = File.ReadAllText(SettingsFilePath);
-   
... (truncated)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions