Last active
December 18, 2015 09:29
-
-
Save alxhub/5761424 to your computer and use it in GitHub Desktop.
Benchmarks of native method dispatch, noSuchMethod dispatch, MirrorSystem.getName and InstanceMirror.delegate.
This file contains hidden or 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
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