Commit 91d49028 authored by Nico Hartmann's avatar Nico Hartmann Committed by Commit Bot

[js-perf-test] Performance benchmarks for addition of BigInts

Bug: v8:9213
Change-Id: I78e2e4b00c3dec92252adf3823138b14a7bc4a5c
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1640202
Commit-Queue: Nico Hartmann <nicohartmann@google.com>
Reviewed-by: 's avatarSigurd Schneider <sigurds@chromium.org>
Reviewed-by: 's avatarGeorg Neis <neis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#61994}
parent b15b2c91
// Copyright 2019 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";
load('bigint-util.js');
const TEST_ITERATIONS = 1000;
const SLOW_TEST_ITERATIONS = 50;
const BITS_CASES = [32, 64, 128, 256, 512, 1024, 2048, 4096, 8192];
let initial_sum = 0n;
let a = 0n;
// 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', [10000], [
new Benchmark('Prevent-Inline-Dummy', true, false, 0, () => {})
]);
new BenchmarkSuite('Add-TypeError', [10000], [
new Benchmark('Add-TypeError', true, false, 0, TestAddTypeError,
SetUpTestAddTypeError)
]);
new BenchmarkSuite('Add-Zero', [1000], [
new Benchmark('Add-Zero', true, false, 0, TestAddZero, SetUpTestAddZero)
]);
BITS_CASES.forEach((d) => {
new BenchmarkSuite(`Add-SameSign-${d}`, [1000], [
new Benchmark(`Add-SameSign-${d}`, true, false, 0, TestAddSameSign,
() => SetUpTestAddSameSign(d))
]);
});
BITS_CASES.forEach((d) => {
new BenchmarkSuite(`Add-DifferentSign-${d}`, [1000], [
new Benchmark(`Add-DifferentSign-${d}`, true, false, 0,
TestAddDifferentSign, () => SetUpTestAddDifferentSign(d))
]);
});
function SetUpTestAddTypeError() {
initial_sum = 42n;
}
function TestAddTypeError() {
let sum = initial_sum;
for (let i = 0; i < SLOW_TEST_ITERATIONS; ++i) {
try {
sum = 0 + sum;
}
catch(e) {
}
}
return sum;
}
function SetUpTestAddZero() {
initial_sum = 42n;
}
function TestAddZero() {
let sum = initial_sum;
for (let i = 0; i < TEST_ITERATIONS; ++i) {
sum = 0n + sum;
}
return sum;
}
function SetUpTestAddSameSign(bits) {
// Add two small random positive values to make sure the sum does not grow
// in digits.
initial_sum = SmallRandomBigIntWithBits(bits);
a = SmallRandomBigIntWithBits(bits);
}
function TestAddSameSign() {
let sum = initial_sum;
for (let i = 0; i < TEST_ITERATIONS; ++i) {
sum = a + sum;
}
return sum;
}
function SetUpTestAddDifferentSign(bits) {
// Add a small random negative value to a large positive one to make sure the
// sum does not shrink in digits.
initial_sum = MaxBigIntWithBits(bits);
a = -SmallRandomBigIntWithBits(bits);
}
function TestAddDifferentSign() {
let sum = initial_sum;
for (let i = 0; i < TEST_ITERATIONS; ++i) {
sum = a + sum;
}
return sum;
}
// Copyright 2019 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";
function RandomHexDigit(allow_zero) {
const chars = allow_zero ? '0123456789ABCDEF' : '123456789ABCDEF';
return chars.charAt(Math.floor(Math.random() * chars.length));
}
// Some benchmarks shall compute sums but the result must not grow in terms
// of digits. These can use "small" BigInts, which are BigInts where the most
// significant bits of a digit are 0, so it does not overflow.
function SmallRandomBigIntWithBits(bits) {
console.assert(bits % 4 === 0);
if (bits <= 0) {
return 0n;
}
let s = "0x" + RandomHexDigit(false);
bits -= 4;
// Digits are at least 32 bits long, so we round down to the next smaller
// multiple of 32 to keep the most significant digit small.
bits = Math.floor(bits / 32) * 32;
for (; bits > 0; bits -= 4) {
s += RandomHexDigit(true);
}
return BigInt(s);
}
function MaxBigIntWithBits(bits) {
console.assert(bits % 4 === 0);
if (bits <= 0) {
return 0n;
}
let s = "0x";
for (; bits > 0; bits -= 4) {
s += "F";
}
return BigInt(s);
}
......@@ -6,6 +6,7 @@
load('../base.js');
load('to-boolean.js');
load('add.js');
var success = true;
......
......@@ -50,14 +50,36 @@
"path": ["BigInt"],
"main": "run.js",
"resources": [
"to-boolean.js"
"bigint-util.js",
"to-boolean.js",
"add.js"
],
"results_regexp": "^%s\\-BigInt\\(Score\\): (.+)$",
"tests": [
{ "name": "BigInt-ToBoolean" },
{ "name": "BigInt-BooleanConstructor" },
{ "name": "BigInt-NewBooleanConstructor" }
]
{ "name": "BigInt-NewBooleanConstructor" },
{ "name": "Add-TypeError" },
{ "name": "Add-Zero" },
{ "name": "Add-SameSign-32" },
{ "name": "Add-DifferentSign-32" },
{ "name": "Add-SameSign-64" },
{ "name": "Add-DifferentSign-64" },
{ "name": "Add-SameSign-128" },
{ "name": "Add-DifferentSign-128" },
{ "name": "Add-SameSign-256" },
{ "name": "Add-DifferentSign-256" },
{ "name": "Add-SameSign-512" },
{ "name": "Add-DifferentSign-512" },
{ "name": "Add-SameSign-1024" },
{ "name": "Add-DifferentSign-1024" },
{ "name": "Add-SameSign-2048" },
{ "name": "Add-DifferentSign-2048" },
{ "name": "Add-SameSign-4096" },
{ "name": "Add-DifferentSign-4096" },
{ "name": "Add-SameSign-8192" },
{ "name": "Add-DifferentSign-8192" }
]
},
{
"name": "BigInt-Jitless",
......
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