Skip to content

Instantly share code, notes, and snippets.

@alxhub
Last active December 18, 2015 09:29
Show Gist options
  • Save alxhub/5761424 to your computer and use it in GitHub Desktop.
Save alxhub/5761424 to your computer and use it in GitHub Desktop.
Benchmarks of native method dispatch, noSuchMethod dispatch, MirrorSystem.getName and InstanceMirror.delegate.
import 'package:benchmark_harness/benchmark_harness.dart';
import 'dart:mirrors';
/// Contains a basic method call.
class Foo {
method() => 1 + 1;
}
/// No different than [Foo] except Bar.method() is resolved via [noSuchMethod].
class Bar {
noSuchMethod(Invocation i) => 1 + 1;
}
/// Used to steal an [Invocation] for later repeated testing of code which
/// requires one.
class InvocationSpy {
noSuchMethod(Invocation i) => i;
}
class DispatchBenchmark extends BenchmarkBase {
final Foo foo = new Foo();
const DispatchBenchmark() : super("Method dispatch");
static main() {
new DispatchBenchmark().report();
}
void run() {
foo.method();
}
}
class NoSuchMethodBenchmark extends BenchmarkBase {
final Bar bar = new Bar();
const NoSuchMethodBenchmark() : super("noSuchMethod-based dispatch");
static main() {
new NoSuchMethodBenchmark().report();
}
void run() {
bar.method();
}
}
class InvocationNameBenchmark extends BenchmarkBase {
Invocation invocation;
InvocationNameBenchmark() : super("MirrorSystem.getName(Invocation.memberName)");
static main() {
new InvocationNameBenchmark().report();
}
void run() {
MirrorSystem.getName(invocation.memberName);
}
void setup() {
invocation = new InvocationSpy().method();
}
}
class DelegateBenchmark extends BenchmarkBase {
Invocation invocation;
final InstanceMirror mirror = reflect(new Foo());
DelegateBenchmark() : super("InstanceMirror.delegate(Invocation) dispatch");
static main() {
new DelegateBenchmark().report();
}
void run() {
mirror.delegate(invocation);
}
void setup() {
invocation = new InvocationSpy().method();
}
}
main() {
DispatchBenchmark.main();
NoSuchMethodBenchmark.main();
InvocationNameBenchmark.main();
DelegateBenchmark.main();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment