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 intorust-lang:mainfrom
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. |
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.