rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.45k stars 1.54k forks source link

Calling .metadata() just for .is_{file,dir,symlink} when DirEntry is available #12955

Open cdown opened 4 months ago

cdown commented 4 months ago

What it does

At Meta internally I've been working on performance optimisations for a number of our widely deployed Rust daemons. One pattern I have repeatedly seen and optimised is:

    for entry in read_dir(".")? {
        entry?.metadata()?.is_dir();
    }

This requires an additional statx() (or equivalent) syscall. In some cases I have saved very large amounts of CPU and IO by just changing to:

    for entry in read_dir(".")? {
        entry?.file_type()?.is_dir();
    }

...which just uses the DT_* returned from readdir().

I plan to implement a lint which detects cases like this (where metadata() is called on entry only to call .is_{file,dir,symlink}().

Does this sound reasonable?

Advantage

Drawbacks

No response

Example

See above description.

Alexendoo commented 4 months ago

Yeah that sounds great

cdown commented 4 months ago

Thanks! I'll get to work then.