From 298e4149368b23fee573fa27f1a00e0c50828c8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Mon, 17 Jul 2023 23:17:28 +0200 Subject: fix(bench): run warmup benchmark to break JIT bias (#19844) Closes https://github.com/denoland/deno/issues/15277 This commit adds a single "warmup" run of empty function when running `deno bench`. This change will break so-called "JIT bias" which makes V8 optimize the first function and then bail out of optimization on second function. In essence the "warmup" function is getting optimized and then all user benches are bailed out of optimization. --- cli/js/40_testing.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'cli/js/40_testing.js') diff --git a/cli/js/40_testing.js b/cli/js/40_testing.js index 3058fcee3..740e950a3 100644 --- a/cli/js/40_testing.js +++ b/cli/js/40_testing.js @@ -685,6 +685,8 @@ function test( }); } +let registeredWarmupBench = false; + // Main bench function provided by Deno. function bench( nameOrFnOrOptions, @@ -695,6 +697,25 @@ function bench( return; } + if (!registeredWarmupBench) { + registeredWarmupBench = true; + const warmupBenchDesc = { + name: "", + fn: function warmup() {}, + async: false, + ignore: false, + baseline: false, + only: false, + sanitizeExit: true, + permissions: null, + warmup: true, + }; + warmupBenchDesc.fn = wrapBenchmark(warmupBenchDesc); + const { id, origin } = ops.op_register_bench(warmupBenchDesc); + warmupBenchDesc.id = id; + warmupBenchDesc.origin = origin; + } + let benchDesc; const defaults = { ignore: false, @@ -777,6 +798,7 @@ function bench( const AsyncFunction = (async () => {}).constructor; benchDesc.async = AsyncFunction === benchDesc.fn.constructor; benchDesc.fn = wrapBenchmark(benchDesc); + benchDesc.warmup = false; const { id, origin } = ops.op_register_bench(benchDesc); benchDesc.id = id; -- cgit v1.2.3