num_cpus icon indicating copy to clipboard operation
num_cpus copied to clipboard

Be more optimistic when querying on Windows?

Open dpc opened this issue 6 years ago • 3 comments

As a part of cargo crev I was reviewing this crate in version 1.10.1 . All was good, but I have one idea.

It seems like on Windows this crate calls the GetLogicalProcessorInformation twice. Any reason not to start with a buffer of a fix size (eg. 64 entries)? Maybe this initial buffer could be on stack, so it wouldn't really cost anything. On most machines that would cut the number of calls to just 1.

dpc avatar Jul 03 '19 21:07 dpc

What you say sounds reasonable. I'm not too familiar with Windows programming, but I noticed the source links to an example on MSDN, and it looks like they also query once to get the size needed. Is the example naive?

seanmonstar avatar Jul 03 '19 22:07 seanmonstar

Seems so to me. But I have no clue, and haven't wrote for or used Windows in more than a decade, so what do I know. :D

dpc avatar Jul 03 '19 22:07 dpc

More optimal approach would be to use first call with some stack array of hard-coded size. If function fails with unsufficient memory, then it will update len to required one, in which case you could fallback to heap allocated vec.

This way in happy case the call will be only once.

DoumanAsh avatar Jan 12 '20 09:01 DoumanAsh