stanislav-tkach / os_info

Rust library to detect the operating system type
MIT License
172 stars 52 forks source link

os_info shouldn't call out to external commands on Linux (it is slow) #383

Open VorpalBlade opened 3 months ago

VorpalBlade commented 3 months ago

I have a program that runs for a very short time (<100 ms), that needs to know what Linux distro it is on. Unfortunately using os_info results in a lot of (relative) overhead due to it calling out to external binaries. About 35 ms extra in fact. The program runtime is performance criticial, it gets invoked many times from a shell script.

It seems that lsb_release is the worst offender (at ~35 ms). Removing lsb_release from path brings the overhead down to a more manageble ~4-5 ms. There is an old issue #319 about this, but no progress.

The remaining invoked programs seem to be:

Both getconf and uname should be possible to replace with calls to nix, libc or even rustix.

I have no idea why it looks like os_info is reivoking my program (paketkoll) though. But that shouldn't be needed either really. And yes, that extra process goes away if I remove os_info (and hard code in what distro is in use, in this case Arch Linux).

stanislav-tkach commented 3 months ago

I agree with your concerns, but unfortunately I don't have time right now to fix that. Hopefully I will be able to look into that in about a month, but I don't want to promise anything. I welcome pull requests if you would like to implement that yourself.

ydirson commented 2 months ago

I would argue that a frequently-running tool does not really need to call os_info every time, since that info is unlikely to change. Even if it would avoid calling lsb_release it would do extra work you don't need. I suggest caching the info you need in /run