Skip to content

process: Cache dlOpen lib for faster lib calls#1903

Open
prashantv wants to merge 1 commit intoshirou:masterfrom
prashantv:darwin-faster
Open

process: Cache dlOpen lib for faster lib calls#1903
prashantv wants to merge 1 commit intoshirou:masterfrom
prashantv:darwin-faster

Conversation

@prashantv
Copy link
Copy Markdown
Contributor

On darwin, calls that rely on purego functions such as process times or memory info will open/close the library, which dlOpen/dlCloses the same library repeatedly. This causes them to be significantly slower when they're used for periodic metrics emission.

Cache the library on the process to avoid the repeated dlOpen/dlClose.

The added benchmark shows significant improvements:

# old
BenchmarkProcessCPUTime-10        211495              5682 ns/op            2776 B/op         64 allocs/op
# new
BenchmarkProcessCPUTime-10       1159842              1021 ns/op             760 B/op         15 allocs/op

@prashantv
Copy link
Copy Markdown
Contributor Author

Can I get a review for this optimization, it's especially helpful for use-cases that get the CPU time periodically.

Copy link
Copy Markdown
Owner

@shirou shirou left a comment

Choose a reason for hiding this comment

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

I see, indeed the initialization cost can be significant when it’s called frequently. The Process struct already caches some information to reduce overhead, so adding this as part of that caching aligns well with the existing implementation. Thank you.

However, I think the file name process_state_none.go is a bit unclear in terms of what "state" means. Could you consider renaming it to something like process_osprocess_fallback.go instead?

@prashantv
Copy link
Copy Markdown
Contributor Author

prashantv commented Sep 17, 2025

@shirou Happy to, renamed process_state_none.go to process_osprocess_fallback.go (and rebased off latest master)

On darwin, calls that rely on purego functions such as process times
or memory info will open/close the library, which dlOpen/dlCloses the
same library repeatedly. This causes them to be significantly slower
when they're used for periodic metrics emission.

Cache the library on the process to avoid the repeated dlOpen/dlClose.

The added benchmark shows significant improvements:

```
BenchmarkProcessCPUTime-10        211495              5682 ns/op            2776 B/op         64 allocs/op
BenchmarkProcessCPUTime-10       1159842              1021 ns/op             760 B/op         15 allocs/op
```
funcs, err := p.os.funcs()
if err != nil {
return "", err
return "", nil
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Sorry for the late comment. I think we should return err when p.os.funcs() returns an error. Was there a specific reason you decided to return nil instead?

funcs, err := p.os.funcs()
if err != nil {
return "", err
return "", nil
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

ditto

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