What Bend should do
Expose the effective number of CPU worker threads to a running Bend program. For example:
def IO.thread_count() -> IO(U32):
...
def main() -> IO(Unit):
do IO<Unit>:
workers : U32 <- IO.thread_count()
run_bounded_pipeline(workers * 2)
On native builds this should return the runtime's effective worker-pool size: the value selected by --threads, or the detected available CPU count when the flag is omitted, after runtime clamping. A generated JavaScript program should return 1, since that lane is sequential.
The native runtime already computes this as pool_size, but it is not exposed to Bend code. IO.args() cannot recover it because the runtime consumes --threads before invoking the program. A public Base API would avoid relying on a custom effect that reaches into runtime implementation details.
Why
Programs with bounded parallel pipelines need to choose their amount of in-flight work from the actual runtime concurrency. A fixed queue either leaves workers idle or retains much more input, output, and scratch memory than necessary.
For example, a parallel compressor should usually retain only a small multiple of the active worker count. Today it cannot distinguish --threads 1, --threads 8, and the default, so it must over-allocate, under-utilize the machine, add its own unrelated configuration flag, or duplicate the runtime's CPU-detection logic in foreign code.
What Bend should do
Expose the effective number of CPU worker threads to a running Bend program. For example:
On native builds this should return the runtime's effective worker-pool size: the value selected by
--threads, or the detected available CPU count when the flag is omitted, after runtime clamping. A generated JavaScript program should return1, since that lane is sequential.The native runtime already computes this as
pool_size, but it is not exposed to Bend code.IO.args()cannot recover it because the runtime consumes--threadsbefore invoking the program. A public Base API would avoid relying on a custom effect that reaches into runtime implementation details.Why
Programs with bounded parallel pipelines need to choose their amount of in-flight work from the actual runtime concurrency. A fixed queue either leaves workers idle or retains much more input, output, and scratch memory than necessary.
For example, a parallel compressor should usually retain only a small multiple of the active worker count. Today it cannot distinguish
--threads 1,--threads 8, and the default, so it must over-allocate, under-utilize the machine, add its own unrelated configuration flag, or duplicate the runtime's CPU-detection logic in foreign code.