Skip to content

Commit

Permalink
timers: clean up for readability
Browse files Browse the repository at this point in the history
Remove micro-optimizations that no longer yield any benefits,
restructure timers & immediates to be a bit more straightforward.

Adjust timers benchmarks to run long enough to offer meaningful data.

PR-URL: #17279
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
  • Loading branch information
apapirovski authored and MylesBorins committed Dec 12, 2017
1 parent 0db1f87 commit bd79c37
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 174 deletions.
4 changes: 3 additions & 1 deletion benchmark/timers/immediate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const common = require('../common.js');

const bench = common.createBenchmark(main, {
thousands: [2000],
thousands: [5000],
type: ['depth', 'depth1', 'breadth', 'breadth1', 'breadth4', 'clear']
});

Expand Down Expand Up @@ -88,6 +88,7 @@ function breadth1(N) {

// concurrent setImmediate, 4 arguments
function breadth4(N) {
N /= 2;
var n = 0;
bench.start();
function cb(a1, a2, a3, a4) {
Expand All @@ -101,6 +102,7 @@ function breadth4(N) {
}

function clear(N) {
N *= 4;
bench.start();
function cb(a1) {
if (a1 === 2)
Expand Down
4 changes: 2 additions & 2 deletions benchmark/timers/set-immediate-breadth-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ function main(conf) {
bench.start();
for (let i = 0; i < N; i++) {
if (i % 3 === 0)
setImmediate(cb3, 512, true, null);
setImmediate(cb3, 512, true, null, 512, true, null);
else if (i % 2 === 0)
setImmediate(cb2, false, 5.1);
setImmediate(cb2, false, 5.1, 512);
else
setImmediate(cb1, 0);
}
Expand Down
14 changes: 7 additions & 7 deletions benchmark/timers/set-immediate-depth-args.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const common = require('../common.js');
const bench = common.createBenchmark(main, {
millions: [10]
millions: [5]
});

function main(conf) {
Expand All @@ -15,29 +15,29 @@ function main(conf) {
function cb3(n, arg2, arg3) {
if (--n) {
if (n % 3 === 0)
setImmediate(cb3, n, true, null);
setImmediate(cb3, n, true, null, 5.1, null, true);
else if (n % 2 === 0)
setImmediate(cb2, n, 5.1);
setImmediate(cb2, n, 5.1, true);
else
setImmediate(cb1, n);
}
}
function cb2(n, arg2) {
if (--n) {
if (n % 3 === 0)
setImmediate(cb3, n, true, null);
setImmediate(cb3, n, true, null, 5.1, null, true);
else if (n % 2 === 0)
setImmediate(cb2, n, 5.1);
setImmediate(cb2, n, 5.1, true);
else
setImmediate(cb1, n);
}
}
function cb1(n) {
if (--n) {
if (n % 3 === 0)
setImmediate(cb3, n, true, null);
setImmediate(cb3, n, true, null, 5.1, null, true);
else if (n % 2 === 0)
setImmediate(cb2, n, 5.1);
setImmediate(cb2, n, 5.1, true);
else
setImmediate(cb1, n);
}
Expand Down
2 changes: 1 addition & 1 deletion benchmark/timers/timers-breadth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const common = require('../common.js');

const bench = common.createBenchmark(main, {
thousands: [500],
thousands: [5000],
});

function main(conf) {
Expand Down
6 changes: 3 additions & 3 deletions benchmark/timers/timers-cancel-pooled.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ const common = require('../common.js');
const assert = require('assert');

const bench = common.createBenchmark(main, {
thousands: [500],
millions: [5],
});

function main(conf) {
const iterations = +conf.thousands * 1e3;
const iterations = +conf.millions * 1e6;

var timer = setTimeout(() => {}, 1);
for (var i = 0; i < iterations; i++) {
Expand All @@ -24,7 +24,7 @@ function main(conf) {
clearTimeout(timer);
}

bench.end(iterations / 1e3);
bench.end(iterations / 1e6);
}

function cb() {
Expand Down
6 changes: 3 additions & 3 deletions benchmark/timers/timers-cancel-unpooled.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ const common = require('../common.js');
const assert = require('assert');

const bench = common.createBenchmark(main, {
thousands: [100],
millions: [1],
});

function main(conf) {
const iterations = +conf.thousands * 1e3;
const iterations = +conf.millions * 1e6;

const timersList = [];
for (var i = 0; i < iterations; i++) {
Expand All @@ -18,7 +18,7 @@ function main(conf) {
for (var j = 0; j < iterations + 1; j++) {
clearTimeout(timersList[j]);
}
bench.end(iterations / 1e3);
bench.end(iterations / 1e6);
}

function cb() {
Expand Down
6 changes: 3 additions & 3 deletions benchmark/timers/timers-insert-pooled.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
const common = require('../common.js');

const bench = common.createBenchmark(main, {
thousands: [500],
millions: [5],
});

function main(conf) {
const iterations = +conf.thousands * 1e3;
const iterations = +conf.millions * 1e6;

bench.start();

for (var i = 0; i < iterations; i++) {
setTimeout(() => {}, 1);
}

bench.end(iterations / 1e3);
bench.end(iterations / 1e6);
}
6 changes: 3 additions & 3 deletions benchmark/timers/timers-insert-unpooled.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ const common = require('../common.js');
const assert = require('assert');

const bench = common.createBenchmark(main, {
thousands: [100],
millions: [1],
});

function main(conf) {
const iterations = +conf.thousands * 1e3;
const iterations = +conf.millions * 1e6;

const timersList = [];

bench.start();
for (var i = 0; i < iterations; i++) {
timersList.push(setTimeout(cb, i + 1));
}
bench.end(iterations / 1e3);
bench.end(iterations / 1e6);

for (var j = 0; j < iterations + 1; j++) {
clearTimeout(timersList[j]);
Expand Down
30 changes: 21 additions & 9 deletions benchmark/timers/timers-timeout-pooled.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
'use strict';
const common = require('../common.js');

// The following benchmark measures setting up n * 1e6 timeouts,
// which then get executed on the next uv tick

const bench = common.createBenchmark(main, {
thousands: [500],
millions: [10],
});

function main(conf) {
const iterations = +conf.thousands * 1e3;
var count = 0;
const iterations = +conf.millions * 1e6;
let count = 0;

for (var i = 0; i < iterations; i++) {
setTimeout(cb, 1);
}

bench.start();
// Function tracking on the hidden class in V8 can cause misleading
// results in this benchmark if only a single function is used —
// alternate between two functions for a fairer benchmark

function cb() {
count++;
if (count === iterations)
bench.end(iterations / 1e3);
bench.end(iterations / 1e6);
}
function cb2() {
count++;
if (count === iterations)
bench.end(iterations / 1e6);
}

for (var i = 0; i < iterations; i++) {
setTimeout(i % 2 ? cb : cb2, 1);
}

bench.start();
}
Loading

0 comments on commit bd79c37

Please sign in to comment.