Skip to content

Commit ddd4335

Browse files
Copilotludfjig
authored andcommitted
Replace debug assertions check with optimization level check for benchmarks
Co-authored-by: ludfjig <4257730+ludfjig@users.noreply.github.com>
1 parent 9634538 commit ddd4335

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/hyperlight_host/benches/benchmarks.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
// Benchmarks are only meaningful and should only run with release builds.
18-
// Debug builds have different performance characteristics and would not provide
17+
// Benchmarks are only meaningful and should only run with optimized builds.
18+
// Unoptimized builds have different performance characteristics and would not provide
1919
// useful benchmarking data for performance regression testing.
20-
#[cfg(debug_assertions)]
20+
#[cfg(unoptimized_build)]
2121
compile_error!(
22-
"Benchmarks must be run with release builds only. Use `cargo bench --release` or `just bench`."
22+
"Benchmarks must be run with optimized builds only. Use `cargo bench --release` or `just bench`."
2323
);
2424

2525
use criterion::{Criterion, criterion_group, criterion_main};

src/hyperlight_host/build.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,24 @@ fn main() -> Result<()> {
8585
);
8686
}
8787

88+
// Set a cfg flag based on optimization level for benchmarks
89+
// Benchmarks should only run with optimized builds (opt-level 1+)
90+
println!("cargo:rustc-check-cfg=cfg(unoptimized_build)");
91+
println!("cargo:rustc-check-cfg=cfg(optimized_build)");
92+
93+
if let Ok(opt_level) = std::env::var("OPT_LEVEL") {
94+
if opt_level == "0" {
95+
// Unoptimized build - benchmarks should not run
96+
println!("cargo:rustc-cfg=unoptimized_build");
97+
} else {
98+
// Optimized build - benchmarks can run
99+
println!("cargo:rustc-cfg=optimized_build");
100+
}
101+
} else {
102+
// Fallback: if we can't determine opt level, assume unoptimized to be safe
103+
println!("cargo:rustc-cfg=unoptimized_build");
104+
}
105+
88106
// Makes #[cfg(kvm)] == #[cfg(all(feature = "kvm", target_os = "linux"))]
89107
// and #[cfg(mshv)] == #[cfg(all(any(feature = "mshv2", feature = "mshv3"), target_os = "linux"))].
90108
// Essentially the kvm and mshv features are ignored on windows as long as you use #[cfg(kvm)] and not #[cfg(feature = "kvm")].

0 commit comments

Comments
 (0)