Commit 5874911b authored by Simon Zünd's avatar Simon Zünd Committed by Commit Bot

Extend Array.p.sort benchmarks.

This adds benchmarks that sort all element kinds with multiple comparison
functions. This also adds benchmarks that cause the element kind of
the array to change after x amount of comparisons.
The last set of benchmarks that are added, measure performance on
pre-sorted arrays.

R=jgruber@chromium.org, petermarshall@chromium.org

Bug: v8:7382
Change-Id: I620de37f4a28f8096682bb708ea4f7a9a21d5316
Reviewed-on: https://chromium-review.googlesource.com/1009602
Commit-Queue: Peter Marshall <petermarshall@chromium.org>
Reviewed-by: 's avatarJakob Gruber <jgruber@chromium.org>
Cr-Commit-Position: refs/heads/master@{#52574}
parent 44007be0
......@@ -3,12 +3,12 @@
// found in the LICENSE file.
load('../base.js');
load('sort.js')
load(arguments[0] + '.js')
function benchy(name, test, testSetup) {
function benchy(name, test, testSetup, testTearDown) {
new BenchmarkSuite(name, [1000],
[
new Benchmark(name, false, false, 0, test, testSetup)
new Benchmark(name, false, false, 0, test, testSetup, testTearDown)
]);
}
......
// Copyright 2018 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.
const kArraySize = 4000;
let template_array = [];
for (let i = 0; i < kArraySize; ++i) {
template_array[i] = Math.floor(Math.random() * kArraySize);
}
let array_to_sort = [];
function assert(condition, message) {
if (!condition) {
throw Error(message);
}
}
function AssertPackedSmiElements() {
assert(%HasFastPackedElements(array_to_sort) &&
%HasSmiElements(array_to_sort),
"Element kind is not PACKED_SMI_ELEMENTS");
}
function AssertPackedDoubleElements() {
assert(%HasFastPackedElements(array_to_sort) &&
%HasDoubleElements(array_to_sort),
"Element kind is not PACKED_DOUBLE_ELEMENTS");
}
function AssertPackedObjectElements() {
assert(%HasFastPackedElements(array_to_sort) &&
%HasObjectElements(array_to_sort),
"Element kind is not PACKED_ELEMENTS");
}
function AssertHoleySmiElements() {
assert(%HasHoleyElements(array_to_sort) &&
%HasSmiElements(array_to_sort),
"Element kind is not HOLEY_SMI_ELEMENTS");
}
function AssertHoleyDoubleElements() {
assert(%HasHoleyElements(array_to_sort) &&
%HasDoubleElements(array_to_sort),
"Element kind is not HOLEY_DOUBLE_ELEMENTS");
}
function AssertHoleyObjectElements() {
assert(%HasHoleyElements(array_to_sort) &&
%HasObjectElements(array_to_sort),
"Element kind is not HOLEY_ELEMENTS");
}
function AssertDictionaryElements() {
assert(%HasDictionaryElements(array_to_sort),
"Element kind is not DICTIONARY_ELEMENTS");
}
function CreatePackedSmiArray() {
array_to_sort = Array.from(template_array);
AssertPackedSmiElements();
}
function CreatePackedDoubleArray() {
array_to_sort = Array.from(template_array, (x,_) => x + 0.1);
AssertPackedDoubleElements();
}
function CreatePackedObjectArray() {
array_to_sort = Array.from(template_array, (x,_) => `value ${x}`);
AssertPackedObjectElements();
}
function CreateHoleySmiArray() {
array_to_sort = new Array(kArraySize);
for (let i = 0; i < kArraySize; ++i) {
array_to_sort[i] = template_array[i];
}
AssertHoleySmiElements();
}
function CreateHoleyDoubleArray() {
array_to_sort = new Array(kArraySize);
for (let i = 0; i < kArraySize; ++i) {
array_to_sort[i] = template_array[i] + 0.1;
}
AssertHoleyDoubleElements();
}
function CreateHoleyObjectArray() {
array_to_sort = new Array(kArraySize);
for (let i = 0; i < kArraySize; ++i) {
array_to_sort[i] = `value ${template_array[i]}`;
}
AssertHoleyObjectElements();
}
function CreateDictionaryArray() {
array_to_sort = Array.from(template_array);
array_to_sort[%MaxSmi()] = 42;
AssertDictionaryElements();
}
function Sort() {
array_to_sort.sort();
}
function CreateSortFn(comparefns = []) {
return () => {
for (let cmpfn of comparefns) {
array_to_sort.sort(cmpfn);
}
}
}
function cmp_smaller(a, b) {
if (a < b) return -1;
if (b < a) return 1;
return 0;
}
// Copyright 2018 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('sort-base.js');
// Creates a comparison function, that will call the provided transform function
// after a set amount of comparisons. The transform function should cause the
// element kind of the array to change.
function CreateCompareFn(transformfn) {
let counter = 0;
return (a, b) => {
++counter;
if (counter == kArraySize/2) {
transformfn();
}
return cmp_smaller(a, b);
}
}
let cmp_packed_smi_to_double = CreateCompareFn(() => array_to_sort.push(0.1));
let cmp_holey_smi_to_double = CreateCompareFn(() => array_to_sort.push(0.1));
let cmp_double_to_double = CreateCompareFn(() => array_to_sort.length *= 2);
benchy('PackedSmiToPackedDouble', CreateSortFn([cmp_packed_smi_to_double]),
CreatePackedSmiArray, AssertPackedDoubleElements);
benchy('HoleySmiToHoleyDouble', CreateSortFn([cmp_holey_smi_to_double]),
CreateHoleySmiArray, AssertHoleyDoubleElements);
benchy('PackedDoubleToHoleyDouble', CreateSortFn([cmp_double_to_double]),
CreatePackedDoubleArray, AssertHoleyDoubleElements);
let cmp_packed_to_dict = CreateCompareFn(() => array_to_sort[%MaxSmi()] = 42);
let cmp_holey_to_dict = CreateCompareFn(() => array_to_sort[%MaxSmi()] = 42);
benchy('PackedElementToDictionary', CreateSortFn([cmp_packed_to_dict]),
CreatePackedObjectArray, AssertDictionaryElements);
benchy('HoleyElementToDictionary', CreateSortFn([cmp_holey_to_dict]),
CreateHoleyObjectArray, AssertDictionaryElements);
// Copyright 2018 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('sort-base.js');
function cmp_greater(a, b) { return cmp_smaller(b, a); }
// Each benchmark calls sort with multiple different comparison functions
// to create polyomorphic call sites. Most/all of the
// other sort benchmarks have monomorphic call sites.
let sortfn = CreateSortFn([cmp_smaller, cmp_greater]);
benchy('PackedSmi', sortfn, CreatePackedSmiArray);
benchy('PackedDouble', sortfn, CreatePackedDoubleArray);
benchy('PackedElement', sortfn, CreatePackedObjectArray);
benchy('HoleySmi', sortfn, CreateHoleySmiArray);
benchy('HoleyDouble', sortfn, CreateHoleyDoubleArray);
benchy('HoleyElement', sortfn, CreateHoleyObjectArray);
benchy('Dictionary', sortfn, CreateDictionaryArray);
// Copyright 2018 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('sort-base.js');
function SetupPreSorted() {
CreatePackedSmiArray();
array_to_sort.sort();
}
function SetupPreSortedReversed() {
CreatePackedSmiArray();
array_to_sort.sort();
array_to_sort.reverse();
}
benchy('PackedSmiPreSorted', Sort, SetupPreSorted);
benchy('PackedSmiPreSortedReversed', Sort, SetupPreSortedReversed);
......@@ -2,97 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
(() => {
load('sort-base.js');
const size = 10;
let template_array = [];
benchy('PackedSmi', Sort, CreatePackedSmiArray);
benchy('PackedDouble', Sort, CreatePackedDoubleArray);
benchy('PackedElement', Sort, CreatePackedObjectArray);
for (let i = 0; i < size; ++i) {
template_array[i] = Math.floor(Math.random() * size);
}
benchy('HoleySmi', Sort, CreateHoleySmiArray);
benchy('HoleyDouble', Sort, CreateHoleyDoubleArray);
benchy('HoleyElement', Sort, CreateHoleyObjectArray);
let packed_smi_array = Array.from(template_array);
let packed_double_array = Array.from(template_array, (x,_) => x + 0.1);
let packed_object_array = Array.from(template_array, (x,_) => `value ${x}`);
let holey_smi_array = new Array(size);
let holey_double_array = new Array(size);
let holey_object_array = new Array(size);
for (let i = 0; i < size; i += 5) {
const x = Math.floor(Math.random() * size);
holey_smi_array[i] = x;
holey_double_array[i] = x + 0.1;
holey_object_array[i] = `value ${x}`;
}
let dictionary_array = Array.from(template_array);
dictionary_array[%MaxSmi()] = 42;
function assert(condition, message) {
if (!condition) {
throw Error(message);
}
}
assert(%HasFastPackedElements(packed_smi_array) &&
%HasSmiElements(packed_smi_array),
"Element kind is not PACKED_SMI_ELEMENTS");
assert(%HasFastPackedElements(packed_double_array) &&
%HasDoubleElements(packed_double_array),
"Element kind is not PACKED_DOUBLE_ELEMENTS");
assert(%HasFastPackedElements(packed_object_array) &&
%HasObjectElements(packed_object_array),
"Element kind is not PACKED_ELEMENTS");
assert(%HasHoleyElements(holey_smi_array) &&
%HasSmiElements(holey_smi_array),
"Element kind is not HOLEY_SMI_ELEMENTS");
assert(%HasHoleyElements(holey_double_array) &&
%HasDoubleElements(holey_double_array),
"Element kind is not HOLEY_DOUBLE_ELEMENTS");
assert(%HasHoleyElements(holey_object_array) &&
%HasObjectElements(holey_object_array),
"Element kind is not HOLEY_ELEMENTS");
assert(%HasDictionaryElements(dictionary_array),
"Element kind is not DICTIONARY_ELEMENTS");
function PackedSmiSort() {
packed_smi_array.sort();
}
function PackedDoubleSort() {
packed_double_array.sort();
}
function PackedElementSort() {
packed_object_array.sort();
}
function HoleySmiSort() {
holey_smi_array.sort();
}
function HoleyDoubleSort() {
holey_double_array.sort();
}
function HoleyElementSort() {
holey_object_array.sort();
}
function DictionarySort() {
dictionary_array.sort();
}
benchy('PackedSmi', PackedSmiSort);
benchy('PackedDouble', PackedDoubleSort);
benchy('PackedElement', PackedElementSort);
benchy('HoleySmi', HoleySmiSort);
benchy('HoleyDouble', HoleyDoubleSort);
benchy('HoleyElement', HoleyElementSort);
benchy('Dictionary', DictionarySort);
})();
benchy('Dictionary', Sort, CreateDictionaryArray);
......@@ -648,11 +648,32 @@
"name": "ArraySort",
"path": ["ArraySort"],
"main": "run.js",
"resources": ["sort.js"],
"resources": ["sort-base.js", "sort.js"],
"test_flags": ["sort"],
"results_regexp": "^%s\\-ArraySort\\(Score\\): (.+)$",
"flags": [
"--allow-natives-syntax"
],
"tests": [
{"name": "PackedSmi"},
{"name": "PackedDouble"},
{"name": "PackedElement"},
{"name": "HoleySmi"},
{"name": "HoleyDouble"},
{"name": "HoleyElement"},
{"name": "Dictionary"}
]
},
{
"name": "ArraySortCompareFns",
"path": ["ArraySort"],
"main": "run.js",
"resources": ["sort-base.js", "sort-cmpfn.js"],
"test_flags": ["sort-cmpfn"],
"results_regexp": "^%s\\-ArraySort\\(Score\\): (.+)$",
"flags": [
"--allow-natives-syntax"
],
"tests": [
{"name": "PackedSmi"},
{"name": "PackedDouble"},
......@@ -663,6 +684,39 @@
{"name": "Dictionary"}
]
},
{
"name": "ArraySortCompareFnKindChange",
"path": ["ArraySort"],
"main": "run.js",
"resources": ["sort-base.js", "sort-cmpfn-kindchange.js"],
"test_flags": ["sort-cmpfn-kindchange"],
"results_regexp": "^%s\\-ArraySort\\(Score\\): (.+)$",
"flags": [
"--allow-natives-syntax"
],
"tests": [
{"name": "PackedSmiToPackedDouble"},
{"name": "HoleySmiToHoleyDouble"},
{"name": "PackedDoubleToHoleyDouble"},
{"name": "PackedElementToDictionary"},
{"name": "HoleyElementToDictionary"}
]
},
{
"name": "ArraySortPreSorted",
"path": ["ArraySort"],
"main": "run.js",
"resources": ["sort-base.js", "sort-presorted.js"],
"test_flags": ["sort-presorted"],
"results_regexp": "^%s\\-ArraySort\\(Score\\): (.+)$",
"flags": [
"--allow-natives-syntax"
],
"tests": [
{"name": "PackedSmiPreSorted"},
{"name": "PackedSmiPreSortedReversed"}
]
},
{
"name": "ForLoops",
"path": ["ForLoops"],
......
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