On 24 May 2025, at 20:48, Rob Landers <rob@bottled.codes> wrote:
On Sat, May 24, 2025, at 19:37, Daniel Kesselberg wrote:
Hi everyone,
I'm happy to share my first RFC
It proposes adding a small function
to retrieve the number of available processors; a feature that's
commonly found in other programming languages and one that I believe
would be a useful addition to PHP.
The related PR has already received a bit of early traction, and now
that the RFC is complete, I'm looking forward to your feedback!
RFC: PHP: rfc:num_available_processors
Patch: feat: add function num_cpus (formerly nproc) by kesselb · Pull Request #11137 · php/php-src · GitHub
Best
Daniel
Looks good!
My main question is: what is this actually counting? In the RFC it mentions "available processing units" ... which means, what? What counts as a "processing unit"? Are we talking about CPU Threads/cores; NPU cores; TPM cores; clocks? GPS? GPU? ... a modern computer has many "processing units" for different purposes and workloads. I’m assuming this is CPU Threads, not physical cores? I will refer to CPU Threads as "Logical Cores" so we all don’t get confused since most of us here are programmers and saying "thread" has a different meaning.
Secondly, how is it counting "available"? If I assign PHP to a specific CPU affinity mask (say one logical core), will it return 1, or the total number of logical cores available on my machine? I would expect it to be 1, since PHP only has access to 1, but I can also see the logic in returning the total number.
— Rob
Hi Daniel,
I agree with Rob that "processor" is a bit too ambiguous. I'd use the phrase "cpu_core" instead. Yes, technically that's not entirely accurate when hyper-threading is used, but in most cases it's not trivial to distinguish physical cores from logical cores anyway, and "cpu_cores" provides the most understandable abstraction for the vast majority of use cases: deciding how many parallel processes one should use for optimal use of the CPU.
Also, is it really necessary to add "available" as a disambiguator? In other words: are there future plans to add a function that provides the "total" or "unavailable" number of processors? If not, I'd just drop the "available" part.
Finally, from a quick search in php-src there doesn't seem to be any existing function name that starts with `num_`. For the sake of consistency with the existing PHP functions, and similar functionality in other languages, I suggest suffixing the function name with `_count` instead.
So, to wrap this all up, I'd like to respectfully propose the following function name instead:
cpu_core_count()
Alwin