www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22745] New: std.parallelism.parallel fails to process empty

https://issues.dlang.org/show_bug.cgi?id=22745

          Issue ID: 22745
           Summary: std.parallelism.parallel fails to process empty range
                    on a single CPU
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: schveiguy gmail.com

On a VM where I have allocated one core, std.parallelism.parallel fails with
the message:

workUnitSize must be > 0

This comes from the following code:

```d
int main()
{
   import std.stdio;
   import std.parallelism;
   int[] arr;
   foreach(i; arr.parallel) { writeln(i); }
}
```

Why does it fail?

Here is the code that throws the error:

https://github.com/dlang/phobos/blob/ab4008c13f593b35adc7ec2229e299e73c182930/std/parallelism.d#L1631

Note that the workUnitSize is passed in from the default work unit size.

That is determined via this function:
https://github.com/dlang/phobos/blob/ab4008c13f593b35adc7ec2229e299e73c182930/std/parallelism.d#L1631

The `size` value is set determined by the number of cores here:
https://github.com/dlang/phobos/blob/ab4008c13f593b35adc7ec2229e299e73c182930/std/parallelism.d#L1524

Note that if "size" is zero (having 1 core), and the range is zero length, then
the workUnitSize will be zero.

I'd suspect a fix is to check if the range is empty and immediately return
before getting this far.

--
Feb 07 2022