From 0d0fa52eaeeb9a48eccd0a460e76ea303cc720e2 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 5 Sep 2026 08:43:42 +0000 Subject: [PATCH] macros: evaluate turso_debug_assert conditions only in debug builds turso_debug_assert! bound its condition to a local before handing it to debug_assert!, so release builds still evaluated every condition and only skipped the check. Conditions that call something fallible, like `matches!(self.page_type(), Ok(PageType::TableLeaf))` in the cell readers, cannot be optimized away because the error path builds a LimboError, so every rowid read and every interior-cell probe of a b-tree descent paid for a page type conversion it never used. Put the condition inside debug_assert!, where it sits under `if cfg!(debug_assertions)` and disappears from release code. The antithesis build keeps the bound local, which it needs for its own report. Measured with callgrind, instructions per execution: SELECT * FROM t WHERE id = 12345: 7,878 -> 7,532 (-4.4%) SELECT id FROM t WHERE value = 500 LIMIT 1: 13,527 -> 13,605 (+0.6%) SELECT 1: 1,801 -> 1,813 (+0.7%) and per row of SELECT * FROM t, the rowid read no longer converts the page type: BTreeCursor functions 109,832,094 -> 106,632,094 over 400k rows (-8 per row). The other two statements and the whole-scan totals move by +-1% with the changed inlining inside Program::step, which this change does not touch. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01Feji9LAGhf6W4U2BBE6aWs --- macros/src/lib.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/macros/src/lib.rs b/macros/src/lib.rs index dfd7e4aa5..fd2dc4021 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -763,11 +763,14 @@ fn emit_condition_assert( let details = details_json(&input.details); let fmt_args = details_format_args(&msg, &input.details); + // Without the antithesis cfg, a debug assertion must not evaluate its + // condition in release builds: `debug_assert!` keeps the expression + // inside the `if cfg!(debug_assertions)` block, so the compiler can drop + // it together with the check. Binding it to `__turso_cond` first would + // keep every condition with a fallible or non-trivial callee alive. let assert_call = match kind { - ConditionAssertKind::Assert => quote! { assert!(__turso_cond, #fmt_args); }, - ConditionAssertKind::DebugAssert => { - quote! { debug_assert!(__turso_cond, #fmt_args); } - } + ConditionAssertKind::Assert => quote! { assert!(#cond, #fmt_args); }, + ConditionAssertKind::DebugAssert => quote! { debug_assert!(#cond, #fmt_args); }, }; let exit_msg = quote! { eprint!("[antithesis] assertion failed: "); @@ -778,9 +781,9 @@ fn emit_condition_assert( let env_check = antithesis_env_check(); quote! { { - let __turso_cond = #cond; #[cfg(antithesis)] { + let __turso_cond = #cond; #env_check antithesis_sdk::assert_always_or_unreachable!(__turso_cond, #prefixed, #details); if !__turso_cond {