Skip to content

Commit 937311c

Browse files
authored
Revise heuristic for initial jitting. (#1949)
Increase the magic limit to 10s per #1780. Also use the "1.5" criteria for retry so we don't have odd threshold where initial jitting is a bit faster than IterationTime but not fast enough to trigger the multi action engine.
1 parent 9b990df commit 937311c

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

src/BenchmarkDotNet/Engines/EngineFactory.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,23 @@ public IEngine CreateReadyToRun(EngineParameters engineParameters)
4343

4444
var singleActionEngine = CreateSingleActionEngine(engineParameters);
4545
var singleInvocationTime = Jit(singleActionEngine, ++jitIndex, invokeCount: 1, unrollFactor: 1);
46+
double timesPerIteration = engineParameters.IterationTime / singleInvocationTime; // how many times can we run given benchmark per iteration
4647

47-
if (singleInvocationTime > engineParameters.IterationTime && singleInvocationTime < TimeInterval.FromSeconds(1.0))
48+
if ((timesPerIteration < 1.5) && (singleInvocationTime < TimeInterval.FromSeconds(10.0)))
4849
{
49-
// if the Jitting took more than IterationTime but still less than 1s (a magic number based on observations of the reported bug)
50+
// if the Jitting took more than IterationTime/1.5 but still less than 10s (a magic number based on observations of reported bugs)
5051
// we call it one more time to see if Jitting itself has not dominated the first invocation
51-
// if it did, it shoud NOT be a single invocation engine (see #837, #1337 and #1338)
52+
// if it did, it should NOT be a single invocation engine (see #837, #1337, #1338, and #1780)
5253
singleInvocationTime = Jit(singleActionEngine, ++jitIndex, invokeCount: 1, unrollFactor: 1);
54+
timesPerIteration = engineParameters.IterationTime / singleInvocationTime;
5355
}
5456

55-
if (singleInvocationTime > engineParameters.IterationTime)
56-
return singleActionEngine; // executing once takes longer than iteration time => long running benchmark, needs no pilot and no overhead
57-
58-
int defaultUnrollFactor = Job.Default.ResolveValue(RunMode.UnrollFactorCharacteristic, EngineParameters.DefaultResolver);
59-
60-
double timesPerIteration = engineParameters.IterationTime / singleInvocationTime; // how many times can we run given benchmark per iteration
61-
62-
if (timesPerIteration < 1.5) // example: IterationTime is 0.5s, but single invocation takes 0.4s => we don't want to run it twice per iteration
57+
// executing once takes longer than iteration time => long running benchmark, needs no pilot and no overhead
58+
// Or executing twice would put us well past the iteration time ==> needs no pilot and no overhead
59+
if (timesPerIteration < 1.5)
6360
return singleActionEngine;
6461

62+
int defaultUnrollFactor = Job.Default.ResolveValue(RunMode.UnrollFactorCharacteristic, EngineParameters.DefaultResolver);
6563
int roundedUpTimesPerIteration = (int)Math.Ceiling(timesPerIteration);
6664

6765
if (roundedUpTimesPerIteration < defaultUnrollFactor) // if we run it defaultUnrollFactor times per iteration, it's going to take longer than IterationTime
@@ -129,4 +127,4 @@ private static Engine CreateEngine(EngineParameters engineParameters, Job job, A
129127
engineParameters.MeasureExtraStats,
130128
engineParameters.BenchmarkName);
131129
}
132-
}
130+
}

0 commit comments

Comments
 (0)