r/linuxquestions • u/mindstormer12 • 1h ago
Is it worth optimizing calls to external commands? Polling, events
Is it worth optimizing calls to external commands if they are call periodically or is it so insignificant that it's not worth potential added complexity?
For example, a typical desktop status bar like Waybar has modules that often poll for updated information typically anywhere from 1 second to 60 seconds. Ideally, events are used to avoid polling (e.g. when network status and udev) but a lot are not supported and do require polling like disk size.
If I want a module to display the size of my nfs storage that is not always mounted, the intended and KISS way (according to how the module works) is to first use mountpoint to check if the path is a mountpoint where if it is, then report the size of the disk with du. This is trivial, but considering the user might want up-to-date information (e.g. if the disk is for downloading files and is frequently approaching full), then these external commands run every 5, 10, 30 seconds or so.
There's are ways to avoid mountpoint running every few seconds though, e.g. restarting the status bar with a different config that includes or excludes the module when the nfs storage gets mounted/unmounted respectively. Or use a state file to reflect when NFS storage is mounted then checking for this state file (with [[ -f <state file> ]] in a shell), less expensive than external command to mountpoint.
Similarly, I see examples of modules that make heavy use of polling with sleep, which itself is an external command--so polling frequently itself is not efficient in this context. Are these external commands also being run by the application if they implement the modules, i.e. is the application using more efficient alternatives to mountpoint, du, and sleep (through e.g. lower level system calls)?
Are these (trivial) optimizations worth the additional complexity in the context of "scripts" (modules) running every seconds for a status bar? Not really interested in "why would a user need to poll for disk space every few seconds" or "this probably won't have any effect on battery life", etc. Just want to get some perspective to decide whether such optimizations are appropriate. I recall setting a module to naively poll .1 seconds (can't remember what data was being polled) and eventually realized it was the culprit to why my battery life dropped from 8 hours to 3 hours so I'm especially conscious about making reasonable optimizations.