From 5020a6cf9585d7066553146920253cf856b7384a Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 12 Jul 2026 17:21:57 +0000 Subject: [PATCH] Refactor LINQ .ToDictionary() into Dictionary copy constructor for Context copies Replaces LINQ's `.ToDictionary(pair => pair.Key, pair => pair.Value, Comparer)` with the native `Dictionary` copy constructor `new Dictionary(source, Comparer)`. This removes enumerable closures and delegate overhead in hot paths during `TemporalNetworkSimulationEngine.cs`. --- .jules/bolt.md | 3 +++ .../Services/TemporalNetworkSimulationEngine.cs | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index ad98065..31021c5 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -88,3 +88,6 @@ ## 2026-06-15 - Optimize Multiple ToDictionary Allocations inside Workspace UI **Learning:** In C#, executing multiple LINQ `.ToDictionary()` allocations on the same source collection (like building 9 different actor metrics dictionaries from `SimulationActors`) inside UI metric generation code allocates massive amounts of redundant enumerators, delegates, and intermediate dictionary structures on the UI thread, causing unnecessary memory allocation and garbage collection pauses. **Action:** Replace multiple `.ToDictionary()` allocations on identical source collections with a single manual `foreach` loop that populates pre-allocated dictionaries simultaneously. This transforms an O(K*N) allocation into a tight O(N) loop and completely avoids LINQ overhead. +## 2024-05-31 - [Optimize ToDictionary with manual Dictionary copy constructor] +**Learning:** In C#, replacing `context.Supply.ToDictionary(pair => pair.Key, pair => pair.Value, Comparer)` with the native dictionary copy constructor `new Dictionary(context.Supply, Comparer)` avoids multiple LINQ enumerator allocations and delegate creations for the hot `ToRoutingContext` path inside `TemporalNetworkSimulationEngine`. +**Action:** Replace direct `.ToDictionary` invocations with `new Dictionary(source, Comparer)` when copying dictionaries where the source implements `IDictionary`. diff --git a/src/MedWNetworkSim.App/Services/TemporalNetworkSimulationEngine.cs b/src/MedWNetworkSim.App/Services/TemporalNetworkSimulationEngine.cs index fb2e2ad..5ea9999 100644 --- a/src/MedWNetworkSim.App/Services/TemporalNetworkSimulationEngine.cs +++ b/src/MedWNetworkSim.App/Services/TemporalNetworkSimulationEngine.cs @@ -1072,9 +1072,9 @@ private static RoutingTrafficContext ToRoutingContext(TemporalTrafficContext con Seed = context.Seed, NodesById = context.NodesById, ProfilesByNodeId = context.ProfilesByNodeId, - Supply = context.Supply.ToDictionary(pair => pair.Key, pair => pair.Value, Comparer), - SupplyUnitCosts = context.SupplyUnitCosts.ToDictionary(pair => pair.Key, pair => pair.Value, Comparer), - Demand = context.Demand.ToDictionary(pair => pair.Key, pair => pair.Value, Comparer), + Supply = new Dictionary(context.Supply, Comparer), + SupplyUnitCosts = new Dictionary(context.SupplyUnitCosts, Comparer), + Demand = new Dictionary(context.Demand, Comparer), MeetingDemandEligibleNodeIds = context.MeetingDemandEligibleNodeIds }; }