Skip to content

Allow -1 (unbounded) parallelism; validate settings#3110

Merged
JimBobSquarePants merged 1 commit intomainfrom
js/parallel-settings
Apr 9, 2026
Merged

Allow -1 (unbounded) parallelism; validate settings#3110
JimBobSquarePants merged 1 commit intomainfrom
js/parallel-settings

Conversation

@JimBobSquarePants
Copy link
Copy Markdown
Member

Prerequisites

  • I have written a descriptive pull-request title
  • I have verified that there are no overlapping pull-requests open
  • I have verified that I am following the existing coding patterns and practice as demonstrated in the repository. These follow strict Stylecop rules 👮.
  • I have provided test coverage for my change (where applicable)

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:

  • Updated ParallelExecutionSettings to document and support -1 as an unbounded value for MaxDegreeOfParallelism, and clarified this in both the code and XML documentation. [1] [2] [3] [4]
  • Refactored the logic in ParallelRowIterator to support unbounded parallelism: introduced GetNumberOfSteps and CreateParallelOptions helpers, and adjusted how the number of steps and parallel options are calculated when MaxDegreeOfParallelism is -1. [1] [2] [3] [4] [5] [6] [7] [8] [9]

Validation and safety enhancements:

  • Added ValidateSettings to ensure ParallelExecutionSettings are valid (e.g., MaxDegreeOfParallelism not zero or less than -1, MinimumPixelsProcessedPerTask > 0, and MemoryAllocator not null), guarding against invalid struct defaults and improving error messages.

Testing improvements:

  • Added tests to verify that using MaxDegreeOfParallelism = -1 results in all rows being processed, and that default (uninitialized) ParallelExecutionSettings throw appropriate exceptions. Introduced helper types for test operations. [1] [2] [3] [4]

@JimBobSquarePants JimBobSquarePants merged commit d3ca6eb into main Apr 9, 2026
12 checks passed
@JimBobSquarePants JimBobSquarePants deleted the js/parallel-settings branch April 9, 2026 02:18
/// <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.
Copy link
Copy Markdown
Member

@antonfirsov antonfirsov Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

https://github.com/SixLabors/ImageSharp.Drawing/blob/a4d03787dfc5a18cfe106d13b907a20d31534a8d/src/ImageSharp.Drawing/Processing/Backends/ParallelExecutionHelper.cs#L21-L22

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants