From d1e76c6ea0aa6553e1235c8b31289a040aa6ded0 Mon Sep 17 00:00:00 2001 From: Alexander Aprelev Date: Wed, 16 Nov 2022 18:32:07 +0000 Subject: [PATCH] [vm/benchmark] Improve LongStringCompare benchmark. Follow-up to address comments on https://dart-review.git.corp.google.com/c/sdk/+/269380 BUG=https://github.com/dart-lang/sdk/issues/50190 TEST=ci Change-Id: I5a979a4504e205f493ee71c74d405f1b65246781 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/269602 Reviewed-by: Stephen Adams Commit-Queue: Alexander Aprelev --- .../StringCompare/dart/LongStringCompare.dart | 42 ++++++++++--------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/benchmarks/StringCompare/dart/LongStringCompare.dart b/benchmarks/StringCompare/dart/LongStringCompare.dart index 32ea4b3bd2b..f30dbc2d2fa 100644 --- a/benchmarks/StringCompare/dart/LongStringCompare.dart +++ b/benchmarks/StringCompare/dart/LongStringCompare.dart @@ -6,41 +6,45 @@ import 'package:benchmark_harness/benchmark_harness.dart'; -class LongStringCompare extends BenchmarkBase { - late String s, t; +int equalCount = 0; - String generateLongString() { - var s = "abcdefgh"; - for (int i = 0; i < 20; i++) { - s = "$s ghijkl $s"; - } - return s; +class LongStringCompare extends BenchmarkBase { + final int reps; + final List s = []; + + String generateLongString(int lengthPower) { + return "abc" * (1 << lengthPower) + "def"; } - LongStringCompare() : super('LongStringCompare') { - s = generateLongString(); - t = s; - // Difference in two strings goes in the middle. - s += "." + s; - t += "!" + t; + LongStringCompare(int lengthPower, this.reps) + : super('LongStringCompare.${1 << lengthPower}.${reps}reps') { + final single = generateLongString(lengthPower); + s.add(single + "." + single); + s.add(single + "!" + single); } @override void warmup() { - for (int i = 0; i < 4; i++) { + for (int i = 0; i < reps / 2; i++) { run(); } } @override void run() { - bool b = true; - for (int i = 0; i < 5; i++) { - b &= (s == t); + for (int i = 0; i < reps; i++) { + // Make string comparison code hoisiting harder for the compiler to do. + bool comparison = s[i % 2] == s[(i + 1) % 2]; + if (comparison) { + equalCount++; + } } } } void main() { - LongStringCompare().report(); + LongStringCompare(1, 3000).report(); + LongStringCompare(5, 1000).report(); + LongStringCompare(10, 30).report(); + if (equalCount > 0) throw StateError("Unexpected equalCount: $equalCount"); }