Skip to content

feat: improve ProcFDUsage performance#189

Merged
andrewkroh merged 1 commit intoelastic:masterfrom
stevendanna:ssd/faster-fdusage
Jun 6, 2025
Merged

feat: improve ProcFDUsage performance#189
andrewkroh merged 1 commit intoelastic:masterfrom
stevendanna:ssd/faster-fdusage

Conversation

@stevendanna
Copy link
Copy Markdown

@stevendanna stevendanna commented Jun 6, 2025

ioutil.ReadDir calls stat on every directory entry. These calls are not required
to count the number of file descriptors.

For a process with many open file descriptors this can be costly.

In a local benchmark on a directory with 10k files, we observe the following improvement by using (*os.File).Readdirnames directly:

goos: linux
goarch: amd64
pkg: github.com/elastic/gosigar
cpu: Intel(R) Xeon(R) CPU @ 2.80GHz
                    │   old.txt    │               new.txt               │
                    │    sec/op    │   sec/op     vs base                │
GetDirEntryCount-24   23.874m ± 0%   2.967m ± 0%  -87.57% (p=0.000 n=25)

                    │    old.txt    │               new.txt                │
                    │     B/op      │     B/op      vs base                │
GetDirEntryCount-24   3310.4Ki ± 0%   729.4Ki ± 0%  -77.97% (p=0.000 n=25)

                    │   old.txt   │               new.txt               │
                    │  allocs/op  │  allocs/op   vs base                │
GetDirEntryCount-24   30.03k ± 0%   10.02k ± 0%  -66.62% (p=0.000 n=25)

ioutil.ReadDir calls stat on every directory entry. These calls are not required
to count the number of file descriptors.

For a process with many open file descriptors this can be costly.

In a local benchmark on a directory with 10k files, we observe the following
improvement by using (*os.File).Readdir directly:

```
goos: linux
goarch: amd64
pkg: github.com/elastic/gosigar
cpu: Intel(R) Xeon(R) CPU @ 2.80GHz
                    │   old.txt    │               new.txt               │
                    │    sec/op    │   sec/op     vs base                │
GetDirEntryCount-24   23.874m ± 0%   2.967m ± 0%  -87.57% (p=0.000 n=25)

                    │    old.txt    │               new.txt                │
                    │     B/op      │     B/op      vs base                │
GetDirEntryCount-24   3310.4Ki ± 0%   729.4Ki ± 0%  -77.97% (p=0.000 n=25)

                    │   old.txt   │               new.txt               │
                    │  allocs/op  │  allocs/op   vs base                │
GetDirEntryCount-24   30.03k ± 0%   10.02k ± 0%  -66.62% (p=0.000 n=25)
```
Copy link
Copy Markdown
Member

@andrewkroh andrewkroh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks.

@andrewkroh andrewkroh merged commit 4438852 into elastic:master Jun 6, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants