-
-
Notifications
You must be signed in to change notification settings - Fork 888
Limit ResizeProcessor memory consumption #888
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
Merged
Merged
Changes from 37 commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
f455dad
PixelConversionModifierExtensions.ApplyCompanding() + tests
antonfirsov 9861084
drop FileTestBase usage in ResizeTests
antonfirsov 985357c
introduce [WithBasicTestPatternImages]
antonfirsov 8695c39
drop parallelism in ResizeProcessor for simplicity
antonfirsov 701343a
ResizeRectangle -> TargetRectangle
antonfirsov 285b892
Merge remote-tracking branch 'origin/master' into af/resize-sandbox
antonfirsov 4d38d7c
ResizeWindowOld
antonfirsov f81939e
ResizeWindow refactor 1
antonfirsov 7079410
ResizeWindow refactor 2
antonfirsov 545abf2
ResizeWindow refactor 3
antonfirsov 20ddd11
ResizeWindow refactor 4
antonfirsov 86cc83d
basic sliding window implementation (semi-stable state)
antonfirsov 56de415
fix ResizeWithCropHeightMode
antonfirsov 8e607ae
reference output for Resize_BasicSmall
antonfirsov 682e521
minor optimization
antonfirsov 4a0839a
refactor
antonfirsov b3495e8
refactor stuff + implement CalculateResizeWorkerWindowCount()
antonfirsov 40aea16
utilize CalculateResizeWorkerHeightInWindowBands()
antonfirsov 15b3b2a
improve benchmark: ArrayCopy -> CopyBuffers
antonfirsov 6db0701
moar RowInterval stuff
antonfirsov 19766df
buffer.CopyColumns(...)
antonfirsov d9e4d6c
simplify ResizeWorker logic
antonfirsov 51cc659
WorkingBufferSizeHintInBytes_IsAppliedCorrectly
antonfirsov 3860fb6
more robust tests
antonfirsov 1fd135f
ResizeTests.LargeImage
antonfirsov f6c7784
optimized sliding works!
antonfirsov f7e0a94
reapply unsafe optimizations
antonfirsov 2168071
moar unsafe optimization
antonfirsov a7c5295
benchmark WorkingBufferSizeHint effects
antonfirsov d882d91
memory profiling with Sandbox46
antonfirsov 0c684ab
add ResizeWorker.pptx
antonfirsov 1ab291c
refine ResizeWorker.pptx
antonfirsov 94b52bf
xmldoc for ResizeWorker
antonfirsov d2e1a8d
update ResizeWorker.pptx [skip CI]
antonfirsov b7c4c9c
Merge remote-tracking branch 'origin/master' into af/resize-sandbox
antonfirsov c87a0f9
fix tests
antonfirsov 026df1e
fix license text in CopyBuffers benchmark
antonfirsov 1430336
extend the CopyBuffers benchmark
antonfirsov a359b19
use HashCode.Combine()
antonfirsov 58f8d63
Merge branch 'master' into af/resize-sandbox
antonfirsov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,10 @@ | |
| // Licensed under the Apache License, Version 2.0. | ||
|
|
||
| using System; | ||
| using System.Buffers; | ||
| using System.Diagnostics; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| using SixLabors.Primitives; | ||
|
|
||
|
|
@@ -14,55 +17,135 @@ namespace SixLabors.ImageSharp.Memory | |
| internal static class Buffer2DExtensions | ||
| { | ||
| /// <summary> | ||
| /// Gets a <see cref="Span{T}"/> to the backing buffer of <paramref name="buffer"/>. | ||
| /// Copy <paramref name="columnCount"/> columns of <paramref name="buffer"/> inplace, | ||
| /// from positions starting at <paramref name="sourceIndex"/> to positions at <paramref name="destIndex"/>. | ||
| /// </summary> | ||
| internal static Span<T> GetSpan<T>(this Buffer2D<T> buffer) | ||
| public static unsafe void CopyColumns<T>( | ||
| this Buffer2D<T> buffer, | ||
| int sourceIndex, | ||
| int destIndex, | ||
| int columnCount) | ||
| where T : struct | ||
| { | ||
| return buffer.MemorySource.GetSpan(); | ||
| DebugGuard.NotNull(buffer, nameof(buffer)); | ||
| DebugGuard.MustBeGreaterThanOrEqualTo(sourceIndex, 0, nameof(sourceIndex)); | ||
| DebugGuard.MustBeGreaterThanOrEqualTo(destIndex, 0, nameof(sourceIndex)); | ||
| CheckColumnRegionsDoNotOverlap(buffer, sourceIndex, destIndex, columnCount); | ||
|
|
||
| int elementSize = Unsafe.SizeOf<T>(); | ||
| int width = buffer.Width * elementSize; | ||
| int sOffset = sourceIndex * elementSize; | ||
| int dOffset = destIndex * elementSize; | ||
| long count = columnCount * elementSize; | ||
|
|
||
| Span<byte> span = MemoryMarshal.AsBytes(buffer.Memory.Span); | ||
|
|
||
| fixed (byte* ptr = span) | ||
| { | ||
| byte* basePtr = (byte*)ptr; | ||
| for (int y = 0; y < buffer.Height; y++) | ||
| { | ||
| byte* sPtr = basePtr + sOffset; | ||
| byte* dPtr = basePtr + dOffset; | ||
|
|
||
| Buffer.MemoryCopy(sPtr, dPtr, count, count); | ||
|
|
||
| basePtr += width; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets a <see cref="Span{T}"/> to the row 'y' beginning from the pixel at 'x'. | ||
| /// Returns a <see cref="Rectangle"/> representing the full area of the buffer. | ||
| /// </summary> | ||
| /// <typeparam name="T">The element type</typeparam> | ||
| /// <param name="buffer">The <see cref="Buffer2D{T}"/></param> | ||
| /// <returns>The <see cref="Rectangle"/></returns> | ||
| public static Rectangle FullRectangle<T>(this Buffer2D<T> buffer) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All these methods here are very nice! |
||
| where T : struct | ||
| { | ||
| return new Rectangle(0, 0, buffer.Width, buffer.Height); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Return a <see cref="BufferArea{T}"/> to the subarea represented by 'rectangle' | ||
| /// </summary> | ||
| /// <typeparam name="T">The element type</typeparam> | ||
| /// <param name="buffer">The <see cref="Buffer2D{T}"/></param> | ||
| /// <param name="rectangle">The rectangle subarea</param> | ||
| /// <returns>The <see cref="BufferArea{T}"/></returns> | ||
| public static BufferArea<T> GetArea<T>(this Buffer2D<T> buffer, in Rectangle rectangle) | ||
| where T : struct => | ||
| new BufferArea<T>(buffer, rectangle); | ||
|
|
||
| public static BufferArea<T> GetArea<T>(this Buffer2D<T> buffer, int x, int y, int width, int height) | ||
| where T : struct => | ||
| new BufferArea<T>(buffer, new Rectangle(x, y, width, height)); | ||
|
|
||
| /// <summary> | ||
| /// Return a <see cref="BufferArea{T}"/> to the whole area of 'buffer' | ||
| /// </summary> | ||
| /// <typeparam name="T">The element type</typeparam> | ||
| /// <param name="buffer">The <see cref="Buffer2D{T}"/></param> | ||
| /// <returns>The <see cref="BufferArea{T}"/></returns> | ||
| public static BufferArea<T> GetArea<T>(this Buffer2D<T> buffer) | ||
| where T : struct => | ||
| new BufferArea<T>(buffer); | ||
|
|
||
| public static BufferArea<T> GetAreaBetweenRows<T>(this Buffer2D<T> buffer, int minY, int maxY) | ||
| where T : struct => | ||
| new BufferArea<T>(buffer, new Rectangle(0, minY, buffer.Width, maxY - minY)); | ||
|
|
||
| /// <summary> | ||
| /// Gets a span for all the pixels in <paramref name="buffer"/> defined by <paramref name="rows"/> | ||
| /// </summary> | ||
| public static Span<T> GetMultiRowSpan<T>(this Buffer2D<T> buffer, in RowInterval rows) | ||
| where T : struct | ||
| { | ||
| return buffer.Span.Slice(rows.Min * buffer.Width, rows.Height * buffer.Width); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets a <see cref="Memory{T}"/> to the row 'y' beginning from the pixel at the first pixel on that row. | ||
| /// </summary> | ||
| /// <param name="buffer">The buffer</param> | ||
| /// <param name="x">The x coordinate (position in the row)</param> | ||
| /// <param name="y">The y (row) coordinate</param> | ||
| /// <typeparam name="T">The element type</typeparam> | ||
| /// <returns>The <see cref="Span{T}"/></returns> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public static Span<T> GetRowSpan<T>(this Buffer2D<T> buffer, int x, int y) | ||
| public static Memory<T> GetRowMemory<T>(this Buffer2D<T> buffer, int y) | ||
| where T : struct | ||
| { | ||
| return buffer.GetSpan().Slice((y * buffer.Width) + x, buffer.Width - x); | ||
| return buffer.MemorySource.Memory.Slice(y * buffer.Width, buffer.Width); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets a <see cref="Span{T}"/> to the row 'y' beginning from the pixel at the first pixel on that row. | ||
| /// Gets a <see cref="Span{T}"/> to the row 'y' beginning from the pixel at 'x'. | ||
| /// </summary> | ||
| /// <param name="buffer">The buffer</param> | ||
| /// <param name="x">The x coordinate (position in the row)</param> | ||
| /// <param name="y">The y (row) coordinate</param> | ||
| /// <typeparam name="T">The element type</typeparam> | ||
| /// <returns>The <see cref="Span{T}"/></returns> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public static Span<T> GetRowSpan<T>(this Buffer2D<T> buffer, int y) | ||
| public static Span<T> GetRowSpan<T>(this Buffer2D<T> buffer, int x, int y) | ||
| where T : struct | ||
| { | ||
| return buffer.GetSpan().Slice(y * buffer.Width, buffer.Width); | ||
| return buffer.GetSpan().Slice((y * buffer.Width) + x, buffer.Width - x); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets a <see cref="Memory{T}"/> to the row 'y' beginning from the pixel at the first pixel on that row. | ||
| /// Gets a <see cref="Span{T}"/> to the row 'y' beginning from the pixel at the first pixel on that row. | ||
| /// </summary> | ||
| /// <param name="buffer">The buffer</param> | ||
| /// <param name="y">The y (row) coordinate</param> | ||
| /// <typeparam name="T">The element type</typeparam> | ||
| /// <returns>The <see cref="Span{T}"/></returns> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public static Memory<T> GetRowMemory<T>(this Buffer2D<T> buffer, int y) | ||
| public static Span<T> GetRowSpan<T>(this Buffer2D<T> buffer, int y) | ||
| where T : struct | ||
| { | ||
| return buffer.MemorySource.Memory.Slice(y * buffer.Width, buffer.Width); | ||
| return buffer.GetSpan().Slice(y * buffer.Width, buffer.Width); | ||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -78,49 +161,28 @@ public static Size Size<T>(this Buffer2D<T> buffer) | |
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns a <see cref="Rectangle"/> representing the full area of the buffer. | ||
| /// Gets a <see cref="Span{T}"/> to the backing buffer of <paramref name="buffer"/>. | ||
| /// </summary> | ||
| /// <typeparam name="T">The element type</typeparam> | ||
| /// <param name="buffer">The <see cref="Buffer2D{T}"/></param> | ||
| /// <returns>The <see cref="Rectangle"/></returns> | ||
| public static Rectangle FullRectangle<T>(this Buffer2D<T> buffer) | ||
| internal static Span<T> GetSpan<T>(this Buffer2D<T> buffer) | ||
| where T : struct | ||
| { | ||
| return new Rectangle(0, 0, buffer.Width, buffer.Height); | ||
| return buffer.MemorySource.GetSpan(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Return a <see cref="BufferArea{T}"/> to the subarea represented by 'rectangle' | ||
| /// </summary> | ||
| /// <typeparam name="T">The element type</typeparam> | ||
| /// <param name="buffer">The <see cref="Buffer2D{T}"/></param> | ||
| /// <param name="rectangle">The rectangle subarea</param> | ||
| /// <returns>The <see cref="BufferArea{T}"/></returns> | ||
| public static BufferArea<T> GetArea<T>(this Buffer2D<T> buffer, in Rectangle rectangle) | ||
| where T : struct => new BufferArea<T>(buffer, rectangle); | ||
|
|
||
| public static BufferArea<T> GetArea<T>(this Buffer2D<T> buffer, int x, int y, int width, int height) | ||
| where T : struct => new BufferArea<T>(buffer, new Rectangle(x, y, width, height)); | ||
|
|
||
| public static BufferArea<T> GetAreaBetweenRows<T>(this Buffer2D<T> buffer, int minY, int maxY) | ||
| where T : struct => new BufferArea<T>(buffer, new Rectangle(0, minY, buffer.Width, maxY - minY)); | ||
|
|
||
| /// <summary> | ||
| /// Return a <see cref="BufferArea{T}"/> to the whole area of 'buffer' | ||
| /// </summary> | ||
| /// <typeparam name="T">The element type</typeparam> | ||
| /// <param name="buffer">The <see cref="Buffer2D{T}"/></param> | ||
| /// <returns>The <see cref="BufferArea{T}"/></returns> | ||
| public static BufferArea<T> GetArea<T>(this Buffer2D<T> buffer) | ||
| where T : struct => new BufferArea<T>(buffer); | ||
|
|
||
| /// <summary> | ||
| /// Gets a span for all the pixels in <paramref name="buffer"/> defined by <paramref name="rows"/> | ||
| /// </summary> | ||
| public static Span<T> GetMultiRowSpan<T>(this Buffer2D<T> buffer, in RowInterval rows) | ||
| [Conditional("DEBUG")] | ||
| private static void CheckColumnRegionsDoNotOverlap<T>( | ||
| Buffer2D<T> buffer, | ||
| int sourceIndex, | ||
| int destIndex, | ||
| int columnCount) | ||
| where T : struct | ||
| { | ||
| return buffer.Span.Slice(rows.Min * buffer.Width, rows.Height * buffer.Width); | ||
| int minIndex = Math.Min(sourceIndex, destIndex); | ||
| int maxIndex = Math.Max(sourceIndex, destIndex); | ||
| if (maxIndex < minIndex + columnCount || maxIndex > buffer.Width - columnCount) | ||
| { | ||
| throw new InvalidOperationException("Column regions should not overlap!"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,9 @@ | |
|
|
||
| namespace SixLabors.ImageSharp.PixelFormats | ||
| { | ||
| /// <summary> | ||
| /// Extension and utility methods for <see cref="PixelConversionModifiers"/>. | ||
| /// </summary> | ||
| internal static class PixelConversionModifiersExtensions | ||
| { | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
|
|
@@ -16,5 +19,20 @@ public static PixelConversionModifiers Remove( | |
| this PixelConversionModifiers modifiers, | ||
| PixelConversionModifiers removeThis) => | ||
| modifiers & ~removeThis; | ||
|
|
||
| /// <summary> | ||
| /// Applies the union of <see cref="PixelConversionModifiers.Scale"/> and <see cref="PixelConversionModifiers.SRgbCompand"/>, | ||
| /// if <paramref name="compand"/> is true, returns unmodified <paramref name="originalModifiers"/> otherwise. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// <see cref="PixelConversionModifiers.Scale"/> and <see cref="PixelConversionModifiers.SRgbCompand"/> | ||
| /// should be always used together! | ||
| /// </remarks> | ||
| public static PixelConversionModifiers ApplyCompanding( | ||
| this PixelConversionModifiers originalModifiers, | ||
| bool compand) => | ||
| compand | ||
| ? originalModifiers | PixelConversionModifiers.Scale | PixelConversionModifiers.SRgbCompand | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Excellent! This solves the complexity issues |
||
| : originalModifiers; | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.