Conversation
telecos
reviewed
Feb 12, 2026
This has been a long standing issue. The lack of generalization in Rust makes it generally hard to provide specialized implementations of `copy_from` that can make full use of the layout information to improve code generation. Since the argument is generic we stay with the default implementation even for the very strict targets (and source) of `ImageBuffer`. We sidestep a large part of the problem by speeding up only the copy from a well-described layout: `View` (a strided matrix). Then an optional method on `GenericImageView` lets us query if the source is equivalent to such a copy. We wont reap the benefits for user defined trait impls but importantly the crate's own `ImageBuffer` and `SubImage` can provide it The latter also has a generic impl for SubImage<D> were <D as Deref>::Target: GenericImageView Due to this we do not use a double-dispatch approach (adding a `copy_into_buffer`) but this which composes purely at runtime. Also note that DynamicImage can not provide it but the goal is to replace this, too.
This also speeds up copies from them as well as copies from sub images of sample views.
The pattern was caught in review, went ahead and generalized it to the other occasion where a rectangular placement is built from an image.
Member
Author
|
Rebased so we can run complete benchmarks. Although, it does not seem necessary to justify this any further. Details |
telecos
approved these changes
Feb 12, 2026
Since conversion is fallible, using `to_*` is more appropriate.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Benchmarks TBD but expecting ~60% performance or more. Motivation, context and approach from the first commit and follows pretty directly what was presented in the thoughts on #2553.
This has been a long standing issue. The lack of generalization in Rust
makes it generally hard to provide specialized implementations of
copy_fromthat can make full use of the layout information to improvecode generation. Since the argument is generic we stay with the default
implementation even for the very strict targets (and source) of
ImageBuffer.We sidestep a large part of the problem by speeding up only the copy
from a well-described layout:
View(a strided matrix). Then anoptional method on
GenericImageViewlets us query if the source isequivalent to such a copy. We wont reap the benefits for user defined
trait impls but importantly the crate's own
ImageBufferandSubImagecan provide it The latter also has a generic impl for
Due to this we do not use a double-dispatch approach (adding a
copy_into_buffer) but this which composes purely at runtime.Also note that DynamicImage can not provide it but the goal is to
replace this, too.
Closes: #2417, possibly #2553 (@fintelia addresses the immediate concern but we expanded the scope for reasons of extensibility concerns; but sealing the trait is already represented in another issue; your call)