PHP RFC: num_available_processors
- Version: 0.1
- Date: 2025-05-18
- Author: Daniel Kesselberg, mail@danielkesselberg.de
- Status: Draft
- First Published at: https://wiki.php.net/rfc/num_available_processors
Introduction
The number of available processors is valuable for tools like php-cs-fixer, enabling parallel execution, or when used with semaphores to manage the concurrent execution of resource-intensive tasks. The new num_available_processors function simplifies getting the number of logical processors across various operating systems.
Proposal
This RFC proposes the addition of a new function that retrieves the number of available processors:
function num_available_processors(): ?int
- The initial version of this proposal used nproc as the function name, inspired by the idea of adding a similar functionality to PHP. However, the current implementation is simpler than nproc, and to avoid potential differences in behavior, I prefer not to use nproc.
- The current name in the related PR is num_cpus, inspired by the rust crate num_cpus (https://docs.rs/num_cpus/latest/num_cpus/). However, it could be argued that num_cpus should return the number of physical CPUs, whereas the current implementation returns the number of available processing units.
- A popular package for php, to obtain the number of processors, uses the name cpu-core-counter.
- Therefore, I suggest using num_available_processors as the function name. However, I don't have a strong preference on this matter.
Backward Incompatible Changes
None.
Proposed PHP Version(s)
Next PHP 8.x
RFC Impact
This RFC introduces a new function, which might affect existing userland extensions if developers have already created helper functions with the same name. Since the proposed function has a clear and meaningful name, any custom functions should ideally do the same thing and be easy to replace.
There is no impact on SAPIs or the opcache.
Future Scope
There has already been some discussion on the GitHub pull request. While not currently proposed, a valuable addition could be the introduction of a class such as ReflectionServer or, OS (similar to Node.js, https://nodejs.org/api/os.html) to provide information about the underlying system. Existing functions like sys_getloadavg or php_uname could be integrated into this class.
Proposed Voting Choices
Include these so readers know where you are heading and can discuss the proposed voting options.
Patches and Tests
https://github.com/php/php-src/pull/11137 (the current prototype, probably just a rename of the function is necessary)
Implementation
After the project is implemented, this section should contain
- the version(s) it was merged into
- a link to the git commit(s)
- a link to the PHP manual entry for the feature
- a link to the language specification section (if any)
References
Links to external references, discussions or RFCs
Rejected Features
Keep this updated with features that were discussed on the mail lists.