Skip to content

V3 Backport : Reduce the number of memory allocations in lossless WebP encoder #2947

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 1 commit into from
Jun 13, 2025

Conversation

JimBobSquarePants
Copy link
Member

Prerequisites

  • I have written a descriptive pull-request title
  • I have verified that there are no overlapping pull-requests open
  • I have verified that I am following the existing coding patterns and practice as demonstrated in the repository. These follow strict Stylecop rules 👮.
  • I have provided test coverage for my change (where applicable)

Description

Backport of #2940 for V3.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR backports performance improvements for the lossless WebP encoder by reducing memory allocations and optimizing backward reference handling. Key changes include:

  • Updating test and production code to use the new MemoryAllocator-based constructors for Vp8LBackwardRefs.
  • Converting PixOrCopy from a class to a readonly struct and updating its creation methods.
  • Refactoring loops to use foreach and in/ref semantics, as well as streamlining histogram and backward reference operations.

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/ImageSharp.Tests/Formats/WebP/Vp8LHistogramTests.cs Updated instantiation of Vp8LBackwardRefs and literal token creation.
src/ImageSharp/Formats/Webp/Lossless/Vp8LHistogram.cs Changed iteration in StoreRefs and updated AddSinglePixOrCopy to use in parameters.
src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs Removed block size calculation and updated references disposal.
src/ImageSharp/Formats/Webp/Lossless/Vp8LBackwardRefs.cs Migrated from List to MemoryAllocator-based storage and added a Clear method.
src/ImageSharp/Formats/Webp/Lossless/PixOrCopy.cs Converted to a readonly struct with updated factory methods for immutability.
src/ImageSharp/Formats/Webp/Lossless/HistogramEncoder.cs Refactored iteration logic and updated HistoListUpdateHead to require an index parameter.
src/ImageSharp/Formats/Webp/Lossless/CostModel.cs Replaced loop constructs with foreach for direct reference iteration.
src/ImageSharp/Formats/Webp/Lossless/BackwardReferenceEncoder.cs Updated loops to use ref iteration and replaced clear methods for consistency.
Comments suppressed due to low confidence (2)

src/ImageSharp/Formats/Webp/Lossless/BackwardReferenceEncoder.cs:782

  • [nitpick] Ensure that using ref iteration to mutate elements of a readonly struct is intended and safe in this context.
foreach (ref PixOrCopy v in refs)

src/ImageSharp/Formats/Webp/Lossless/HistogramEncoder.cs:467

  • [nitpick] Verify that passing the index 'j' to HistoListUpdateHead correctly maintains the intended order of histogram pairs.
HistoListUpdateHead(histoPriorityList, p, j);

this.count = 0;
}

public void Add(PixOrCopy pixOrCopy) => this.refs.Memory.Span[this.count++] = pixOrCopy;
Copy link
Preview

Copilot AI Jun 13, 2025

Choose a reason for hiding this comment

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

Consider adding a boundary check to ensure 'this.count' does not exceed the allocated memory length to prevent potential overflows.

Suggested change
public void Add(PixOrCopy pixOrCopy) => this.refs.Memory.Span[this.count++] = pixOrCopy;
public void Add(PixOrCopy pixOrCopy)
{
if (this.count >= this.refs.Memory.Length)
{
throw new InvalidOperationException("Cannot add more elements. Memory limit exceeded.");
}
this.refs.Memory.Span[this.count++] = pixOrCopy;
}

Copilot uses AI. Check for mistakes.

@JimBobSquarePants JimBobSquarePants merged commit f7d1a9e into release/3.1.x Jun 13, 2025
22 checks passed
@JimBobSquarePants JimBobSquarePants deleted the js/v3-backport-2940 branch June 13, 2025 09:48
This was referenced Aug 1, 2025
HEJOK254 pushed a commit to HEJOK254/Discord-QuickEdit that referenced this pull request Aug 15, 2025
Updated [SixLabors.ImageSharp](https://github.com/SixLabors/ImageSharp)
from 3.1.7 to 3.1.11.

<details>
<summary>Release notes</summary>

_Sourced from [SixLabors.ImageSharp's
releases](https://github.com/SixLabors/ImageSharp/releases)._

## 3.1.11

## What's Changed
* Backport V3 - GIF: Check for end of stream when reading comments. by
@​JimBobSquarePants in SixLabors/ImageSharp#2955


**Full Changelog**:
SixLabors/ImageSharp@v3.1.10...v3.1.11

## 3.1.10

## What's Changed
* V3: Remove ExifTagValue.InteroperabilityIndex by @​JimBobSquarePants
in SixLabors/ImageSharp#2939
* V3 TIFF : Allow additional and undefined extra samples by
@​JimBobSquarePants in SixLabors/ImageSharp#2942
* V3 Use EXIF byte order for EXIF encoded strings. by
@​JimBobSquarePants in SixLabors/ImageSharp#2944
* V3 Backport : Reduce the number of memory allocations in lossless WebP
encoder by @​JimBobSquarePants in
SixLabors/ImageSharp#2947


**Full Changelog**:
SixLabors/ImageSharp@v3.1.9...v3.1.10

## 3.1.9

## What's Changed
* V3: Do not attempt to decode iDAT chunks when image is fully decoded.
by @​JimBobSquarePants in
SixLabors/ImageSharp#2927
* V3 - Do not throw exception for non-required chunks. by
@​JimBobSquarePants in SixLabors/ImageSharp#2929
* V3: Do not encode WEBP images exceeding max dimensions by
@​JimBobSquarePants in SixLabors/ImageSharp#2931


**Full Changelog**:
SixLabors/ImageSharp@v3.1.8...v3.1.9

## 3.1.8

## What's Changed
* V3 : Fix GIF, PNG, and WEBP Edge Case Handling by @​JimBobSquarePants
in SixLabors/ImageSharp#2882
* V3 Nested sub ifds parsing fix by @​JimBobSquarePants in
SixLabors/ImageSharp#2913
* V3: Undo horizontal prediction for each tile row in case of tiled
tiff's by @​JimBobSquarePants in
SixLabors/ImageSharp#2914
* V3 - Use BPP from Color Writer for Compressor by @​JimBobSquarePants
in SixLabors/ImageSharp#2915


**Full Changelog**:
SixLabors/ImageSharp@v3.1.7...v3.1.8

Commits viewable in [compare
view](SixLabors/ImageSharp@v3.1.7...v3.1.11).
</details>

[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=SixLabors.ImageSharp&package-manager=nuget&previous-version=3.1.7&new-version=3.1.11)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)
You can disable automated security fix PRs for this repo from the
[Security Alerts
page](https://github.com/HEJOK254/Discord-QuickEdit/network/alerts).

</details>

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant