Skip to content

Commit

Permalink
[benchmarks] Add benchmark for taking tear-offs repeatedly
Browse files Browse the repository at this point in the history
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
alexmarkov authored and Commit Queue committed Feb 7, 2024
1 parent 6ee8a42 commit e7eb0aa
Showing 1 changed file with 192 additions and 0 deletions.
192 changes: 192 additions & 0 deletions benchmarks/TearOff/dart/TearOff.dart
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();
}
}

0 comments on commit e7eb0aa

Please sign in to comment.