Improve perf for worker

This commit is contained in:
2025-11-11 04:09:45 +07:00
parent 1d70355617
commit c6becb032b
2 changed files with 701 additions and 4 deletions

View File

@@ -227,6 +227,7 @@ public class BacktestExecutor
// Use optimized rolling window approach - TradingBox.GetSignal only needs last 600 candles
const int rollingWindowSize = 600;
var rollingCandles = new List<Candle>(rollingWindowSize); // Pre-allocate capacity for better performance
var fixedCandlesHashSet = new HashSet<Candle>(rollingWindowSize); // Reuse HashSet to avoid allocations
var candlesProcessed = 0;
// Signal caching optimization - reduce signal update frequency for better performance
@@ -259,10 +260,14 @@ public class BacktestExecutor
if (rollingCandles.Count > rollingWindowSize)
{
// Remove oldest candle (first element) - O(n) but acceptable for small window
// Remove oldest candle from both structures
var removedCandle = rollingCandles[0];
rollingCandles.RemoveAt(0);
fixedCandlesHashSet.Remove(removedCandle);
}
// Add to HashSet for reuse
fixedCandlesHashSet.Add(candle);
tradingBot.LastCandle = candle;
// Smart signal caching - reduce signal update frequency for performance
@@ -276,10 +281,9 @@ public class BacktestExecutor
if (!shouldSkipSignalUpdate)
{
// Convert to HashSet only when needed for GetSignal (it expects HashSet)
var fixedCandles = new HashSet<Candle>(rollingCandles);
// Reuse the pre-allocated HashSet instead of creating new one
var signalUpdateStart = Stopwatch.GetTimestamp();
await tradingBot.UpdateSignals(fixedCandles);
await tradingBot.UpdateSignals(fixedCandlesHashSet);
signalUpdateTotalTime += Stopwatch.GetElapsedTime(signalUpdateStart);
telemetry.TotalSignalUpdates++;
}