Inlining, macro vs const generic. What is the most efficient, when arguments are const.?

I'm making fixed tests for my function pow_mod, which is similar to num_bigint::BigUint::modpow .
For convenience, I want to create a wrapper so that I can insert u64 literals while calling it.

I know that efficiency of testing functions hardly matters.
But I wonder what is most efficient (and possibly convenient) and why. Using inlining, macro or const generic or maybe inlined const generic function?

This is how inlined function would look like.

[inline]  // All parameters will be const.
fn u64_wrapped_pow_mod(base:u64, exp:u64, modulus:u64)->BigUint{
    pow_mod(base.to_biguint().unwrap(), &exp.to_biguint().unwrap(), &modulus.to_biguint().unwrap())
}

This is how const generic function would look like.

fn u64_wrapped_pow_mod<const base:u64, const exp:u64, const modulus:u64>()->BigUint{
    pow_mod(base.to_biguint().unwrap(), &exp.to_biguint().unwrap(), &modulus.to_biguint().unwrap())
}

To avoid clutter I won't insert code of macro & inlined const generic function.

All are very likely to compile to the exact same code (unless inling is not performed).

I would choose the normal function because it is more generic than the generic function (able to pass runtime values), more convenient than a macro, and can be more efficient than both (doesn't have to duplicate code).

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.