ckb-std icon indicating copy to clipboard operation
ckb-std copied to clipboard

Disable `debug_assertions` won't ingore function calls in `debug!(..)` statements.

Open chaoticlonghair opened this issue 10 months ago • 9 comments

Issue

I have the following code:

#[cfg(debug_assertions)]
use ckb_std::ckb_types::prelude::*;

debug!("script hash = {:#x}", script_hash.pack());

When set debug_assertions = false, the above could NOT be compiled.

Description

When users don't want to print the debug messages, all functions in debug!(..), such as script_hash.pack() in above code, should be ignored and they should never be executed.

https://github.com/nervosnetwork/ckb-std/blob/676455542258235a22f6f443b18e4b4d887a661a/src/debug.rs#L38-L44

chaoticlonghair avatar Apr 16 '24 09:04 chaoticlonghair

It's due to the "Avoid unused warnings. ". Try this:

use ckb_std::ckb_types::prelude::*;
debug!("script hash = {:#x}", script_hash.pack());

XuJiandong avatar May 23 '24 06:05 XuJiandong

It's due to the "Avoid unused warnings. ". Try this:

use ckb_std::ckb_types::prelude::*;
debug!("script hash = {:#x}", script_hash.pack());

Why execute functions (or cost cycles) when no log outputs are required?

I think it is a bug.

chaoticlonghair avatar Aug 08 '24 13:08 chaoticlonghair

It was to avoid a warning here in release build that tx_hash is not used: https://github.com/nervosnetwork/capsule/blob/a439682150afa2248fc53b1fa7990fb591ee4a07/templates/rust/contract/src/entry.rs#L31

Maybe you should fix the template instead. Then you don't need the drop.

blckngm avatar Aug 08 '24 14:08 blckngm

Maybe you should fix the template instead. Then you don't need the drop.

Sorry, I didn’t get you.

Did you meaning I should remove the .pack()?

In fact, my final goal is disabling any function calls in the debug! macro. Some type conversions (or data load) are only for debugging, they don't have to execute when in release mode.

They just waste cycles.


If you allowed, I could push a PR to remove the drop function. And I promise there will be no warnings.

chaoticlonghair avatar Aug 11 '24 11:08 chaoticlonghair

It's due to the "Avoid unused warnings. ". Try this:

use ckb_std::ckb_types::prelude::*;
debug!("script hash = {:#x}", script_hash.pack());

I think using drop in debug macro is wrong, for example, this code doesn't compiling:

    let tx_hash = load_tx_hash()?;
    debug!("tx hash is {:?}", tx_hash);
    let h0 = tx_hash[0]

quake avatar Aug 11 '24 23:08 quake

resolved by https://github.com/nervosnetwork/ckb-std/pull/98

quake avatar Aug 12 '24 01:08 quake

I mean you should modify the template to maybe

debug!("tx hash is {:?}", load_tx_hash()?);

Or like what quake wrote which uses the tx_hash in let h0 = tx_hash[0]. So that there isn't an unused variable tx_hash when debug assertions are not enabled.

blckngm avatar Aug 12 '24 07:08 blckngm

I mean you should modify the template to maybe

debug!("tx hash is {:?}", load_tx_hash()?);

Or like what quake wrote which uses the tx_hash in let h0 = tx_hash[0]. So that there isn't an unused variable tx_hash when debug assertions are not enabled.

I think that my code snippet doesn't compiling when debug assertions is disabled, we should remove the drop from macro.

quake avatar Aug 12 '24 07:08 quake

I think that my code snippet doesn't compiling when debug assertions is disabled, we should remove the drop from macro.

Yeah, I mean how to avoid the warning when the drop is removed.

blckngm avatar Aug 12 '24 07:08 blckngm