My first time getting and manipulating paths from the OS. (Quick code review of simple function logic)?

Edit: Okay, as is usually the case, there is more going on here that is causing the problem I am perceiving, because I'm spawning a thread, returning the result via a channel and waiting for the GUI to update, that's how i first noticed it was slow. it was slow to a human... but 2ms is not that.

In any case, a code review just to let me know i'm doing this right would be great because it's my first time using and manipulating paths. Thank you :slight_smile:


Hello,

So I have curious problem. Returning all .docx in a given directory is super slow, and I can not tell why. The logic is very similar to the other which works really well

Additionally, my logic in both functions are probably not ideal, so this would make an excellent code review, if someone would like to give their opinion. Thank you so much!

(Eventually I need a std::io::Result<im::Vector<Arc<String>>> but we can absolutely refactor into separate functions, if advisable)

/// Return all .docx files in a given directory
pub fn fetch_docx(path: &Path) -> std::io::Result<im::Vector<Arc<String>>> {
    let docx = OsStr::new("docx");
    Ok(std::fs::read_dir(path)?
        .filter_map(|res| {
            let entry = res.ok()?;
            if entry.path().extension() == Some(docx) {
                Some(Arc::new(
                    entry
                        .path()
                        .as_path()
                        .file_name()?
                        .to_string_lossy()
                        .to_string(),
                ))
            } else {
                None
            }
        })
        .collect())
}
/// Return all directories in a given directory
pub fn fetch_dirs(path: &Path) -> std::io::Result<im::Vector<Arc<String>>> {
    Ok(std::fs::read_dir(path)?
        .filter_map(|res| {
            let entry = res.ok()?;
            if entry.file_type().ok()?.is_dir() {
                Some(Arc::new(
                    entry
                        .path()
                        .as_path()
                        .file_name()?
                        .to_string_lossy()
                        .to_string(),
                ))
            } else {
                None
            }
        })
        .collect())
}

Okay, to be transparent: This is how i tested it:

let now = std::time::Instant::now();
                for _ in 0..100 {
                    let _ = ops::local::fetch_dirs(&data_thread.paths().studies());
                }
                println!("{:?}", now.elapsed()); // 991.685ยตs

//...

let now = std::time::Instant::now();
                for _ in 0..100 {
                    let _ = ops::local::fetch_docx(&data_thread.paths().templates());
                }
                println!("{:?}", now.elapsed()); // 2.273075ms