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.25k stars 1.52k forks source link

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

Open cdown opened 2 months ago

cdown commented 2 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 2 months ago

Yeah that sounds great

cdown commented 2 months ago

Thanks! I'll get to work then.