Benchmarks
#[builder]
generates code that is easily optimizable by the compiler. This has been tested by the benchmarks below. The benchmarks compare regular positional function call syntax and builder syntax for functions annotated with #[builder]
.
In many cases rustc
generates the same assembly code for the builder syntax as it would for a regular function call. Even when generated assembly differs, the performance differences are negligible.
TIP
Don't take these microbenchmarks for granted. Do your own performance measurements in your application in real conditions. Feel free to open an issue if you find performance problems in bon
.
Wallclock statistics
Benchmark | Description | Assembly output | Run time |
---|---|---|---|
args_3 | 3 args of primitive types | Equal | regular: 6.6536ns builder: 6.6494ns |
args_5 | 5 args of primitive types | Equal | regular: 7.9592ns builder: 7.9731ns |
args_10 | 10 args of primitive types | Ordering diff | regular: 18.082ns builder: 18.217ns |
args_10_structs | 10 args of primitive types and structs | Equal | regular: 5.0784ns builder: 5.0481ns |
args_10_alloc | 10 args of primitive and heap-allocated types | Instructions diff | regular: 86.090ns builder: 86.790ns |
args_20 | 20 args of primitive types | Ordering diff | regular: 37.381ns builder: 37.623ns |
High-precision statistics
Benchmark | Instructions count | L1 accesses | L2 accesses | RAM accesses |
---|---|---|---|---|
args_3 | regular: 106 builder: 106 | regular: 136 builder: 136 | regular: 2 builder: 2 | regular: 4 builder: 4 |
args_5 | regular: 124 builder: 124 | regular: 161 builder: 161 | regular: 1 builder: 1 | regular: 9 builder: 9 |
args_10 | regular: 281 builder: 281 | regular: 381 builder: 380 | regular: 2 builder: 2 | regular: 19 builder: 20 |
args_10_structs | regular: 73 builder: 73 | regular: 108 builder: 108 | regular: 2 builder: 2 | regular: 10 builder: 10 |
args_10_alloc | regular: 2025 builder: 2026 | regular: 2823 builder: 2823 | regular: 3 builder: 2 | regular: 35 builder: 35 |
args_20 | regular: 554 builder: 554 | regular: 768 builder: 769 | regular: 3 builder: 3 | regular: 34 builder: 33 |
Conditions
The code was compiled with opt-level = 3
and debug = 0
.
Hardware
The benchmarks were run on a dedicated root server AX51-NVMe
on Hetzner.
- OS: Ubuntu 22.04.4 (Linux 5.15.0-76-generic)
- CPU: AMD Ryzen 7 3700X 8-Core Processor (x86_64)
- RAM: 62.8 GiB
References
The source code of the benchmarks is available here.