Skip to content

perf(worker): Decode image once per file in ImageSharpProcessor #153

Closed

Description

@bitbiter-dev

Problem

ImageProxyMiddleware reads the source file into a byte[] once (good), but then each of the three generators (ThumbnailGenerator, FullPreviewGenerator, BlurhashGenerator) independently calls Image.LoadAsync from a fresh MemoryStream. This means a large image (e.g. a 50 MP HEIC or RAW file) is fully decoded three times per ingestion.

Proposed solution

Load Image<Rgba32> once in ImageProxyMiddleware (or in ImageSharpProcessor) and pass the already-decoded image to each generator. The simplest approach is to add an overload / new interface method that accepts a pre-loaded image:

internal interface IImageProcessor
{
    Task<byte[]> CreateThumbnailAsync(Image source, CancellationToken ct);
    Task<byte[]> CreateFullPreviewAsync(Image source, CancellationToken ct);
    Task<string> ComputeBlurhashAsync(Image<Rgba32> source, CancellationToken ct);
}

ImageProxyMiddleware.InvokeAsync would load the image once:

using var image = await Image.LoadAsync<Rgba32>(sourceStream, ct);
var proxyFiles = await Task.WhenAll(generators.Select(g => WriteProxyAsync(g, image)));

The IImageProxyGenerator.GenerateAsync signature would need to change from Stream to Image (or a wrapper type), and the Stream-based overloads can be removed since the middleware now owns the decode lifetime.

Impact

Reduces decode work from O(n × generators) to O(n) per file. Most noticeable for high-resolution sources (HEIC, TIFF, uncompressed RAW) where decoding dominates processing time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions