Skip to content

fs::readdir performance in wasi is not great #4475

Open
@imsnif

Description

@imsnif

Describe the bug

When reading the contents of a large folder inside the guest machine with eg. fs::readdir, it seems as if wasmer if performing a metadata request for every file: https://docs.rs/wasmer-vfs/3.1.1/src/wasmer_vfs/host_fs.rs.html#80 - which is a bit of a time sink, but the real time sink comes when iterating over files in the Readdir iterator. I could not trace where this is coming from.

This results in (on my machine) wasmer taking 30+ seconds to read a folder with 4K+ files. This is a very big pain point for us, and I could not find a way around this.

I tried implementing my own read_dir using the FileSystem trait and set_fs, but the Readdir timesink is still there (this only gets rid of the relatively negligible metadata timesink I mentioned above).

When implementing my own fs, I also noticed that for some reason the read_dir function is called several times on the same folder - but again, the real time sink is the Readdir one which I could not trace.

wasmer -vV; rustc -vV

rustc 1.75.0 (82e1608df 2023-12-21)
binary: rustc
commit-hash: 82e1608dfa6e0b5569232559e3d385fea5a93112
commit-date: 2023-12-21
host: x86_64-unknown-linux-gnu
release: 1.75.0
LLVM version: 17.0.6

I do not have the wasmer executable installed.

Steps to reproduce

Run a guest wasm blob and read a folder with lots of files, rust example:

    for entry in read_dir(Path::new("/host")).unwrap() {
        eprintln!("looping through entry: {:?}", entry);
    }

Expected behavior

Getting this down to <1s would be super nice.

Actual behavior

This takes quite long.

Additional context

This is a very big blocker for us in Zellij, and since we can't upgrade due to not wanting to adopt wasix, I'm a bit at a loss as to what to do about this. I could not mitigate this behavior by implementing my own fs as mentioned above and would really appreciate recommendations for a workaround. Thanks!!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions