Rust Runtime
Technical deep-dive on how the .polybench Rust runtime project works
This page documents how the .polybench/runtime-env/rust/ project is set up, how codegen works, and how execution is performed.
The Rust runtime uses a Cargo project. The generated benchmark code overwrites src/main.rs. The binary is built with cargo build --release and must be named polybench_runner. The runtime crate is runtimes-rust; templates come from poly-bench-project.
When you run poly-bench init with Rust enabled, poly-bench creates the runtime environment and writes the scaffold. Specifically:
.polybench/runtime-env/rust/ and src/Cargo.toml with package name polybench_runner, edition 2021, and default deps: serde, serde_json, alloc_trackersrc/main.rs with a minimal fn main() {} placeholderNo cargo fetch or build runs at init. Dependencies are fetched when you run poly-bench build or poly-bench install.
cargo fetch or build runs at init. Dependencies are fetched when you run poly-bench build or poly-bench install.When you run poly-bench build or poly-bench install:
Cargo.toml if --force is usedalloc_tracker is in Cargo.toml for memory benchmarks (added automatically if missing)[rust] dependencies via cargo add (with optional --features for crates like tiny_keccak)cargo fetch to download dependencies (skipped with --skip-install)main.rs — Codegen produces a complete main() with fixture decoding, init, helpers, and the benchmark loop. The placeholder exists only so Cargo can build before the first run; this avoids maintaining a separate bench_standalone.rs.polybench_runner — The executor expects this exact name. Changing it in Cargo.toml would break execution.[workspace] in Cargo.toml — Marks this as a standalone workspace so it is not accidentally included in a parent workspace.alloc_tracker by default — Templates include it so memory benchmarks work out of the box; the executor adds it dynamically if a memory suite runs and it is missing.opt-level = 3, lto = true, codegen-units = 1 for maximum performance.When you run poly-bench init --languages rust or poly-bench build, the following is created in .polybench/runtime-env/rust/:
| File | Source | Purpose |
|---|---|---|
Cargo.toml | templates.cargo_toml() | Package name, edition 2021, binary name polybench_runner |
src/main.rs | Placeholder | Overwritten by codegen at run time |
The Cargo.toml declares a binary target named polybench_runner. Dependencies added via poly-bench add --rs are written to Cargo.toml via cargo add.
When poly-bench run executes a benchmark, the Rust runtime:
generate_standalone_benchmark(spec, suite) in runtimes-rust/src/codegen.rsmain.rs with: fn main(), fixture decoding, setup/init code, helper functions, and the benchmark loopsrc/main.rs (overwriting the placeholder)For memory benchmarks, the executor automatically adds alloc_tracker = "0.5" to Cargo.toml via ensure_alloc_tracker_in_cargo_toml if not present.
Generated files:
src/main.rs — overwritten with full benchmark harness| Step | Command | Output |
|---|---|---|
| Precompile | cargo build --release | target/release/polybench_runner |
| Check | cargo build on generated main.rs | Compilation only |
The executor caches the binary path and source hash. LSP virtual files in src/bin/ are cleaned up before compilation to avoid conflicts.
src/main.rs, run cargo build --release, cache binary path./target/release/polybench_runner <benchmark_name> <iterations> as a subprocessalloc_tracker = "0.5" is automatically added to Cargo.toml when needed.polybench_runner; do not change it in Cargo.toml.alloc_tracker = "0.5" is automatically added to Cargo.toml when needed.See Requirements — Rust for user-facing setup and the --features flag.