Commit ba62172b authored by Nico Hartmann's avatar Nico Hartmann Committed by V8 LUCI CQ

[js-perf-test] Performance benchmarks for BigInt left and right shift

Bug: v8:11515
Change-Id: I9379187542499439debd293dd7c7d6d02e98accd
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3308709
Commit-Queue: Nico Hartmann <nicohartmann@chromium.org>
Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Cr-Commit-Position: refs/heads/main@{#78213}
parent 81c6be2e
......@@ -1479,5 +1479,11 @@ RUNTIME_FUNCTION(Runtime_Is64Bit) {
return isolate->heap()->ToBoolean(kSystemPointerSize == 8);
}
RUNTIME_FUNCTION(Runtime_BigIntMaxLengthBits) {
HandleScope scope(isolate);
DCHECK_EQ(0, args.length());
return *isolate->factory()->NewNumber(BigInt::kMaxLengthBits);
}
} // namespace internal
} // namespace v8
......@@ -75,6 +75,7 @@ namespace internal {
F(BigIntEqualToBigInt, 2, 1) \
F(BigIntEqualToNumber, 2, 1) \
F(BigIntEqualToString, 2, 1) \
F(BigIntMaxLengthBits, 0, 1) \
F(BigIntToBoolean, 1, 1) \
F(BigIntToNumber, 1, 1) \
F(BigIntUnaryOp, 2, 1) \
......
......@@ -9,6 +9,7 @@ const TEST_ITERATIONS = 1000;
const SLOW_TEST_ITERATIONS = 50;
const BITS_CASES = [32, 64, 128, 256, 512, 1024, 2048, 4096, 8192];
const RANDOM_BIGINTS_MAX_BITS = 64 * 100;
const BIGINT_MAX_BITS = %BigIntMaxLengthBits();
function RandomHexDigit(allow_zero) {
......@@ -46,9 +47,7 @@ function MaxBigIntWithBits(bits) {
}
let s = "0x";
for (; bits > 0; bits -= 4) {
s += "F";
}
s += "F".repeat(bits / 4);
return BigInt(s);
}
......
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
"use strict";
d8.file.execute('bigint-util.js');
let a = 0n;
let random_bigints = [];
let small_shift_values = [];
// This dummy ensures that the feedback for benchmark.run() in the Measure
// function from base.js is not monomorphic, thereby preventing the benchmarks
// below from being inlined. This ensures consistent behavior and comparable
// results.
new BenchmarkSuite('Prevent-Inline-Dummy', [100], [
new Benchmark('Prevent-Inline-Dummy', true, false, 0, () => {})
]);
new BenchmarkSuite('ShiftLeft-ShiftTooBig', [1000], [
new Benchmark('ShiftLeft-ShiftTooBig', true, false, 0,
TestShiftLeftShiftTooBig, SetUpTestShiftLeftShiftTooBig)
]);
new BenchmarkSuite('ShiftLeft-ResultTooBig', [1000], [
new Benchmark('ShiftLeft-ResultTooBig', true, false, 0,
TestShiftLeftResultTooBig, SetUpTestShiftLeftResultTooBig)
]);
new BenchmarkSuite('ShiftLeft-Small', [1000], [
new Benchmark('ShiftLeft-Small', true, false, 0,
TestShiftLeftSmall, SetUpTestShiftLeftSmall)
]);
new BenchmarkSuite('ShiftLeft-Random', [1000], [
new Benchmark('ShiftLeft-Random', true, false, 0,
TestShiftLeftRandom, SetUpTestShiftLeftRandom)
]);
new BenchmarkSuite('ShiftLeft-Growing', [1000], [
new Benchmark('ShiftLeft-Growing', true, false, 0,
TestShiftLeftGrowing, SetUpTestShiftLeftGrowing)
]);
new BenchmarkSuite('ShiftRight-Maximum', [1000], [
new Benchmark('ShiftRight-Maximum', true, false, 0,
TestShiftRightMaximum, SetUpTestShiftRightMaximum)
]);
new BenchmarkSuite('ShiftRight-ToZero', [1000], [
new Benchmark('ShiftRight-ToZero', true, false, 0,
TestShiftRightToZero, SetUpTestShiftRightToZero)
]);
new BenchmarkSuite('ShiftRight-Small', [1000], [
new Benchmark('ShiftRight-Small', true, false, 0,
TestShiftRightSmall, SetUpTestShiftRightSmall)
]);
new BenchmarkSuite('ShiftRight-Random', [1000], [
new Benchmark('ShiftRight-Random', true, false, 0,
TestShiftRightRandom, SetUpTestShiftRightRandom)
]);
new BenchmarkSuite('ShiftRight-Shrinking', [1000], [
new Benchmark('ShiftRight-Shrinking', true, false, 0,
TestShiftRightShrinking, SetUpTestShiftRightShrinking)
]);
function SetUpRandomBigInts() {
random_bigints = [];
// RandomBigIntWithBits needs multiples of 4 bits.
const max_in_4bits = RANDOM_BIGINTS_MAX_BITS / 4;
for (let i = 0; i < Math.max(TEST_ITERATIONS, SLOW_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 SetUpTestShiftLeftShiftTooBig() {
// Left shifting by 2^80 is throwing an exception.
a = SmallRandomBigIntWithBits(80);
SetUpRandomBigInts();
}
function TestShiftLeftShiftTooBig() {
let result = 0n;
for (let i = 0; i < SLOW_TEST_ITERATIONS; ++i) {
try {
result = random_bigints[i] << a;
} catch(e) {
}
}
return result;
}
function SetUpTestShiftLeftResultTooBig() {
a = BigInt(BIGINT_MAX_BITS - 4);
for (let i = 0; i < SLOW_TEST_ITERATIONS; ++i) {
random_bigints[i] = RandomBigIntWithBits(64);
}
}
function TestShiftLeftResultTooBig() {
let result = 0n;
for (let i = 0; i < SLOW_TEST_ITERATIONS; ++i) {
try {
result = random_bigints[i] << a;
} catch(e) {
}
}
return result;
}
function SetUpTestShiftLeftSmall() {
random_bigints = [];
// Set up all values such that the left shifted values still fit into one
// digit.
for (let i = 0; i < TEST_ITERATIONS; ++i) {
random_bigints[i] = SmallRandomBigIntWithBits(16);
small_shift_values[i] = SmallRandomBigIntWithBits(4);
}
}
function TestShiftLeftSmall() {
let result = 0n;
for (let i = 0; i < TEST_ITERATIONS; ++i) {
result = random_bigints[i] << small_shift_values[i];
}
return result;
}
function SetUpTestShiftLeftRandom() {
if (RANDOM_BIGINTS_MAX_BITS + 2**16 > BIGINT_MAX_BITS) {
throw "Invalid Test Configuration";
}
small_shift_values = [];
SetUpRandomBigInts();
for (let i = 0; i < TEST_ITERATIONS; ++i) {
small_shift_values[i] = SmallRandomBigIntWithBits(16);
}
}
function TestShiftLeftRandom() {
let result = 0n;
for (let i = 0; i < TEST_ITERATIONS; ++i) {
result = random_bigints[i] << small_shift_values[i];
}
return result;
}
function SetUpTestShiftLeftGrowing() {
if (2**4 + (1000 * (2**8)) > BIGINT_MAX_BITS) {
throw "Invalid Test Configuration";
}
small_shift_values = [];
for (let i = 0; i < 1000; ++i) {
small_shift_values[i] = SmallRandomBigIntWithBits(8);
}
}
function TestShiftLeftGrowing() {
let result = a;
for (let i = 0; i < 1000; ++i) {
result = result << small_shift_values[i];
}
return result;
}
function SetUpTestShiftRightMaximum() {
// Right shifting by 2^80 will shift out all bits.
a = SmallRandomBigIntWithBits(80);
SetUpRandomBigInts();
}
function TestShiftRightMaximum() {
let result = 0n;
for (let i = 0; i < TEST_ITERATIONS; ++i) {
result = random_bigints[i] >> a;
}
return result;
}
function SetUpTestShiftRightToZero() {
// Choose a such that every random BigInt >> a will result in 0n/-1n.
a = BigInt(2 * RANDOM_BIGINTS_MAX_BITS);
SetUpRandomBigInts();
}
function TestShiftRightToZero() {
let result = 0n;
for (let i = 0; i < TEST_ITERATIONS; ++i) {
result = random_bigints[i] >> a;
}
return result;
}
function SetUpTestShiftRightSmall() {
random_bigints = [];
// We construct small BigInts with 32 bits such that they have a single digit
// even on 32bit architectures.
for (let i = 0; i < TEST_ITERATIONS; ++i) {
random_bigints[i] = SmallRandomBigIntWithBits(32);
small_shift_values[i] = BigInt(Math.floor(Math.random() * 30) + 1);
}
}
function TestShiftRightSmall() {
let result = 0n;
for (let i = 0; i < TEST_ITERATIONS; ++i) {
result = random_bigints[i] >> small_shift_values[i];
}
return result;
}
function SetUpTestShiftRightRandom() {
random_bigints = [];
small_shift_values = [];
const max_in_4bits = RANDOM_BIGINTS_MAX_BITS / 4;
for (let i = 0; i < TEST_ITERATIONS; ++i) {
const bits_in_4 = Math.floor(Math.random() * max_in_4bits);
const shift_in_4 = Math.floor(Math.random() * bits_in_4);
const bigint = RandomBigIntWithBits(bits_in_4 * 4);
random_bigints.push(Math.random() < 0.5 ? -bigint : bigint);
small_shift_values.push(BigInt(shift_in_4 * 4));
}
}
function TestShiftRightRandom() {
let result = 0n;
for (let i = 0; i < TEST_ITERATIONS; ++i) {
result = random_bigints[i] >> small_shift_values[i];
}
return result;
}
function SetUpTestShiftRightShrinking() {
small_shift_values = [];
// Choose values such that shifting right by 1000 times will not make the
// result to go 0n/-1n.
a = RandomBigIntWithBits(16*1000);
for (let i = 0; i < 1000; ++i) {
small_shift_values[i] = SmallRandomBigIntWithBits(4);
}
}
function TestShiftRightShrinking() {
let result = a;
for (let i = 0; i < 1000; ++i) {
result = result >> small_shift_values[i];
}
return result;
}
......@@ -65,6 +65,26 @@
"name": "BigInt",
"path": ["BigInt"],
"tests": [
{
"name": "Shift",
"main": "run.js",
"flags": ["--allow-natives-syntax"],
"resources": ["shift.js", "bigint-util.js"],
"test_flags": ["shift"],
"results_regexp": "^BigInt\\-%s\\(Score\\): (.+)$",
"tests": [
{ "name": "ShiftLeft-ShiftTooBig" },
{ "name": "ShiftLeft-ResultTooBig" },
{ "name": "ShiftLeft-Small" },
{ "name": "ShiftLeft-Random" },
{ "name": "ShiftLeft-Growing" },
{ "name": "ShiftRight-Maximum" },
{ "name": "ShiftRight-ToZero" },
{ "name": "ShiftRight-Small" },
{ "name": "ShiftRight-Random" },
{ "name": "ShiftRight-Shrinking" }
]
},
{
"name": "ToBoolean",
"main": "run.js",
......@@ -80,6 +100,7 @@
{
"name": "Add",
"main": "run.js",
"flags": ["--allow-natives-syntax"],
"resources": ["add.js", "bigint-util.js"],
"test_flags": ["add"],
"results_regexp": "^BigInt\\-%s\\(Score\\): (.+)$",
......@@ -110,6 +131,7 @@
{
"name": "Subtract",
"main": "run.js",
"flags": ["--allow-natives-syntax"],
"resources": ["subtract.js", "bigint-util.js"],
"test_flags": ["subtract"],
"results_regexp": "^BigInt\\-%s\\(Score\\): (.+)$",
......@@ -140,6 +162,7 @@
{
"name": "AsUintN",
"main": "run.js",
"flags": ["--allow-natives-syntax"],
"resources": ["as-uint-n.js", "bigint-util.js"],
"test_flags": ["as-uint-n"],
"results_regexp": "^BigInt\\-%s\\(Score\\): (.+)$",
......@@ -161,6 +184,7 @@
{
"name": "AsIntN",
"main": "run.js",
"flags": ["--allow-natives-syntax"],
"resources": ["as-int-n.js", "bigint-util.js"],
"test_flags": ["as-int-n"],
"results_regexp": "^BigInt\\-%s\\(Score\\): (.+)$",
......
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