I suspect would it not add to the cost for calling it too many if assuming this procedure is invoked several times in a row additionally what do you think inline would do to it?
Good point regarding Long dropping that from my understanding maybe we should not be at the mercy of compiler for assuming that it will responsibly drop this the moment we intent it to be!
Also do you think if we manually called drop method on the type would it be a problem ? Incase we were esoteric and had some raw pointers present within it !
You can add #[inline] to the function as an additional hint. You can do it with a macro.
macro_rules! time {
($($code:tt)*) => {{
let timer = ::std::time::Instant::now();
let _ = { $($code)* };
timer.elapsed()
}}
}
// ...
let response;
let duration = time!{
response = MyStruct::init();
};
But honestly, if you care that much, the rough measurements you're going to get with this approach already aren't good enough, and you really want to look into going through the trouble of using something like criterion.
If you don't care enough to do that, you shouldn't care about a missed function call inline or whatever here or there.[1] Other things like context switching or CPU warmup or FS cache, etc etc, will add as much noise or more.
Things drop at well defined points (albeit not necessarily intuitive points). But they also do so automatically and invisibly, so it's easy to forget to manually drop something when you want it to drop before the well defined point.
You can't call Drop::drop directly. You can invoke the destructor[2] by (unsafely) using drop_in_place. But probably you don't want or need to do that, and should instead just (safely) call std::mem::drop.[3]
Most meaningful things you can do with raw pointers also requires unsafe. Anytime you use unsafe, it's on you to understand and uphold the invariants.
Which the Drop approach could also incur, incidentally ↩︎
which will include a call to Drop::drop if it exists, but having a destructor is more general than having a Drop implementation ↩︎
Which simply moves the value to a function where it's automatically dropped -- as the documentation says, there is zero magic going on, and you can write the function yourself ↩︎