Allow -1 (unbounded) parallelism; validate settings#3110
Allow -1 (unbounded) parallelism; validate settings#3110JimBobSquarePants merged 1 commit intomainfrom
Conversation
| /// <param name="maxDegreeOfParallelism">The value used for initializing <see cref="ParallelOptions.MaxDegreeOfParallelism"/> when using TPL.</param> | ||
| /// <param name="maxDegreeOfParallelism"> | ||
| /// The value used for initializing <see cref="ParallelOptions.MaxDegreeOfParallelism"/> when using TPL. | ||
| /// Set to <c>-1</c> to leave the degree of parallelism unbounded. |
There was a problem hiding this comment.
@JimBobSquarePants first I didn't want to comment because it felt like nitpicking on terminology, but now I'm afraid the proposed semantics for -1 are odd.
The official docs do not say -1 means unbounded, they only state that
If it is -1, there is no limit on the number of concurrently running operations
This doesn't mean that there will be no reasonable bounds on the number of operations, it means that the ThreadPool will dynamically determine the number.
However, it looks like the drawing code really interprets it as unbounded letting it go as high as the number of rows to draw. For CPU-bound work it doesn't make sense to go above the number of CPU-s available.
Long story short, `-1' should mean "let the runtime decide", and we should not attempt to outsmart it.
Prerequisites
Description
This pull request enhances the flexibility and robustness of parallel execution in ImageSharp by allowing an unbounded degree of parallelism (using
-1), improving validation for parallel execution settings, and updating the parallel row iteration logic accordingly. It also adds comprehensive tests to ensure the new behaviors are correct and validated.Parallel execution improvements:
ParallelExecutionSettingsto document and support-1as an unbounded value forMaxDegreeOfParallelism, and clarified this in both the code and XML documentation. [1] [2] [3] [4]ParallelRowIteratorto support unbounded parallelism: introducedGetNumberOfStepsandCreateParallelOptionshelpers, and adjusted how the number of steps and parallel options are calculated whenMaxDegreeOfParallelismis-1. [1] [2] [3] [4] [5] [6] [7] [8] [9]Validation and safety enhancements:
ValidateSettingsto ensureParallelExecutionSettingsare valid (e.g.,MaxDegreeOfParallelismnot zero or less than-1,MinimumPixelsProcessedPerTask> 0, andMemoryAllocatornot null), guarding against invalid struct defaults and improving error messages.Testing improvements:
MaxDegreeOfParallelism = -1results in all rows being processed, and that default (uninitialized)ParallelExecutionSettingsthrow appropriate exceptions. Introduced helper types for test operations. [1] [2] [3] [4]