我希望在我的代码中将一些影响性能的函数调用放入功能门后。如果该功能未启用,我想只需实现一个空实现的函数即可。这样,希望 Rust 编译器可以完全从函数中删除它。
类似这样:
类似这样:
// Included if feature is enabled
fn foo() {
// ...
}
// Included if the feature is disabled
fn foo() {}
// Performance critical code
for i in 1..1000000000 {
// ...
foo();
}
如果foo()函数为空,那么这个调用会被优化掉吗?
-C remark=all
以查看优化备注。循环无法进行优化,因为“无法确定循环迭代次数”@paholg @MatthieuM - kennytm