Introduce a specialization trait for ReadDir for most platforms#155367
Introduce a specialization trait for ReadDir for most platforms#155367asder8215 wants to merge 1 commit into
ReadDir for most platforms#155367Conversation
| } | ||
|
|
||
| pub fn readdir(p: &Path) -> io::Result<ReadDir> { | ||
| let path = crate::path::absolute(p)?; |
There was a problem hiding this comment.
This seems to be unnecessary since internally uefi_fs::File::from_path already uses crate::path::absolute(p) on the given path reference.
|
Is this allocation actually that expensive compared to the allocation that need to be done for every returned directory entry to warrant a ton of extra code? |
You're right to point this out since each However, I guess I've been thinking about why not just potentially improve the performance and usage of building The effects of how big this change makes for performance is something that I should benchmark. The |
…on most platforms to construct a ReadDir with zero cost conversion if given a PathBuf or something that could be converted into a PathBuf
2858476 to
13a0cee
Compare
|
I don't think this complexity is warranted, especially given it needs specialization which carries some correctness risk too. But I'll r? @the8472 if you want to take review here. |
|
So I get the motivation, but the question is how much of a win is this? So yeah, benchmarks needed. I suspect the difference would be small, but I could be wrong (maybe for long paths?). But there's another angle. There's work on directory handles (#120426) which means we'll soon have to think about a |
I wrote some benchmark code for this in this repo. Some of the error-related stuff I extracted or implemented manually inside What I benchmarked were the following (num of nested directories and files were found through: This is from my This is from my This is from my home directory (though I set the Criterion sampling size to 10 since it takes a while to run this), which it contains 278652 directories, and 2515101 files at this current time. Yeah, the performance gain is pretty small (~2.3% diff on |
Got you. I'm going to need a bit of time to look at the PR and understand the context behind this. However, that makes sense to make |
For most platforms, we can achieve a zero cost way of constructing a
ReadDirviastd::fs::read_dirwhen passing in aPathBufor something that can be converted into aPathBuf(in a zero cost fashion) through a specialization trait (asstd::fs::read_dirtakes in anAsRef<Path>). We can use a specialization trait via what's offered bymin_specializationfeature.The only platforms, as far as I can tell, that cannot achieve a zero cost way (at least conveniently) of constructing a
ReadDirwith an owned path isuefisince it seems like it needs to create an absolute path from a path reference (which creates and allocates for aPathBufanyways). On Windows, this specialization trait gets rid oflet root = p.into_path_buf(), reducing onePathBufallocation, but there's already anotherPathBufallocation that occurs through pushing a"*"on the path. Of course, onunsupportedandvexos, sincereaddiris unsupported on those platforms, I didn't introduce the trait there.I also tried out making a mock
ReadDirwith themin_specializationfeature on rust playground and can see that an owned path and a path reference uses different functions of the specialization trait accordingly.r? @Mark-Simulacrum since this goes hand in hand with the
remove_dir_all_recursivePR I made that he's a reviewer of.