andrewdavidmackenzie / libproc-rs

A rust library for getting information about running processes for Mac and Linux
MIT License
58 stars 17 forks source link

Unnecessary big vector allocation #9

Closed ghost closed 6 years ago

ghost commented 6 years ago

Hey, first of all thanks for the efforts for making this library! I have an optimization idea for listpids: https://github.com/andrewdavidmackenzie/libproc-rs/blob/master/src/libproc/proc_pid.rs#L232

let mut pids: Vec<u32> = Vec::with_capacity(PID_MAX);

Function allocates (99999 * 4 / 1024) =~ 390kb of space for a buffer which is not that bad by modern standards but still unnecessary because it's possible to retrieve the desired buffer size beforehand by calling proc_listpids in a special way like this:

int numberOfProcesses = proc_listpids(PROC_ALL_PIDS, 0, NULL, 0);

Example code at the bottom of the question: https://stackoverflow.com/q/3018054

andrewdavidmackenzie commented 6 years ago

Send me a Pull Request and I'll merge it!

andrewdavidmackenzie commented 6 years ago

Sorry, been travelling. Will look at your PR asap.

andrewdavidmackenzie commented 6 years ago

Merged. Released in 0.3.2 which has also been pushed to crates.io at https://crates.io/crates/libproc