Skip to content

Commit 3518d50

Browse files
committed
Sleep thread to account for tiered jit in older Core runtimes.
1 parent 02f1381 commit 3518d50

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/BenchmarkDotNet/Engines/Engine.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Globalization;
44
using System.Linq;
55
using System.Runtime.CompilerServices;
6+
using System.Threading;
67
using BenchmarkDotNet.Characteristics;
78
using BenchmarkDotNet.Jobs;
89
using BenchmarkDotNet.Portability;
@@ -214,15 +215,24 @@ private ClockSpan Measure(Action<long> action, long invokeCount)
214215

215216
private (GcStats, ThreadingStats, double) GetExtraStats(IterationData data)
216217
{
217-
// Warm up the GetAllocatedBytes function before starting the actual measurement.
218+
// Warm up the measurement functions before starting the actual measurement.
218219
DeadCodeEliminationHelper.KeepAliveWithoutBoxing(GcStats.ReadInitial());
220+
DeadCodeEliminationHelper.KeepAliveWithoutBoxing(GcStats.ReadFinal());
219221

220222
IterationSetupAction(); // we run iteration setup first, so even if it allocates, it is not included in the results
221223

222224
var initialThreadingStats = ThreadingStats.ReadInitial(); // this method might allocate
223225
var exceptionsStats = new ExceptionsStats(); // allocates
224226
exceptionsStats.StartListening(); // this method might allocate
225227

228+
if (RuntimeInformation.IsNetCore && Environment.Version.Major < 7)
229+
{
230+
// We put the current thread to sleep so tiered jit can kick in, compile it's stuff
231+
// and NOT allocate anything on the background thread when we are measuring allocations.
232+
// This is only an issue on net6 and older.
233+
Thread.Sleep(TimeSpan.FromMilliseconds(500));
234+
}
235+
226236
// GC collect before measuring allocations, as we do not collect during the measurement.
227237
ForceGcCollect();
228238
var gcStats = MeasureWithGc(data.InvokeCount / data.UnrollFactor);

0 commit comments

Comments
 (0)