|
int | add_for_benchmark (int a, int b) |
|
| diag ("infix Call Overhead Benchmark") |
|
| diag ("Iterations: %d", BENCHMARK_ITERATIONS) |
|
| diag ("Target function: int(int, int)") |
|
| for (int i=0;i< BENCHMARK_ITERATIONS;++i) |
|
| diag ("Direct Call Time: %.4f s (%.2f ns/call)", direct_time, direct_ns_per_call) |
|
| if (infix_forward_create_unbound_manual &,,, 2, 2arg_types !=INFIX_SUCCESS) |
|
| diag ("infix (Unbound): %.4f s (%.2f ns/call) -> Overhead: ~%.2f ns", unbound_time, unbound_ns, unbound_ns - direct_ns_per_call) |
|
| if (infix_forward_create_manual(&bound_t, ret_type, arg_types, 2, 2,(void *) add_for_benchmark) !=INFIX_SUCCESS) bail_out("Failed to create bound trampoline") |
|
| diag ("infix (Bound): %.4f s (%.2f ns/call) -> Overhead: ~%.2f ns", bound_time, bound_ns, bound_ns - direct_ns_per_call) |
|
| infix_forward_destroy (unbound_t) |
|
| infix_forward_destroy (bound_t) |
|
| note ("dyncall benchmarking was not enabled.") |
|
| pass ("Benchmark completed (final accumulator value: %d)", accumulator) |
|
A microbenchmark to measure the performance overhead of an FFI call.
Copyright (c) 2025 Sanko Robinson
This source code is dual-licensed under the Artistic License 2.0 or the MIT License. You may choose to use this code under the terms of either license.
SPDX-License-Identifier: (Artistic-2.0 OR MIT)
The documentation blocks within this file are licensed under the Creative Commons Attribution 4.0 International License (CC BY 4.0).
SPDX-License-Identifier: CC-BY-4.0
This is a non-functional test designed to measure the raw speed of the generated FFI trampolines. It should not be run as part of the standard test suite, but rather invoked explicitly when performance analysis is needed.
The test operates in two phases:
- Baseline (Direct Call): It measures the time taken to execute millions of direct, native C function calls to a simple
int add(int, int)
function. This establishes a baseline performance measurement.
- FFI Call: It then generates a forward trampoline for the same function and measures the time taken to execute the same number of calls through the FFI.
The difference between these two measurements, divided by the number of iterations, gives the average per-call overhead of the trampoline mechanism in nanoseconds. This is a critical metric for performance-sensitive applications.
An optional comparison against the dyncall
library can be enabled by defining the DYNCALL_BENCHMARK
macro at compile time.