Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revise heuristic for initial jitting. #1949

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions src/BenchmarkDotNet/Engines/EngineFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,23 @@ public IEngine CreateReadyToRun(EngineParameters engineParameters)

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

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

if (singleInvocationTime > engineParameters.IterationTime)
return singleActionEngine; // executing once takes longer than iteration time => long running benchmark, needs no pilot and no overhead

int defaultUnrollFactor = Job.Default.ResolveValue(RunMode.UnrollFactorCharacteristic, EngineParameters.DefaultResolver);

double timesPerIteration = engineParameters.IterationTime / singleInvocationTime; // how many times can we run given benchmark per iteration

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
// executing once takes longer than iteration time => long running benchmark, needs no pilot and no overhead
// Or executing twice would put us well past the iteration time ==> needs no pilot and no overhead
if (timesPerIteration < 1.5)
return singleActionEngine;

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

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