diff --git a/src/Microsoft.TestPlatform.CrossPlatEngine/Discovery/DiscoveryResultCache.cs b/src/Microsoft.TestPlatform.CrossPlatEngine/Discovery/DiscoveryResultCache.cs index 63de79957a..9b9e8002ce 100644 --- a/src/Microsoft.TestPlatform.CrossPlatEngine/Discovery/DiscoveryResultCache.cs +++ b/src/Microsoft.TestPlatform.CrossPlatEngine/Discovery/DiscoveryResultCache.cs @@ -60,7 +60,7 @@ public DiscoveryResultCache(long cacheSize, TimeSpan discoveredTestEventTimeout, _lastUpdate = DateTime.UtcNow; _cacheTimeout = discoveredTestEventTimeout; - _tests = new List(); + _tests = new List(InitialCapacity(cacheSize)); TotalDiscoveredTests = 0; } @@ -112,11 +112,13 @@ public void AddTest(TestCase test) { // Pass on the buffer to the listener and clear the old one _onReportTestCases(_tests); - _tests = new List(); + _tests = new List(InitialCapacity(_cacheSize)); _lastUpdate = DateTime.UtcNow; EqtTrace.Verbose("DiscoveryResultCache.AddTest: Notified the onReportTestCases callback."); } } } + + private static int InitialCapacity(long cacheSize) => (int)Math.Min(cacheSize, 512); } diff --git a/src/Microsoft.TestPlatform.CrossPlatEngine/Execution/TestRunCache.cs b/src/Microsoft.TestPlatform.CrossPlatEngine/Execution/TestRunCache.cs index bf680a873a..ba177ef0f7 100644 --- a/src/Microsoft.TestPlatform.CrossPlatEngine/Execution/TestRunCache.cs +++ b/src/Microsoft.TestPlatform.CrossPlatEngine/Execution/TestRunCache.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; -using System.Collections.ObjectModel; using System.Linq; using System.Threading; @@ -60,12 +59,12 @@ internal class TestRunCache : ITestRunCache /// /// The test case currently in progress. /// - private ICollection _inProgressTests; + private List _inProgressTests; /// /// Test results buffer /// - private ICollection _testResults; + private List _testResults; /// /// Sync object @@ -93,8 +92,8 @@ internal TestRunCache(long cacheSize, TimeSpan cacheTimeout, OnCacheHit onCacheH _onCacheHit = onCacheHit; _lastUpdate = DateTime.UtcNow; _cacheTimeout = cacheTimeout; - _inProgressTests = new Collection(); - _testResults = new Collection(); + _inProgressTests = new List(InitialCapacity(cacheSize)); + _testResults = new List(InitialCapacity(cacheSize)); _runStats = new Dictionary(); _syncObject = new object(); @@ -272,7 +271,10 @@ public ICollection GetLastChunk() { var lastChunk = _testResults; - _testResults = new Collection(); + // GetLastChunk() is the end-of-run drain; no further results are expected after this + // call, so avoid pre-allocating capacity here. The replacement list exists only to keep + // the field non-null and will be allocated lazily if results somehow arrive afterwards. + _testResults = new List(); return lastChunk; } @@ -337,8 +339,8 @@ private void SendResults() { // Pass on the buffer to the listener and clear the old one _onCacheHit(TestRunStatistics, _testResults, _inProgressTests); - _testResults = new Collection(); - _inProgressTests = new Collection(); + _testResults = new List(InitialCapacity(_cacheSize)); + _inProgressTests = new List(InitialCapacity(_cacheSize)); _lastUpdate = DateTime.UtcNow; // Reset the timer @@ -371,4 +373,6 @@ private void RemoveInProgress(TestResult result) } } + private static int InitialCapacity(long cacheSize) => (int)Math.Min(cacheSize, 512); + }