Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/MedWNetworkSim.App/Services/NetworkSimulationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1081,9 +1081,39 @@ private static List<RouteCandidate> BuildCandidateRoutes(
{
var routes = new List<RouteCandidate>();

foreach (var producerNodeId in context.Supply.Where(pair => pair.Value > Epsilon).Select(pair => pair.Key))
// Cache active producers and consumers to avoid repeated LINQ enumerations and allocations,
// especially since the consumer loop is nested inside the producer loop.
var activeProducers = new List<string>();
foreach (var pair in context.Supply)
{
if (pair.Value > Epsilon)
{
activeProducers.Add(pair.Key);
}
}

if (activeProducers.Count == 0)
{
return routes;
}

var activeConsumers = new List<string>();
foreach (var pair in context.Demand)
{
if (pair.Value > Epsilon)
{
activeConsumers.Add(pair.Key);
}
}

if (activeConsumers.Count == 0)
{
return routes;
}

foreach (var producerNodeId in activeProducers)
{
foreach (var consumerNodeId in context.Demand.Where(pair => pair.Value > Epsilon).Select(pair => pair.Key))
foreach (var consumerNodeId in activeConsumers)
{
if (Comparer.Equals(producerNodeId, consumerNodeId))
{
Expand Down