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.6536nsbuilder: 6.6494ns |
args_5 | 5 args of primitive types | Equal | regular: 7.9592nsbuilder: 7.9731ns |
args_10 | 10 args of primitive types | Ordering diff | regular: 18.082nsbuilder: 18.217ns |
args_10_structs | 10 args of primitive types and structs | Equal | regular: 5.0784nsbuilder: 5.0481ns |
args_10_alloc | 10 args of primitive and heap-allocated types | Instructions diff | regular: 86.090nsbuilder: 86.790ns |
args_20 | 20 args of primitive types | Ordering diff | regular: 37.381nsbuilder: 37.623ns |
High-precision statistics
| Benchmark | Instructions count | L1 accesses | L2 accesses | RAM accesses |
|---|---|---|---|---|
args_3 | regular: 106builder: 106 | regular: 136builder: 136 | regular: 2builder: 2 | regular: 4builder: 4 |
args_5 | regular: 124builder: 124 | regular: 161builder: 161 | regular: 1builder: 1 | regular: 9builder: 9 |
args_10 | regular: 281builder: 281 | regular: 381builder: 380 | regular: 2builder: 2 | regular: 19builder: 20 |
args_10_structs | regular: 73builder: 73 | regular: 108builder: 108 | regular: 2builder: 2 | regular: 10builder: 10 |
args_10_alloc | regular: 2025builder: 2026 | regular: 2823builder: 2823 | regular: 3builder: 2 | regular: 35builder: 35 |
args_20 | regular: 554builder: 554 | regular: 768builder: 769 | regular: 3builder: 3 | regular: 34builder: 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.