-
-
Notifications
You must be signed in to change notification settings - Fork 889
Reduce the number of memory allocations in lossless WebP encoder #2940
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 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0981832
Reduce the number of memory allocations in lossless WebP encoder
SladeThe ff9d3a6
Replace indexed span iteration with foreach (+performance)
SladeThe 0faf614
Change AddSinglePixOrCopy to accept 'in PixOrCopy'
SladeThe b240679
Pin the refs for faster access
SladeThe 909e06b
Revert "Pin the refs for faster access"
SladeThe 17fc6db
Remove list search in HistoListUpdateHead, we always know pair index …
SladeThe 33c055d
Merge branch 'main' into main
JimBobSquarePants a645b54
Merge branch 'main' into main
JimBobSquarePants 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
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
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 |
|---|---|---|
| @@ -1,21 +1,28 @@ | ||
| // Copyright (c) Six Labors. | ||
| // Licensed under the Six Labors Split License. | ||
|
|
||
| using System.Buffers; | ||
| using SixLabors.ImageSharp.Memory; | ||
|
|
||
| namespace SixLabors.ImageSharp.Formats.Webp.Lossless; | ||
|
|
||
| internal class Vp8LBackwardRefs | ||
| internal class Vp8LBackwardRefs : IDisposable | ||
| { | ||
| public Vp8LBackwardRefs(int pixels) => this.Refs = new List<PixOrCopy>(pixels); | ||
| private readonly IMemoryOwner<PixOrCopy> refs; | ||
| private int count; | ||
|
|
||
| public Vp8LBackwardRefs(MemoryAllocator memoryAllocator, int pixels) | ||
| { | ||
| this.refs = memoryAllocator.Allocate<PixOrCopy>(pixels); | ||
| this.count = 0; | ||
| } | ||
|
|
||
| public void Add(PixOrCopy pixOrCopy) => this.refs.Memory.Span[this.count++] = pixOrCopy; | ||
antonfirsov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// <summary> | ||
| /// Gets or sets the common block-size. | ||
| /// </summary> | ||
| public int BlockSize { get; set; } | ||
| public void Clear() => this.count = 0; | ||
|
|
||
| /// <summary> | ||
| /// Gets the backward references. | ||
| /// </summary> | ||
| public List<PixOrCopy> Refs { get; } | ||
| public Span<PixOrCopy>.Enumerator GetEnumerator() => this.refs.Slice(0, this.count).GetEnumerator(); | ||
|
|
||
| public void Add(PixOrCopy pixOrCopy) => this.Refs.Add(pixOrCopy); | ||
| /// <inheritdoc/> | ||
| public void Dispose() => this.refs.Dispose(); | ||
| } | ||
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method could be updated to take the struct via
into avoid the copy.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've made the change. But if there is any performance difference, it's hard to notice. The structure is not that big.