-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[benchmarks] Add benchmark for taking tear-offs repeatedly
Issue: #54808 Change-Id: Ice28033bab71e30f9a586564d7e55d9c4f5ce770 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/350640 Reviewed-by: Martin Kustermann <[email protected]> Commit-Queue: Alexander Markov <[email protected]>
- Loading branch information
1 parent
6ee8a42
commit e7eb0aa
Showing
1 changed file
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:benchmark_harness/benchmark_harness.dart'; | ||
|
||
// Micro-benchmark for taking tear-off of instance method multiple | ||
// times and in a loop. | ||
|
||
class BenchTearOffInlined extends BenchmarkBase { | ||
BenchTearOffInlined() : super('TearOff.Inlined'); | ||
|
||
int sum = 0; | ||
|
||
@pragma('vm:prefer-inline') | ||
@pragma('dart2js:prefer-inline') | ||
void foo(int arg) { | ||
sum += arg; | ||
} | ||
|
||
@pragma('vm:prefer-inline') | ||
@pragma('dart2js:prefer-inline') | ||
void callIt(void Function(int) func, int arg) { | ||
func(arg); | ||
} | ||
|
||
@override | ||
@pragma('vm:never-inline') | ||
@pragma('dart2js:never-inline') | ||
void run() { | ||
sum = 0; | ||
callIt(foo, 1); | ||
callIt(foo, 2); | ||
callIt(foo, 3); | ||
callIt(foo, 4); | ||
callIt(foo, 5); | ||
for (int i = 11; i < 20; ++i) { | ||
callIt(foo, i); | ||
} | ||
callIt(foo, 6); | ||
callIt(foo, 7); | ||
callIt(foo, 8); | ||
callIt(foo, 9); | ||
callIt(foo, 10); | ||
|
||
const int expectedSum = 20 * (20 - 1) ~/ 2; | ||
if (sum != expectedSum) throw 'Bad result: $sum'; | ||
} | ||
} | ||
|
||
class BenchTearOffInlinedInTry extends BenchmarkBase { | ||
BenchTearOffInlinedInTry() : super('TearOff.Inlined.InTry'); | ||
|
||
int sum = 0; | ||
|
||
@pragma('vm:prefer-inline') | ||
@pragma('dart2js:prefer-inline') | ||
void foo(int arg) { | ||
sum += arg; | ||
} | ||
|
||
@pragma('vm:prefer-inline') | ||
@pragma('dart2js:prefer-inline') | ||
void callIt(void Function(int) func, int arg) { | ||
func(arg); | ||
} | ||
|
||
@override | ||
@pragma('vm:never-inline') | ||
@pragma('dart2js:never-inline') | ||
void run() { | ||
sum = 0; | ||
try { | ||
callIt(foo, 1); | ||
callIt(foo, 2); | ||
callIt(foo, 3); | ||
callIt(foo, 4); | ||
callIt(foo, 5); | ||
for (int i = 11; i < 20; ++i) { | ||
callIt(foo, i); | ||
} | ||
callIt(foo, 6); | ||
callIt(foo, 7); | ||
callIt(foo, 8); | ||
callIt(foo, 9); | ||
callIt(foo, 10); | ||
} finally { | ||
const int expectedSum = 20 * (20 - 1) ~/ 2; | ||
if (sum != expectedSum) throw 'Bad result: $sum'; | ||
} | ||
} | ||
} | ||
|
||
class BenchTearOffNotInlined extends BenchmarkBase { | ||
BenchTearOffNotInlined() : super('TearOff.NotInlined'); | ||
|
||
int sum = 0; | ||
|
||
@pragma('vm:never-inline') | ||
@pragma('dart2js:never-inline') | ||
void foo(int arg) { | ||
sum += arg; | ||
} | ||
|
||
@pragma('vm:never-inline') | ||
@pragma('dart2js:never-inline') | ||
void callIt(void Function(int) func, int arg) { | ||
func(arg); | ||
} | ||
|
||
@override | ||
@pragma('vm:never-inline') | ||
@pragma('dart2js:never-inline') | ||
void run() { | ||
sum = 0; | ||
callIt(foo, 1); | ||
callIt(foo, 2); | ||
callIt(foo, 3); | ||
callIt(foo, 4); | ||
callIt(foo, 5); | ||
for (int i = 11; i < 20; ++i) { | ||
callIt(foo, i); | ||
} | ||
callIt(foo, 6); | ||
callIt(foo, 7); | ||
callIt(foo, 8); | ||
callIt(foo, 9); | ||
callIt(foo, 10); | ||
|
||
const int expectedSum = 20 * (20 - 1) ~/ 2; | ||
if (sum != expectedSum) throw 'Bad result: $sum'; | ||
} | ||
} | ||
|
||
class BenchTearOffNotInlinedInTry extends BenchmarkBase { | ||
BenchTearOffNotInlinedInTry() : super('TearOff.NotInlined.InTry'); | ||
|
||
int sum = 0; | ||
|
||
@pragma('vm:never-inline') | ||
@pragma('dart2js:never-inline') | ||
void foo(int arg) { | ||
sum += arg; | ||
} | ||
|
||
@pragma('vm:never-inline') | ||
@pragma('dart2js:never-inline') | ||
void callIt(void Function(int) func, int arg) { | ||
func(arg); | ||
} | ||
|
||
@override | ||
@pragma('vm:never-inline') | ||
@pragma('dart2js:never-inline') | ||
void run() { | ||
sum = 0; | ||
try { | ||
callIt(foo, 1); | ||
callIt(foo, 2); | ||
callIt(foo, 3); | ||
callIt(foo, 4); | ||
callIt(foo, 5); | ||
for (int i = 11; i < 20; ++i) { | ||
callIt(foo, i); | ||
} | ||
callIt(foo, 6); | ||
callIt(foo, 7); | ||
callIt(foo, 8); | ||
callIt(foo, 9); | ||
callIt(foo, 10); | ||
} finally { | ||
const int expectedSum = 20 * (20 - 1) ~/ 2; | ||
if (sum != expectedSum) throw 'Bad result: $sum'; | ||
} | ||
} | ||
} | ||
|
||
void main() { | ||
final benchmarks = [ | ||
BenchTearOffInlined(), | ||
BenchTearOffInlinedInTry(), | ||
BenchTearOffNotInlined(), | ||
BenchTearOffNotInlinedInTry(), | ||
]; | ||
|
||
for (final benchmark in benchmarks) { | ||
benchmark.warmup(); | ||
} | ||
for (final benchmark in benchmarks) { | ||
benchmark.report(); | ||
} | ||
} |