Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rayon parallel iterators #2058

Merged
merged 3 commits into from
Dec 2, 2023
Merged

Conversation

ScottyThePilot
Copy link
Contributor

This adds variants of the Pixels/PixelsMut/EnumeratePixels/EnumeratePixelsMut iterators that implement ParallelIterator/IndexedParallelIterator from rayon, as well as a from_par_fn function on ImageBuffer. This will allow large image buffers to be manipulated with multi-threading easily. Everything is also under a new optional rayon feature flag. This PR doesn't currently do it, but it might be worth considering re-exporting rayon somewhere.

I license past and future contributions under the dual MIT/Apache-2.0 license,
allowing licensees to choose either at their option.

Copy link
Contributor

@fintelia fintelia left a comment

Choose a reason for hiding this comment

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

Thanks for this PR! I think this makes a nice addition to the existing API.

I haven't gotten a chance to fully review the change, but I commented on a few things that I noticed. I think you'll also need to run cargo fmt to fix some of the formatting so that the CI passes

Cargo.toml Outdated
@@ -43,6 +43,7 @@ color_quant = "1.1"
exr = { version = "1.5.0", optional = true }
qoi = { version = "0.4", optional = true }
libwebp = { package = "webp", version = "0.2.2", default-features = false, optional = true }
rayon = { version = "1.8.0", optional = true }
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you able to depend on 1.7.0 instead? We currently target a lower MSRV than the 1.8.0 series.

To get the CI to pass, you probably will also have to run something like:

mv Cargo.lock.msrv Cargo.lock
cargo update -p rayon --precise 1.7.0
mv Cargo.lock Cargo.lock.msrv

@@ -888,7 +888,7 @@ where
Container: Deref<Target = [P::Subpixel]> + DerefMut,
{
// TODO: choose name under which to expose.
fn inner_pixels_mut(&mut self) -> &mut [P::Subpixel] {
pub(crate) fn inner_pixels_mut(&mut self) -> &mut [P::Subpixel] {
Copy link
Contributor

Choose a reason for hiding this comment

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

Mostly a note to myself: this method should theoretically be the same as the DerefMut implementation below, but actually behaves slightly differently. I want to look into why, and maybe even eliminate the duplication

}
}

impl<P> Clone for PixelsPar<'_, P>
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this implementation be derived automatically?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like derive(Clone) on this automatically puts a Clone bound on P. Since Pixels/EnumeratePixels doesn't have this bound, I assume it's unnecessary.

Copy link
Contributor

Choose a reason for hiding this comment

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

The P: Pixel bounds implies P: Clone (see https://docs.rs/image/latest/image/trait.Pixel.html)

}
}

impl<P> fmt::Debug for PixelsPar<'_, P>
Copy link
Contributor

Choose a reason for hiding this comment

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

This also might be automatically derivable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just like clone, derive(Debug) also seems to put unnecessary bounds on P (when it should be just P::Subpixel, I think), and also breaks on EnumeratePixelsPar and EnumeratePixelsMutPar for some reason.

@fintelia fintelia merged commit 81b3fe6 into image-rs:master Dec 2, 2023
35 checks passed
@fintelia
Copy link
Contributor

fintelia commented Dec 2, 2023

Sorry for the delay, I wanted to confirm that this was compatible with nice rustdoc output (which I've now added in #2063)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants