@@ -64,7 +64,7 @@ public class Signals
64
64
public int noSignals ;
65
65
private int foundSignals ;
66
66
67
- private static int updatecount ;
67
+ private static int UpdateIndex ;
68
68
69
69
public List < TrackCircuitSection > TrackCircuitList ;
70
70
private Dictionary < int , CrossOverItem > CrossoverList = new Dictionary < int , CrossOverItem > ( ) ;
@@ -553,6 +553,11 @@ private void BuildSignalWorld(Simulator simulator, SignalConfigurationFile sigcf
553
553
554
554
} //BuildSignalWorld
555
555
556
+ Stopwatch UpdateTimer = new Stopwatch ( ) ;
557
+ long UpdateCounter = 0 ;
558
+ long UpdateTickTarget = 10000 ;
559
+ // long DebugUpdateCounter = 0;
560
+
556
561
/// <summary>
557
562
/// Update : perform signal updates
558
563
/// </summary>
@@ -562,30 +567,39 @@ public void Update(bool preUpdate)
562
567
563
568
if ( foundSignals > 0 )
564
569
{
565
-
566
570
// loop through all signals
567
571
// update required part
568
- // in preupdate, process all
569
-
570
- int totalSignal = foundSignals ;
571
-
572
- int updatestep = ( totalSignal / 20 ) + 1 ;
573
- if ( preUpdate )
574
- {
575
- updatestep = totalSignal ;
576
- }
577
-
578
- for ( int icount = updatecount ; icount < Math . Min ( totalSignal , updatecount + updatestep ) ; icount ++ )
572
+ var updates = 0 ;
573
+ var updateStep = 0 ;
574
+ var targetTicks = Stopwatch . GetTimestamp ( ) + UpdateTickTarget ;
575
+ UpdateTimer . Start ( ) ;
576
+ while ( updateStep < foundSignals )
579
577
{
580
- SignalObject signal = SignalObjects [ icount ] ;
578
+ var signal = SignalObjects [ ( UpdateIndex + updateStep ) % foundSignals ] ;
581
579
if ( signal != null && ! signal . noupdate ) // to cater for orphans, and skip signals which do not require updates
582
580
{
583
581
signal . Update ( ) ;
582
+ updates ++ ;
584
583
}
584
+ updateStep ++ ;
585
+
586
+ // in preupdate, process all
587
+ if ( ! preUpdate && updates % 10 == 0 && Stopwatch . GetTimestamp ( ) >= targetTicks ) break ;
585
588
}
589
+ UpdateCounter += updates ;
590
+ UpdateTimer . Stop ( ) ;
586
591
587
- updatecount += updatestep ;
588
- updatecount = updatecount >= totalSignal ? 0 : updatecount ;
592
+ if ( UpdateIndex + updateStep >= foundSignals )
593
+ {
594
+ // Calculate how long it takes to update all signals and target 1/20th of that
595
+ // Slow adjustment using clamp stops it jumping around too much
596
+ var ticksPerSignal = ( double ) UpdateTimer . ElapsedTicks / UpdateCounter ;
597
+ UpdateTickTarget = ( long ) MathHelper . Clamp ( ( float ) ( ticksPerSignal * foundSignals / 20 ) , UpdateTickTarget - 100 , UpdateTickTarget + 100 ) ;
598
+ // if (++DebugUpdateCounter % 10 == 0) Trace.WriteLine($"Signal update for {UpdateCounter,5} signals took {(double)UpdateTimer.ElapsedTicks * 1000 / Stopwatch.Frequency,9:F6} ms ({ticksPerSignal * 1000 / Stopwatch.Frequency,9:F6} ms/signal); new {(double)UpdateTickTarget * 1000 / Stopwatch.Frequency,6:F6} ms target");
599
+ UpdateTimer . Reset ( ) ;
600
+ UpdateCounter = 0 ;
601
+ }
602
+ UpdateIndex = ( UpdateIndex + updateStep ) % foundSignals ;
589
603
}
590
604
}
591
605
0 commit comments