|
| 1 | +package; |
| 2 | + |
| 3 | +/** |
| 4 | + CFFI calls from native code, the way an extension library makes them. |
| 5 | + The entry points are looked up through hx_cffi once, untimed; both |
| 6 | + runtimes export the same ones. |
| 7 | +**/ |
| 8 | +@:cppFileCode(' |
| 9 | +extern "C" void *hx_cffi(const char *inName); |
| 10 | +typedef hx::Object *(*BenchCallN)(hx::Object *, hx::Object **, int); |
| 11 | +typedef const char *(*BenchValString)(hx::Object *); |
| 12 | + |
| 13 | +static BenchCallN bench_callN = 0; |
| 14 | +static BenchValString bench_val_string = 0; |
| 15 | + |
| 16 | +static int bench_call_vector(Dynamic func, Dynamic a, Dynamic b, Dynamic c, int count) |
| 17 | +{ |
| 18 | + if (!bench_callN) |
| 19 | + bench_callN = (BenchCallN)hx_cffi("val_callN"); |
| 20 | + hx::Object *args[3] = { a.mPtr, b.mPtr, c.mPtr }; |
| 21 | + int sum = 0; |
| 22 | + for(int i=0;i<count;i++) |
| 23 | + sum += (int)Dynamic(bench_callN(func.mPtr, args, 3)); |
| 24 | + return sum; |
| 25 | +} |
| 26 | + |
| 27 | +static int bench_string_views(Dynamic str, int count) |
| 28 | +{ |
| 29 | + if (!bench_val_string) |
| 30 | + bench_val_string = (BenchValString)hx_cffi("val_string"); |
| 31 | + int sum = 0; |
| 32 | + for(int i=0;i<count;i++) |
| 33 | + { |
| 34 | + const unsigned char *s = (const unsigned char *)bench_val_string(str.mPtr); |
| 35 | + sum = sum * 31 + s[0] + s[1]; |
| 36 | + } |
| 37 | + return sum; |
| 38 | +} |
| 39 | +') |
| 40 | +class CffiNative { |
| 41 | + public static function callVector(func:Dynamic, a:Dynamic, b:Dynamic, c:Dynamic, count:Int):Int |
| 42 | + return untyped __cpp__('bench_call_vector({0}, {1}, {2}, {3}, {4})', func, a, b, c, count); |
| 43 | + |
| 44 | + public static function stringViews(str:Dynamic, count:Int):Int |
| 45 | + return untyped __cpp__('bench_string_views({0}, {1})', str, count); |
| 46 | +} |
| 47 | + |
| 48 | +/** val_callN with three arguments, the uniform call a CFFI library makes. **/ |
| 49 | +class CffiCallVector extends Benchmark { |
| 50 | + static inline var N = 1000000; |
| 51 | + |
| 52 | + final func:Dynamic = (a:Int, b:Int, c:Int) -> a + b + c; |
| 53 | + |
| 54 | + public function new() { |
| 55 | + super("cffi_call_vector", "calls", N); |
| 56 | + } |
| 57 | + |
| 58 | + override public function run():Int { |
| 59 | + // Small Ints are shared boxes, so no allocation per argument is timed |
| 60 | + return CffiNative.callVector(func, 1, 2, 3, N); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/** val_string on one boxed non-ASCII string, as a library reading the same value again. **/ |
| 65 | +class CffiStringView extends Benchmark { |
| 66 | + static inline var N = 256; |
| 67 | + |
| 68 | + var str:Dynamic; |
| 69 | + |
| 70 | + public function new() { |
| 71 | + super("cffi_string_view", "string views", N); |
| 72 | + } |
| 73 | + |
| 74 | + override public function setup() { |
| 75 | + var b = new StringBuf(); |
| 76 | + for (i in 0...65536) |
| 77 | + b.addChar(0x3bb); |
| 78 | + str = b.toString(); |
| 79 | + } |
| 80 | + |
| 81 | + override public function run():Int { |
| 82 | + return CffiNative.stringViews(str, N); |
| 83 | + } |
| 84 | +} |
0 commit comments