Skip to content

Commit

Permalink
[benchmarks/ffi] Add FfiCall benchmark with Handles
Browse files Browse the repository at this point in the history
On x64 desktop in JIT the trampolines with Handles are a bit slower
than trampolines passing integers or Pointers.

FfiCall.Uint64x01(RunTime): 200.8482627033541 us.
FfiCall.PointerUint8x01(RunTime): 261.3910088865656 us.
FfiCall.Handlex01(RunTime): 355.4978670458585 us.
FfiCall.Handlex02(RunTime): 384.86376755820663 us.
FfiCall.Handlex04(RunTime): 492.896007885658 us.
FfiCall.Handlex10(RunTime): 846.1214043993232 us.
FfiCall.Handlex20(RunTime): 1193.412291169451 us.

Issue: #36858
Issue: #41319

New issue for optimizing multiple handles:
#42341

Also cleans up dart2/native which was erroneously left over from the
benchmark duplication for NNBD.

Change-Id: I81223fefc47129d00984492efb9502d5449f0d4a
Cq-Include-Trybots: luci.dart.try:benchmark-linux-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/145593
Commit-Queue: Daco Harkes <dacoharkes@google.com>
Reviewed-by: Jonas Termansen <sortie@google.com>
  • Loading branch information
dcharkes authored and commit-bot@chromium.org committed Jun 15, 2020
1 parent 28606d0 commit d870463
Show file tree
Hide file tree
Showing 8 changed files with 514 additions and 202 deletions.
2 changes: 1 addition & 1 deletion DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ deps = {
"packages": [
{
"package": "dart/benchmarks/fficall",
"version": "_AKfaNTE0blKZwinuNMETidy44AK5b-u_rXmdBpat9UC",
"version": "ebF5aRXKDananlaN4Y8b0bbCNHT1MnkGbWqfpCpiND4C",
},
],
"dep_type": "cipd",
Expand Down
232 changes: 232 additions & 0 deletions benchmarks/FfiCall/dart/FfiCall.dart
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,67 @@ typedef Function20PointerUint8 = Pointer<Uint8> Function(
Function20PointerUint8 function20PointerUint8 = ffiTestFunctions.lookupFunction<
Function20PointerUint8, Function20PointerUint8>("Function20PointerUint8");

final function1handle = ffiTestFunctions.lookupFunction<Handle Function(Handle),
Object Function(Object)>("Function1Handle");

final function2handle = ffiTestFunctions.lookupFunction<
Handle Function(Handle, Handle),
Object Function(Object, Object)>("Function2Handle");

final function4handle = ffiTestFunctions.lookupFunction<
Handle Function(Handle, Handle, Handle, Handle),
Object Function(Object, Object, Object, Object)>("Function4Handle");

final function10handle = ffiTestFunctions.lookupFunction<
Handle Function(Handle, Handle, Handle, Handle, Handle, Handle, Handle,
Handle, Handle, Handle),
Object Function(Object, Object, Object, Object, Object, Object, Object,
Object, Object, Object)>("Function10Handle");

final function20handle = ffiTestFunctions.lookupFunction<
Handle Function(
Handle,
Handle,
Handle,
Handle,
Handle,
Handle,
Handle,
Handle,
Handle,
Handle,
Handle,
Handle,
Handle,
Handle,
Handle,
Handle,
Handle,
Handle,
Handle,
Handle),
Object Function(
Object,
Object,
Object,
Object,
Object,
Object,
Object,
Object,
Object,
Object,
Object,
Object,
Object,
Object,
Object,
Object,
Object,
Object,
Object,
Object)>("Function20Handle");

//
// Trampoline call.
//
Expand Down Expand Up @@ -597,6 +658,69 @@ Pointer<Uint8> doCall20PointerUint8(
return x;
}

Object doCall1Handle(int length, Object p1) {
Object x = p1;
for (int i = 0; i < length; i++) {
x = function1handle(x);
}
return x;
}

Object doCall2Handle(int length, Object p1, Object p2) {
Object x = p1;
for (int i = 0; i < length; i++) {
x = function2handle(x, p2);
}
return x;
}

Object doCall4Handle(int length, Object p1, Object p2, Object p3, Object p4) {
Object x = p1;
for (int i = 0; i < length; i++) {
x = function4handle(x, p2, p3, p4);
}
return x;
}

Object doCall10Handle(int length, Object p1, Object p2, Object p3, Object p4,
Object p5, Object p6, Object p7, Object p8, Object p9, Object p10) {
Object x = p1;
for (int i = 0; i < length; i++) {
x = function10handle(x, p2, p3, p4, p5, p6, p7, p8, p9, p10);
}
return x;
}

Object doCall20Handle(
int length,
Object p1,
Object p2,
Object p3,
Object p4,
Object p5,
Object p6,
Object p7,
Object p8,
Object p9,
Object p10,
Object p11,
Object p12,
Object p13,
Object p14,
Object p15,
Object p16,
Object p17,
Object p18,
Object p19,
Object p20) {
Object x = p1;
for (int i = 0; i < length; i++) {
x = function20handle(x, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13,
p14, p15, p16, p17, p18, p19, p20);
}
return x;
}

//
// Benchmark fixtures.
//
Expand Down Expand Up @@ -1106,6 +1230,109 @@ class PointerUint8x20 extends BenchmarkBase {
}
}

class MyClass {
int a;
MyClass(this.a);
}

class Handlex01 extends BenchmarkBase {
Handlex01() : super("FfiCall.Handlex01");

void run() {
final p1 = MyClass(123);
final x = doCall1Handle(N, p1);

if (!identical(p1, x)) {
throw Exception("$name: Unexpected result: $x");
}
}
}

class Handlex02 extends BenchmarkBase {
Handlex02() : super("FfiCall.Handlex02");

void run() {
final p1 = MyClass(123);
final p2 = MyClass(2);
final x = doCall2Handle(N, p1, p2);

if (!identical(p1, x)) {
throw Exception("$name: Unexpected result: $x");
}
}
}

class Handlex04 extends BenchmarkBase {
Handlex04() : super("FfiCall.Handlex04");

void run() {
final p1 = MyClass(123);
final p2 = MyClass(2);
final p3 = MyClass(3);
final p4 = MyClass(4);
final x = doCall4Handle(N, p1, p2, p3, p4);

if (!identical(p1, x)) {
throw Exception("$name: Unexpected result: $x");
}
}
}

class Handlex10 extends BenchmarkBase {
Handlex10() : super("FfiCall.Handlex10");

void run() {
final p1 = MyClass(123);
final p2 = MyClass(2);
final p3 = MyClass(3);
final p4 = MyClass(4);
final p5 = MyClass(5);
final p6 = MyClass(6);
final p7 = MyClass(7);
final p8 = MyClass(8);
final p9 = MyClass(9);
final p10 = MyClass(10);
final x = doCall10Handle(N, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10);

if (!identical(p1, x)) {
throw Exception("$name: Unexpected result: $x");
}
}
}

class Handlex20 extends BenchmarkBase {
Handlex20() : super("FfiCall.Handlex20");

void run() {
final p1 = MyClass(123);
final p2 = MyClass(2);
final p3 = MyClass(3);
final p4 = MyClass(4);
final p5 = MyClass(5);
final p6 = MyClass(6);
final p7 = MyClass(7);
final p8 = MyClass(8);
final p9 = MyClass(9);
final p10 = MyClass(10);
final p11 = MyClass(11);
final p12 = MyClass(12);
final p13 = MyClass(13);
final p14 = MyClass(14);
final p15 = MyClass(15);
final p16 = MyClass(16);
final p17 = MyClass(17);
final p18 = MyClass(18);
final p19 = MyClass(19);
final p20 = MyClass(20);
final x = doCall20Handle(N, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11,
p12, p13, p14, p15, p16, p17, p18, p19, p20);

if (!identical(p1, x)) {
throw Exception("$name: Unexpected result: $x");
}
}
}

//
// Main driver.
//
Expand Down Expand Up @@ -1144,6 +1371,11 @@ main() {
() => PointerUint8x04(),
() => PointerUint8x10(),
() => PointerUint8x20(),
() => Handlex01(),
() => Handlex02(),
() => Handlex04(),
() => Handlex10(),
() => Handlex20(),
];
benchmarks.forEach((benchmark) => benchmark().report());
}
Loading

0 comments on commit d870463

Please sign in to comment.