[benchmark] Update BigIntParsePrint

- Generate strings on one place rather than work hard to generate them
  the same with different number types.

- Update dart2 version

Change-Id: I449d08a5ead13504ec604b11bd63b729590d6ffe
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/175861
Reviewed-by: William Hesse <whesse@google.com>
Commit-Queue: Stephen Adams <sra@google.com>
This commit is contained in:
Stephen Adams 2020-12-17 06:32:33 +00:00 committed by commit-bot@chromium.org
parent 368d9f375f
commit 7ebbc89e78
2 changed files with 139 additions and 269 deletions

View file

@ -4,8 +4,6 @@
// ignore_for_file: avoid_function_literals_in_foreach_calls
import 'dart:math' show pow;
import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:fixnum/fixnum.dart';
@ -33,29 +31,38 @@ void check(bool sink2isEven) {
// integers.
const requiredDigits = 11106;
class ParseBigIntBenchmark extends BenchmarkBase {
final int bits;
final BigInt seed;
final List<String> strings = [];
ParseBigIntBenchmark(String name, this.bits)
: seed = (BigInt.one << bits) - BigInt.one,
class Benchmark extends BenchmarkBase {
final List<String> strings;
Benchmark(String name, int bits)
: strings = generateStrings(bits),
super(name);
@override
void setup() {
static List<String> generateStrings(int bits) {
List<String> strings = [];
BigInt seed = (BigInt.one << bits) - BigInt.one;
var b = seed;
var restartDelta = BigInt.zero;
var totalLength = 0;
while (totalLength < requiredDigits) {
if (b.bitLength < bits) {
b = seed;
restartDelta += seed >> 20;
restartDelta += BigInt.one;
// Restart from a slighly reduced seed to generate different numbers.
b = seed - restartDelta;
}
final string = b.toString();
strings.add(string);
totalLength += string.length;
b = b - (b >> 8);
var delta = b >> 8;
if (delta == BigInt.zero) delta = BigInt.one;
b = b - delta;
}
return strings;
}
}
class ParseBigIntBenchmark extends Benchmark {
ParseBigIntBenchmark(String name, int bits) : super(name, bits);
@override
void run() {
@ -68,31 +75,8 @@ class ParseBigIntBenchmark extends BenchmarkBase {
}
}
int int64UnsignedBitLength(Int64 i) => i.isNegative ? 64 : i.bitLength;
class ParseInt64Benchmark extends BenchmarkBase {
final int bits;
final Int64 seed;
final List<String> strings = [];
ParseInt64Benchmark(String name, this.bits)
: seed = (Int64.ONE << bits) - Int64.ONE,
super(name);
@override
void setup() {
var b = seed;
var totalLength = 0;
while (totalLength < requiredDigits) {
if (int64UnsignedBitLength(b) < bits) {
b = seed;
}
final string = b.toStringUnsigned();
strings.add(string);
totalLength += string.length;
b = b - b.shiftRightUnsigned(8);
}
}
class ParseInt64Benchmark extends Benchmark {
ParseInt64Benchmark(String name, int bits) : super(name, bits);
@override
void run() {
@ -105,29 +89,8 @@ class ParseInt64Benchmark extends BenchmarkBase {
}
}
class ParseIntBenchmark extends BenchmarkBase {
final int bits;
final int seed;
final List<String> strings = [];
ParseIntBenchmark(String name, this.bits)
: seed = (pow(2, bits) as int) - 1,
super(name);
@override
void setup() {
var b = seed;
var totalLength = 0;
while (totalLength < requiredDigits) {
if (b.bitLength < bits) {
b = seed;
}
final string = b.toString();
strings.add(string);
totalLength += string.length;
b = b - b ~/ 256;
}
}
class ParseIntBenchmark extends Benchmark {
ParseIntBenchmark(String name, int bits) : super(name, bits);
@override
void run() {
@ -140,33 +103,8 @@ class ParseIntBenchmark extends BenchmarkBase {
}
}
class ParseJsBigIntBenchmark extends BenchmarkBase {
final int bits;
final Object seed;
final List<String> strings = [];
ParseJsBigIntBenchmark(String name, this.bits)
: seed = nativeBigInt.subtract(
nativeBigInt.shiftLeft(
nativeBigInt.one, nativeBigInt.fromInt(bits)),
nativeBigInt.one),
super(name);
@override
void setup() {
var b = seed;
var totalLength = 0;
while (totalLength < requiredDigits) {
if (nativeBigInt.bitLength(b) < bits) {
b = seed;
}
final string = nativeBigInt.toStringMethod(b);
strings.add(string);
totalLength += string.length;
b = nativeBigInt.subtract(
b, nativeBigInt.shiftRight(b, nativeBigInt.eight));
}
}
class ParseJsBigIntBenchmark extends Benchmark {
ParseJsBigIntBenchmark(String name, int bits) : super(name, bits);
@override
void run() {
@ -179,27 +117,16 @@ class ParseJsBigIntBenchmark extends BenchmarkBase {
}
}
class FormatBigIntBenchmark extends BenchmarkBase {
final int bits;
final BigInt seed;
class FormatBigIntBenchmark extends Benchmark {
final List<BigInt> values = [];
FormatBigIntBenchmark(String name, this.bits)
: seed = (BigInt.one << bits) - BigInt.one,
super(name);
FormatBigIntBenchmark(String name, int bits) : super(name, bits);
@override
void setup() {
var b = seed;
var totalLength = 0;
while (totalLength < requiredDigits) {
if (b.bitLength < bits) {
b = seed;
}
final string = b.toString();
for (String s in strings) {
BigInt b = BigInt.parse(s);
values.add(b - BigInt.one); // We add 'one' back later.
totalLength += string.length;
b = b - (b >> 8);
}
}
@ -218,28 +145,16 @@ class FormatBigIntBenchmark extends BenchmarkBase {
}
}
class FormatIntBenchmark extends BenchmarkBase {
final int bits;
final int seed;
class FormatIntBenchmark extends Benchmark {
final List<int> values = [];
FormatIntBenchmark(String name, this.bits)
: seed = (pow(2, bits) as int) - 1,
super(name);
FormatIntBenchmark(String name, int bits) : super(name, bits);
@override
void setup() {
var b = seed;
var totalLength = 0;
int kk = b ~/ 100000;
while (totalLength < requiredDigits) {
if (b.bitLength < bits) {
b = seed - ++kk;
}
final string = b.toString();
values.add(b - 4096); // We add 'one' back later.
totalLength += string.length;
b = b - (b ~/ 256);
for (String s in strings) {
int b = int.parse(s);
values.add(b - 4096); // We add this back later.
}
}
@ -247,7 +162,9 @@ class FormatIntBenchmark extends BenchmarkBase {
void run() {
for (final b0 in values) {
// Instances might cache `toString()`, so use arithmetic to create a new
// instance to try to protect against measuring a cached string.
// instance to try to protect against measuring a cached string. We use
// 4096 to avoid the arithmetic being a no-op due to rounding on web
// integers (i.e. doubles).
final b = b0 + 4096;
final s = b.toString();
sink1 = s;
@ -257,27 +174,16 @@ class FormatIntBenchmark extends BenchmarkBase {
}
}
class FormatInt64Benchmark extends BenchmarkBase {
final int bits;
final Int64 seed;
class FormatInt64Benchmark extends Benchmark {
final List<Int64> values = [];
FormatInt64Benchmark(String name, this.bits)
: seed = (Int64.ONE << bits) - Int64.ONE,
super(name);
FormatInt64Benchmark(String name, int bits) : super(name, bits);
@override
void setup() {
var b = seed;
var totalLength = 0;
while (totalLength < requiredDigits) {
if (int64UnsignedBitLength(b) < bits) {
b = seed;
}
final string = b.toStringUnsigned();
values.add(b - Int64.ONE);
totalLength += string.length;
b = b - b.shiftRightUnsigned(8);
for (String s in strings) {
final b = Int64.parseInt(s);
values.add(b - Int64.ONE); // We add this back later.
}
}
@ -296,32 +202,17 @@ class FormatInt64Benchmark extends BenchmarkBase {
}
}
class FormatJsBigIntBenchmark extends BenchmarkBase {
final int bits;
final Object seed;
class FormatJsBigIntBenchmark extends Benchmark {
final List<Object> values = [];
FormatJsBigIntBenchmark(String name, this.bits)
: seed = nativeBigInt.subtract(
nativeBigInt.shiftLeft(
nativeBigInt.one, nativeBigInt.fromInt(bits)),
nativeBigInt.one),
super(name);
FormatJsBigIntBenchmark(String name, int bits) : super(name, bits);
@override
void setup() {
final one = nativeBigInt.one;
var b = seed;
var totalLength = 0;
while (totalLength < requiredDigits) {
if (nativeBigInt.bitLength(b) < bits) {
b = seed;
}
final string = nativeBigInt.toStringMethod(b);
values.add(nativeBigInt.subtract(b, one)); // We add 'one' back later.
totalLength += string.length;
b = nativeBigInt.subtract(
b, nativeBigInt.shiftRight(b, nativeBigInt.eight));
for (String s in strings) {
final b = nativeBigInt.parse(s);
values.add(nativeBigInt.subtract(b, one)); // We add this back later.
}
}
@ -371,6 +262,9 @@ void main() {
final benchmarks = [
() => ParseIntBenchmark('Int.parse.0009.bits', 9),
() => ParseIntBenchmark('Int.parse.0032.bits', 32),
// Use '63' bits to avoid 64-bit arithmetic overflowing to negative. Keep
// the name as '64' to help comparisons. The effect of an incorrect number
// is reduced since benchmark results are normalized to a 'per digit' score
() => ParseIntBenchmark('Int.parse.0064.bits', 63),
() => ParseInt64Benchmark('Int64.parse.0009.bits', 9),
() => ParseInt64Benchmark('Int64.parse.0032.bits', 32),
@ -389,7 +283,7 @@ void main() {
selectParseNativeBigIntBenchmark('JsBigInt.parse.4096.bits', 4096),
() => FormatIntBenchmark('Int.toString.0009.bits', 9),
() => FormatIntBenchmark('Int.toString.0032.bits', 32),
() => FormatIntBenchmark('Int.toString.0064.bits', 63),
() => FormatIntBenchmark('Int.toString.0064.bits', 63), // '63': See above.
() => FormatInt64Benchmark('Int64.toString.0009.bits', 9),
() => FormatInt64Benchmark('Int64.toString.0032.bits', 32),
() => FormatInt64Benchmark('Int64.toString.0064.bits', 64),

View file

@ -33,29 +33,38 @@ void check(bool sink2isEven) {
// integers.
const requiredDigits = 11106;
class ParseBigIntBenchmark extends BenchmarkBase {
final int bits;
final BigInt seed;
final List<String> strings = [];
ParseBigIntBenchmark(String name, this.bits)
: seed = (BigInt.one << bits) - BigInt.one,
class Benchmark extends BenchmarkBase {
final List<String> strings;
Benchmark(String name, int bits)
: strings = generateStrings(bits),
super(name);
@override
void setup() {
static List<String> generateStrings(int bits) {
List<String> strings = [];
BigInt seed = (BigInt.one << bits) - BigInt.one;
var b = seed;
var restartDelta = BigInt.zero;
var totalLength = 0;
while (totalLength < requiredDigits) {
if (b.bitLength < bits) {
b = seed;
restartDelta += seed >> 20;
restartDelta += BigInt.one;
// Restart from a slighly reduced seed to generate different numbers.
b = seed - restartDelta;
}
final string = b.toString();
strings.add(string);
totalLength += string.length;
b = b - (b >> 8);
var delta = b >> 8;
if (delta == BigInt.zero) delta = BigInt.one;
b = b - delta;
}
return strings;
}
}
class ParseBigIntBenchmark extends Benchmark {
ParseBigIntBenchmark(String name, int bits) : super(name, bits);
@override
void run() {
@ -68,31 +77,8 @@ class ParseBigIntBenchmark extends BenchmarkBase {
}
}
int int64UnsignedBitLength(Int64 i) => i.isNegative ? 64 : i.bitLength;
class ParseInt64Benchmark extends BenchmarkBase {
final int bits;
final Int64 seed;
final List<String> strings = [];
ParseInt64Benchmark(String name, this.bits)
: seed = (Int64.ONE << bits) - Int64.ONE,
super(name);
@override
void setup() {
var b = seed;
var totalLength = 0;
while (totalLength < requiredDigits) {
if (int64UnsignedBitLength(b) < bits) {
b = seed;
}
final string = b.toStringUnsigned();
strings.add(string);
totalLength += string.length;
b = b - b.shiftRightUnsigned(8);
}
}
class ParseInt64Benchmark extends Benchmark {
ParseInt64Benchmark(String name, int bits) : super(name, bits);
@override
void run() {
@ -105,33 +91,22 @@ class ParseInt64Benchmark extends BenchmarkBase {
}
}
class ParseJsBigIntBenchmark extends BenchmarkBase {
final int bits;
final Object seed;
final List<String> strings = [];
ParseJsBigIntBenchmark(String name, this.bits)
: seed = nativeBigInt.subtract(
nativeBigInt.shiftLeft(
nativeBigInt.one, nativeBigInt.fromInt(bits)),
nativeBigInt.one),
super(name);
class ParseIntBenchmark extends Benchmark {
ParseIntBenchmark(String name, int bits) : super(name, bits);
@override
void setup() {
var b = seed;
var totalLength = 0;
while (totalLength < requiredDigits) {
if (nativeBigInt.bitLength(b) < bits) {
b = seed;
}
final string = nativeBigInt.toStringMethod(b);
strings.add(string);
totalLength += string.length;
b = nativeBigInt.subtract(
b, nativeBigInt.shiftRight(b, nativeBigInt.eight));
void run() {
for (final s in strings) {
final b = int.parse(s);
sink1 = s;
sink2 = b;
}
check(sink2.isEven);
}
}
class ParseJsBigIntBenchmark extends Benchmark {
ParseJsBigIntBenchmark(String name, int bits) : super(name, bits);
@override
void run() {
@ -144,27 +119,16 @@ class ParseJsBigIntBenchmark extends BenchmarkBase {
}
}
class FormatBigIntBenchmark extends BenchmarkBase {
final int bits;
final BigInt seed;
class FormatBigIntBenchmark extends Benchmark {
final List<BigInt> values = [];
FormatBigIntBenchmark(String name, this.bits)
: seed = (BigInt.one << bits) - BigInt.one,
super(name);
FormatBigIntBenchmark(String name, int bits) : super(name, bits);
@override
void setup() {
var b = seed;
var totalLength = 0;
while (totalLength < requiredDigits) {
if (b.bitLength < bits) {
b = seed;
}
final string = b.toString();
for (String s in strings) {
BigInt b = BigInt.parse(s);
values.add(b - BigInt.one); // We add 'one' back later.
totalLength += string.length;
b = b - (b >> 8);
}
}
@ -183,27 +147,45 @@ class FormatBigIntBenchmark extends BenchmarkBase {
}
}
class FormatInt64Benchmark extends BenchmarkBase {
final int bits;
final Int64 seed;
final List<Int64> values = [];
class FormatIntBenchmark extends Benchmark {
final List<int> values = [];
FormatInt64Benchmark(String name, this.bits)
: seed = (Int64.ONE << bits) - Int64.ONE,
super(name);
FormatIntBenchmark(String name, int bits) : super(name, bits);
@override
void setup() {
var b = seed;
var totalLength = 0;
while (totalLength < requiredDigits) {
if (int64UnsignedBitLength(b) < bits) {
b = seed;
}
final string = b.toStringUnsigned();
values.add(b - Int64.ONE);
totalLength += string.length;
b = b - b.shiftRightUnsigned(8);
for (String s in strings) {
int b = int.parse(s);
values.add(b - 4096); // We add this back later.
}
}
@override
void run() {
for (final b0 in values) {
// Instances might cache `toString()`, so use arithmetic to create a new
// instance to try to protect against measuring a cached string. We use
// 4096 to avoid the arithmetic being a no-op due to rounding on web
// integers (i.e. doubles).
final b = b0 + 4096;
final s = b.toString();
sink1 = s;
sink2 = b;
}
check(sink2.isEven);
}
}
class FormatInt64Benchmark extends Benchmark {
final List<Int64> values = [];
FormatInt64Benchmark(String name, int bits) : super(name, bits);
@override
void setup() {
for (String s in strings) {
final b = Int64.parseInt(s);
values.add(b - Int64.ONE); // We add this back later.
}
}
@ -222,32 +204,17 @@ class FormatInt64Benchmark extends BenchmarkBase {
}
}
class FormatJsBigIntBenchmark extends BenchmarkBase {
final int bits;
final Object seed;
class FormatJsBigIntBenchmark extends Benchmark {
final List<Object> values = [];
FormatJsBigIntBenchmark(String name, this.bits)
: seed = nativeBigInt.subtract(
nativeBigInt.shiftLeft(
nativeBigInt.one, nativeBigInt.fromInt(bits)),
nativeBigInt.one),
super(name);
FormatJsBigIntBenchmark(String name, int bits) : super(name, bits);
@override
void setup() {
final one = nativeBigInt.one;
var b = seed;
var totalLength = 0;
while (totalLength < requiredDigits) {
if (nativeBigInt.bitLength(b) < bits) {
b = seed;
}
final string = nativeBigInt.toStringMethod(b);
values.add(nativeBigInt.subtract(b, one)); // We add 'one' back later.
totalLength += string.length;
b = nativeBigInt.subtract(
b, nativeBigInt.shiftRight(b, nativeBigInt.eight));
for (String s in strings) {
final b = nativeBigInt.parse(s);
values.add(nativeBigInt.subtract(b, one)); // We add this back later.
}
}
@ -295,6 +262,12 @@ BenchmarkBase Function() selectFormatNativeBigIntBenchmark(
void main() {
final benchmarks = [
() => ParseIntBenchmark('Int.parse.0009.bits', 9),
() => ParseIntBenchmark('Int.parse.0032.bits', 32),
// Use '63' bits to avoid 64-bit arithmetic overflowing to negative. Keep
// the name as '64' to help comparisons. The effect of an incorrect number
// is reduced since benchmark results are normalized to a 'per digit' score
() => ParseIntBenchmark('Int.parse.0064.bits', 63),
() => ParseInt64Benchmark('Int64.parse.0009.bits', 9),
() => ParseInt64Benchmark('Int64.parse.0032.bits', 32),
() => ParseInt64Benchmark('Int64.parse.0064.bits', 64),
@ -310,6 +283,9 @@ void main() {
selectParseNativeBigIntBenchmark('JsBigInt.parse.0256.bits', 256),
selectParseNativeBigIntBenchmark('JsBigInt.parse.1024.bits', 1024),
selectParseNativeBigIntBenchmark('JsBigInt.parse.4096.bits', 4096),
() => FormatIntBenchmark('Int.toString.0009.bits', 9),
() => FormatIntBenchmark('Int.toString.0032.bits', 32),
() => FormatIntBenchmark('Int.toString.0064.bits', 63), // '63': See above.
() => FormatInt64Benchmark('Int64.toString.0009.bits', 9),
() => FormatInt64Benchmark('Int64.toString.0032.bits', 32),
() => FormatInt64Benchmark('Int64.toString.0064.bits', 64),