gotcharustMajor
Why does std::fs::write(...) use an inner function?
Viewed 0 times
functionwhywriteusestdinnerdoes
Problem
I'm new to Rust and have been looking through the source code a bit, and found this:
Is there any reason this function defines an inner function like this? Why not just write:
#[stable(feature = "fs_read_write_bytes", since = "1.26.0")]
pub fn write, C: AsRef>(path: P, contents: C) -> io::Result {
fn inner(path: &Path, contents: &[u8]) -> io::Result {
File::create(path)?.write_all(contents)
}
inner(path.as_ref(), contents.as_ref())
}Is there any reason this function defines an inner function like this? Why not just write:
File::create(path.as_ref())?.write_all(contents.as_ref())Solution
Monomorphization costs.
To mitigate this cost, it calls an inner, non-generic function that does all the heavy lifting. The outer function contains only the necessarily-generic code - the conversion to
write(), like most filesystem functions in Rust, takes AsRef instead of Path, for convenience (to allow you to pass e.g. a &str). But that also has a cost: it means that the function will be monomorphized and optimized separately for each type, while there is no real need for that. While it is very likely that LLVM will deduplicate all those instances, the time used for optimizing them is still wasted compile time.To mitigate this cost, it calls an inner, non-generic function that does all the heavy lifting. The outer function contains only the necessarily-generic code - the conversion to
Path.Context
Stack Overflow Q#72246105, score: 75
Revisions (0)
No revisions yet.