Commit ea79fa42 authored by Nico Hartmann's avatar Nico Hartmann Committed by Commit Bot

[js-perf-test] Performance benchmark for adding random BigInts

Bug: v8:9213
Change-Id: Iee04bfe25e37cfe639964f8ab31e6702d3750bfb
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1645319
Commit-Queue: Nico Hartmann <nicohartmann@google.com>
Reviewed-by: 's avatarGeorg Neis <neis@chromium.org>
Reviewed-by: 's avatarSigurd Schneider <sigurds@chromium.org>
Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Cr-Commit-Position: refs/heads/master@{#62016}
parent fb4d5480
...@@ -9,9 +9,11 @@ load('bigint-util.js'); ...@@ -9,9 +9,11 @@ load('bigint-util.js');
const TEST_ITERATIONS = 1000; const TEST_ITERATIONS = 1000;
const SLOW_TEST_ITERATIONS = 50; const SLOW_TEST_ITERATIONS = 50;
const BITS_CASES = [32, 64, 128, 256, 512, 1024, 2048, 4096, 8192]; const BITS_CASES = [32, 64, 128, 256, 512, 1024, 2048, 4096, 8192];
const RANDOM_BIGINTS_MAX_BITS = 64 * 100;
let initial_sum = 0n; let initial_sum = 0n;
let a = 0n; let a = 0n;
let random_bigints = [];
// This dummy ensures that the feedback for benchmark.run() in the Measure // This dummy ensures that the feedback for benchmark.run() in the Measure
// function from base.js is not monomorphic, thereby preventing the benchmarks // function from base.js is not monomorphic, thereby preventing the benchmarks
...@@ -49,6 +51,12 @@ BITS_CASES.forEach((d) => { ...@@ -49,6 +51,12 @@ BITS_CASES.forEach((d) => {
}); });
new BenchmarkSuite('Add-Random', [1000], [
new Benchmark('Add-Random', true, false, 0, TestAddRandom,
SetUpTestAddRandom)
]);
function SetUpTestAddTypeError() { function SetUpTestAddTypeError() {
initial_sum = 42n; initial_sum = 42n;
} }
...@@ -115,3 +123,25 @@ function TestAddDifferentSign() { ...@@ -115,3 +123,25 @@ function TestAddDifferentSign() {
return sum; return sum;
} }
function SetUpTestAddRandom() {
random_bigints = [];
// RandomBigIntWithBits needs multiples of 4 bits.
const max_in_4bits = RANDOM_BIGINTS_MAX_BITS / 4;
for (let i = 0; i < TEST_ITERATIONS; ++i) {
const bits = Math.floor(Math.random() * max_in_4bits) * 4;
const bigint = RandomBigIntWithBits(bits);
random_bigints.push(Math.random() < 0.5 ? -bigint : bigint);
}
}
function TestAddRandom() {
let sum = 0n;
for (let i = 0; i < TEST_ITERATIONS; ++i) {
sum = random_bigints[i] + sum;
}
return sum;
}
...@@ -20,6 +20,7 @@ function SmallRandomBigIntWithBits(bits) { ...@@ -20,6 +20,7 @@ function SmallRandomBigIntWithBits(bits) {
return 0n; return 0n;
} }
// Make sure it does not start with four 0-bits.
let s = "0x" + RandomHexDigit(false); let s = "0x" + RandomHexDigit(false);
bits -= 4; bits -= 4;
// Digits are at least 32 bits long, so we round down to the next smaller // Digits are at least 32 bits long, so we round down to the next smaller
...@@ -44,3 +45,21 @@ function MaxBigIntWithBits(bits) { ...@@ -44,3 +45,21 @@ function MaxBigIntWithBits(bits) {
} }
return BigInt(s); return BigInt(s);
} }
// Generates a random BigInt between 2^(bits-4) and 2^bits-1 (for bits > 0).
function RandomBigIntWithBits(bits) {
console.assert(bits % 4 === 0);
if (bits <= 0) {
return 0n;
}
// Make sure it does not start with four 0-bits.
let s = "0x" + RandomHexDigit(false);
bits -= 4;
// Randomly generate remaining bits.
for (; bits > 0; bits -= 4) {
s += RandomHexDigit(true);
}
return BigInt(s);
}
...@@ -78,7 +78,8 @@ ...@@ -78,7 +78,8 @@
{ "name": "Add-SameSign-4096" }, { "name": "Add-SameSign-4096" },
{ "name": "Add-DifferentSign-4096" }, { "name": "Add-DifferentSign-4096" },
{ "name": "Add-SameSign-8192" }, { "name": "Add-SameSign-8192" },
{ "name": "Add-DifferentSign-8192" } { "name": "Add-DifferentSign-8192" },
{ "name": "Add-Random" }
] ]
}, },
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment