From 4f304855e0ec56a5077ca4a2ec89650ce47c1d4a Mon Sep 17 00:00:00 2001 From: David Johnson Date: Fri, 7 Aug 2026 20:16:23 +0100 Subject: [PATCH] perf: use dictionary index for community status satellite lookups TryGetMode and TryGetSatellite previously iterated the Satellites list linearly on every call (O(n)). These are called from the UI rendering path for each enabled satellite's status indicator. Build a lazily-initialized Dictionary keyed by name (case-insensitive) on first access. Subsequent lookups are O(1). The index is built once per API fetch (every 5 minutes) and reused for all rendering ticks in between. --- .../Services/SatelliteCommunityStatus.cs | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/OscarWatch.Core/Services/SatelliteCommunityStatus.cs b/OscarWatch.Core/Services/SatelliteCommunityStatus.cs index 7e2fcbe..d83d2df 100644 --- a/OscarWatch.Core/Services/SatelliteCommunityStatus.cs +++ b/OscarWatch.Core/Services/SatelliteCommunityStatus.cs @@ -33,21 +33,24 @@ public sealed record SatelliteCommunityCatalog( DateTime ServerTimeUtc, DateTime FetchedAtUtc) { + // O(1) lookup index built lazily on first access. + private Dictionary? _index; + + private Dictionary Index => + _index ??= Satellites.ToDictionary(s => s.Name, StringComparer.OrdinalIgnoreCase); + public SatelliteCommunityModeStatus? TryGetMode(string satelliteName, string modeType) { if (string.IsNullOrWhiteSpace(satelliteName) || string.IsNullOrWhiteSpace(modeType)) return null; - foreach (var sat in Satellites) - { - if (!string.Equals(sat.Name, satelliteName.Trim(), StringComparison.OrdinalIgnoreCase)) - continue; + if (!Index.TryGetValue(satelliteName.Trim(), out var sat)) + return null; - foreach (var mode in sat.Modes) - { - if (string.Equals(mode.ModeType, modeType.Trim(), StringComparison.OrdinalIgnoreCase)) - return mode; - } + foreach (var mode in sat.Modes) + { + if (string.Equals(mode.ModeType, modeType.Trim(), StringComparison.OrdinalIgnoreCase)) + return mode; } return null; @@ -58,13 +61,7 @@ public sealed record SatelliteCommunityCatalog( if (string.IsNullOrWhiteSpace(satelliteName)) return null; - foreach (var sat in Satellites) - { - if (string.Equals(sat.Name, satelliteName.Trim(), StringComparison.OrdinalIgnoreCase)) - return sat; - } - - return null; + return Index.GetValueOrDefault(satelliteName.Trim()); } }