Merge pull request #1428 from PepperDash/tieline-mapping

Fix cache clearing for impossibleRoutes
This commit is contained in:
Neil Dorin 2026-06-09 09:25:42 -06:00 committed by GitHub
commit 75587c361f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,5 +1,4 @@
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
@ -52,13 +51,6 @@ namespace PepperDash.Essentials.Core
/// </summary> /// </summary>
private static Dictionary<string, List<TieLine>> _tieLinesBySource; private static Dictionary<string, List<TieLine>> _tieLinesBySource;
/// <summary>
/// Cache of failed route attempts to avoid re-checking impossible paths.
/// Format: "sourceKey|destKey|signalType"
/// Uses ConcurrentDictionary as a thread-safe set (byte value is unused).
/// </summary>
private static readonly ConcurrentDictionary<string, byte> _impossibleRoutes = new ConcurrentDictionary<string, byte>();
/// <summary> /// <summary>
/// Indexes all TieLines by source and destination device keys for faster lookups. /// Indexes all TieLines by source and destination device keys for faster lookups.
/// Should be called once at system startup after all TieLines are created. /// Should be called once at system startup after all TieLines are created.
@ -121,29 +113,6 @@ namespace PepperDash.Essentials.Core
return TieLineCollection.Default.Where(t => t.SourcePort.ParentDevice.Key == sourceKey); return TieLineCollection.Default.Where(t => t.SourcePort.ParentDevice.Key == sourceKey);
} }
/// <summary>
/// Creates a cache key for route impossibility tracking.
/// </summary>
/// <param name="sourceKey">Source device key</param>
/// <param name="destKey">Destination device key</param>
/// <param name="sourcePortKey">Source port key</param>
/// <param name="destinationPortKey">Destination port key</param>
/// <param name="type">Signal type</param>
/// <returns>Cache key string</returns>
private static string GetRouteKey(string sourceKey, string destKey, string sourcePortKey, string destinationPortKey, eRoutingSignalType type)
{
return $"{sourceKey}|{destKey}|{sourcePortKey}|{destinationPortKey}|{type}";
}
/// <summary>
/// Clears the impossible routes cache. Should be called if TieLines are added/removed at runtime.
/// </summary>
public static void ClearImpossibleRoutesCache()
{
_impossibleRoutes.Clear();
Debug.LogInformation("Impossible routes cache cleared");
}
/// <summary> /// <summary>
/// Gets any existing RouteDescriptor for a destination, clears it using ReleaseRoute /// Gets any existing RouteDescriptor for a destination, clears it using ReleaseRoute
/// and then attempts a new Route and if sucessful, stores that RouteDescriptor /// and then attempts a new Route and if sucessful, stores that RouteDescriptor
@ -588,14 +557,6 @@ namespace PepperDash.Essentials.Core
{ {
cycle++; cycle++;
// Check if this route has already been determined to be impossible
var routeKey = GetRouteKey(source.Key, destination.Key, sourcePort?.Key ?? "auto", destinationPort?.Key ?? "auto", signalType);
if (_impossibleRoutes.ContainsKey(routeKey))
{
Debug.LogVerbose("Route {0} is cached as impossible, skipping", routeKey);
return false;
}
Debug.LogVerbose("GetRouteToSource: {cycle} {sourceKey}:{sourcePortKey}--> {destinationKey}:{destinationPortKey} {type}", null, cycle, source.Key, sourcePort?.Key ?? "auto", destination.Key, destinationPort?.Key ?? "auto", signalType.ToString()); Debug.LogVerbose("GetRouteToSource: {cycle} {sourceKey}:{sourcePortKey}--> {destinationKey}:{destinationPortKey} {type}", null, cycle, source.Key, sourcePort?.Key ?? "auto", destination.Key, destinationPort?.Key ?? "auto", signalType.ToString());
RoutingInputPort goodInputPort = null; RoutingInputPort goodInputPort = null;
@ -693,9 +654,6 @@ namespace PepperDash.Essentials.Core
{ {
Debug.LogVerbose(destination, "No route found to {0} from destination {1} for type {2}", source.Key, destination.Key, signalType); Debug.LogVerbose(destination, "No route found to {0} from destination {1} for type {2}", source.Key, destination.Key, signalType);
// Cache this as an impossible route
_impossibleRoutes.TryAdd(routeKey, 0);
return false; return false;
} }