[class] add microbenchmark for defining class fields

Landing define-class-fields microbencharks upstream before optimization so that
the benefit is visible.

Bug: v8:9888
Change-Id: Ie3bd2bd2cdd5710f43e398aa834985b5faa973d5
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2999030
Commit-Queue: Joyee Cheung <joyee@igalia.com>
Reviewed-by: Shu-yu Guo <syg@chromium.org>
Reviewed-by: Igor Sheludko <ishell@chromium.org>
Reviewed-by: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/master@{#76267}
This commit is contained in:
Joyee Cheung 2021-08-13 01:39:57 +08:00 committed by V8 LUCI CQ
parent 3d383fff26
commit aa4df5bc05
4 changed files with 281 additions and 0 deletions

View File

@ -0,0 +1,63 @@
{
"owners": ["caitp@igalia.com", "joyee@igalia.com"],
"name": "ClassFields",
"run_count": 3,
"run_count_arm": 1,
"run_count_arm64": 1,
"timeout": 120,
"timeout_arm64": 240,
"units": "score",
"total": true,
"resources": ["base.js"],
"tests": [
{
"name": "ClassFields",
"path": ["ClassFields"],
"flags": ["--allow-natives-syntax"],
"resources": [ "define-public-field.js" ],
"results_regexp": "^%s\\-ClassFields\\(Score\\): (.+)$",
"tests": [
{
"name": "define-public-field-single-opt",
"main": "run.js",
"test_flags": [ "define-public-field", "single", "opt" ]
},
{
"name": "define-public-field-single-noopt",
"main": "run.js",
"test_flags": [ "define-public-field", "single", "noopt" ]
},
{
"name": "define-public-field-multiple-opt",
"main": "run.js",
"test_flags": [ "define-public-field", "multiple", "opt" ]
},
{
"name": "define-public-field-multiple-noopt",
"main": "run.js",
"test_flags": [ "define-public-field", "multiple", "noopt" ]
},
{
"name": "define-private-field-single-opt",
"main": "run.js",
"test_flags": [ "define-private-field", "single", "opt" ]
},
{
"name": "define-private-field-single-noopt",
"main": "run.js",
"test_flags": [ "define-private-field", "single", "noopt" ]
},
{
"name": "define-private-field-multiple-opt",
"main": "run.js",
"test_flags": [ "define-private-field", "multiple", "opt" ]
},
{
"name": "define-private-field-multiple-noopt",
"main": "run.js",
"test_flags": [ "define-private-field", "multiple", "noopt" ]
}
]
}
]
}

View File

@ -0,0 +1,96 @@
// 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';
const BENCHMARK_NAME = arguments[0];
const TEST_TYPE = arguments[1];
const optimize_param = arguments[2];
let optimize;
if (optimize_param == "opt") {
optimize = true;
} else if (optimize_param == "noopt"){
optimize = false;
} else {
throw new Error("Unknown optimization configuration " + arguments.join(' '));
}
let klass;
let i = 0;
let array;
class SinglePrivateFieldClass {
#x = i;
check() {
return this.#x === i;
}
}
class MultiPrivateFieldClass {
#x = i;
#y = i+1;
#z = i+2;
#q = i+3;
#r = i+4;
#a = i+5;
check() {
return this.#x + 1 === this.#y && this.#y + 1 === this.#z &&
this.#z + 1 === this.#q && this.#q + 1 === this.#r &&
this.#r + 1 === this.#a;
}
}
switch (TEST_TYPE) {
case "single":
klass = SinglePrivateFieldClass;
break;
case "multiple":
klass = MultiPrivateFieldClass;
break;
default:
throw new Error("Unknown optimization configuration " + arguments.join(' '));
}
if (optimize) {
%PrepareFunctionForOptimization(klass);
} else {
%NeverOptimizeFunction(klass);
}
function setUp() {
array = [new klass(), new klass()];
// Populate the array first to reduce the impact of
// array allocations.
for (let i = 0; i < LOCAL_ITERATIONS - 2; ++i) {
array.push(array[0]);
}
if (optimize) {
%OptimizeFunctionOnNextCall(klass);
}
}
function runBenchmark() {
for (let i = 0; i < LOCAL_ITERATIONS; ++i) {
array[i] = new klass();
}
}
function tearDown() {
if (array.length < 3) {
throw new Error(`Check failed, array length ${array.length}`);
}
for (const instance of array) {
if (!instance.check())
throw new Error(`instance.check() failed`);
}
}
const DETERMINISTIC_RUNS = 1;
const LOCAL_ITERATIONS = 10000;
new BenchmarkSuite(`${BENCHMARK_NAME}`, [1000], [
new Benchmark(
`${BENCHMARK_NAME}-${TEST_TYPE}-${optimize_param}`,
false, false, DETERMINISTIC_RUNS, runBenchmark, setUp, tearDown)
]);

View File

@ -0,0 +1,97 @@
// 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';
const BENCHMARK_NAME = arguments[0];
const TEST_TYPE = arguments[1];
const optimize_param = arguments[2];
let optimize;
if (optimize_param == "opt") {
optimize = true;
} else if (optimize_param == "noopt"){
optimize = false;
} else {
throw new Error("Unknown optimization configuration " + arguments.join(' '));
}
let klass;
let i = 0;
let array;
class SinglePublicFieldClass {
x = i;
check() {
return this.x === i;
}
}
class MultiPublicFieldClass {
x = i;
y = i+1;
z = i+2;
q = i+3;
r = i+4;
a = i+5;
check() {
return this.x + 1 === this.y && this.y + 1 === this.z &&
this.z + 1 === this.q && this.q + 1 === this.r &&
this.r + 1 === this.a;
}
}
switch (TEST_TYPE) {
case "single":
klass = SinglePublicFieldClass;
break;
case "multiple":
klass = MultiPublicFieldClass;
break;
default:
throw new Error("Unknown optimization configuration " + arguments.join(' '));
}
if (optimize) {
%PrepareFunctionForOptimization(klass);
} else {
%NeverOptimizeFunction(klass);
}
function setUp() {
array = [new klass(), new klass()];
// Populate the array first to reduce the impact of
// array allocations.
for (let i = 0; i < LOCAL_ITERATIONS - 2; ++i) {
array.push(array[0]);
}
if (optimize) {
%OptimizeFunctionOnNextCall(klass);
}
}
function runBenchmark() {
for (let i = 0; i < LOCAL_ITERATIONS; ++i) {
array[i] = new klass();
}
}
function tearDown() {
if (array.length < 3) {
throw new Error(`Check failed, array length ${array.length}`);
}
for (const instance of array) {
if (!instance.check())
throw new Error(`instance.check() failed`);
}
}
const DETERMINISTIC_RUNS = 1;
const LOCAL_ITERATIONS = 10000;
new BenchmarkSuite(`${BENCHMARK_NAME}`, [1000], [
new Benchmark(
`${BENCHMARK_NAME}-${TEST_TYPE}-${optimize_param}`,
false, false, DETERMINISTIC_RUNS, runBenchmark, setUp, tearDown)
]);

View File

@ -0,0 +1,25 @@
// 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.
load('../base.js');
load(arguments[0] + '.js');
var success = true;
function PrintResult(name, result, mean) {
print(`${name}-ClassFields(Score): ${result} (mean: ${mean})`);
}
function PrintError(name, error) {
PrintResult(name, error);
success = false;
}
BenchmarkSuite.config.doWarmup = undefined;
BenchmarkSuite.config.doDeterministic = undefined;
BenchmarkSuite.RunSuites({ NotifyResult: PrintResult,
NotifyError: PrintError });