-
-
Notifications
You must be signed in to change notification settings - Fork 427
Optimize memory usage by leveraging CommunityToolkit.HighPerformance for buffer pooling #1381
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
Conversation
Thanks for creating this pull request but I don't want to add an extra dependency to this library for such a small change. But maybe we could use And to be honest the |
I am going to close this pull request because I don't want to add the change in this form. |
Thank you for the fast and direct response. I really appreciate the feedback. I can switch implementations by leveraging The gravity extensions were more of an attempt to clean up that part of the code than an optimization, but I can undo that with no problem. |
I now have a proof of concept where I use the technique from the I don't want to add an extra dependency to the netstandard2.0 code. But I am open for changes that improve parts of the netstandard2.1 code without adding extra dependencies. p.s. If you are interested in improving performance and allocations then feel free to take a look at my magick-wasm project and see if I can do some smart things there. And I just realized that I will probably also need some help with using |
This PR proposes leveraging the Microsoft official package
CommunityToolkit.HighPerformance
(alongsideSystem.Memory
andSystem.Collections.Immutable
) to optimize memory usage by removing intermediate allocations where possible. This is only a small change to the project, but such is the nature of optimization: we can work with incremental steps.Description
This PR aims to optimize memory usage for a few methods of the
ImageMagick.MagickImage
class, namely:void Trim(Gravity[] edges)
now rents a buffer from the shared memory pool when converting edges to their string representations, which means we potentially get rid of intermediate allocations. (Also added aReadOnlySpan<Gravity>
overload for users that want to do their own pooling or stack allocate the parameters.)ToByteArray()
by renting a buffer from the shared pool.ToByteArray()
now potentially avoids intermediate allocations by writing the data intoArrayPoolBufferWriter<byte>
instead ofMemoryStream
, which leverages the array pool to avoid unnecessary allocations, especially when dynamic resizes are needed.