Skip to content

Commit

Permalink
⚡️ Some benchmark performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
skerit committed Mar 23, 2024
1 parent d97f704 commit f49ae3d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 18 deletions.
56 changes: 40 additions & 16 deletions lib/benchmark.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
var performanceNow,
let callGarbageCollection,
performanceNow,
performance,
nowOverhead,
polystart,
hrtime;
hrtime,
v8,
vm,
gc;

if (Blast.isNode) {
v8 = require('v8');
vm = require('vm');
v8.setFlagsFromString('--expose_gc');
gc = vm.runInNewContext('gc');
callGarbageCollection = () => {
if (gc) gc();
}
} else {
callGarbageCollection = () => {};
}

// Get the performance object, if it exists
performance = Blast.Globals.performance || {};
Expand Down Expand Up @@ -81,6 +97,8 @@ Blast.benchmark_async_sizes = [120, 120, 120, 240, 240, 480];
*/
Blast.defineStatic('Function', 'benchmark', function benchmark(fn, acb) {

callGarbageCollection();

if (fn.length == 0) {
return doSyncBench(fn, acb);
}
Expand All @@ -94,7 +112,7 @@ Blast.defineStatic('Function', 'benchmark', function benchmark(fn, acb) {
*
* @author Jelle De Loecker <[email protected]>
* @since 0.1.2
* @version 0.5.4
* @version 0.9.3
*
* @param {number} runs
*/
Expand Down Expand Up @@ -125,6 +143,8 @@ function getFunctionOverhead(runs) {
result = 0.5;
}

callGarbageCollection();

return result;
}

Expand All @@ -133,7 +153,7 @@ function getFunctionOverhead(runs) {
*
* @author Jelle De Loecker <[email protected]>
* @since 0.1.2
* @version 0.5.9
* @version 0.9.3
*
* @param {Function} fn
* @param {number} runs Expected runs per 50ms
Expand Down Expand Up @@ -177,6 +197,8 @@ function syncTest(fn, runs, iterOverhead) {
// Get the elapsed time
elapsed = Blast.performanceNow() - start;

callGarbageCollection();

// Remove the function call overhead
elapsed = elapsed - (shard * (iterOverhead/runs));

Expand Down Expand Up @@ -244,11 +266,13 @@ function asyncTest(fn, testruns, asyncOverhead, callback) {
// Calculate how many times to do the test
shard = 1 + ~~((testruns/300)*sizes[r]);

callGarbageCollection();

Fn.doAmount(shard, fn, function done(err, runs, elapsed) {
elapsed = elapsed - (shard * (asyncOverhead/testruns));
samples.push(~~((shard/elapsed)*1000));
total += shard;

callGarbageCollection();
next();
});

Expand Down Expand Up @@ -356,22 +380,14 @@ var benchQueue = [],
*/
function doAsyncBench(fn, callback) {

var fnOverhead,
pretotal,
result,
args,
i;

if (benchRunning > 0) {

// Keep function optimized by not leaking the `arguments` object
args = new Array(arguments.length);
for (i = 0; i < args.length; i++) args[i] = arguments[i];

let args = [...arguments];
benchQueue.push(args);
return;
}

let pledge = new Classes.Pledge.Swift();

benchRunning++;

// See how many times we can get it to run for 300ms
Expand All @@ -384,6 +400,12 @@ function doAsyncBench(fn, callback) {
var next,
name;

if (err) {
pledge.reject(err);
} else {
pledge.resolve(result);
}

// Call the callback
if (callback) {
result.name = fn.name || '';
Expand All @@ -410,6 +432,8 @@ function doAsyncBench(fn, callback) {
});
});
});

return pledge;
}

var baselineMinimum = Infinity,
Expand Down
4 changes: 2 additions & 2 deletions lib/function_flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ defStat(function doUnitTime(amount, ms, task, callback) {
// this function should run, keep on running it
if (time < max) {

if (recursion < 3000) {
if (recursion < 1000) {

// Infinite recursion is bad, but a little can't hurt
task(handler);
Expand All @@ -1148,7 +1148,7 @@ defStat(function doUnitTime(amount, ms, task, callback) {
}

} else {
// We need to reset the recusrion
// We need to reset the recursion
recursion = 0;
trap++;

Expand Down

0 comments on commit f49ae3d

Please sign in to comment.