Skip to content

Performance

Every number here was measured with BenchmarkDotNet, not estimated - and in the browser, they only hold with ahead-of-time compilation. Read the first section before anything else.

Measure this browser

The operations from the table below, on a generated 1920 × 1080 image, best of three after a warm-up. Around five times the AOT column means the interpreter. This site is published with AOT; under dotnet run it is interpreted, and every row takes several times longer - which is the point.

Editing tools in this browser

The editor's straightening, colour, selection, painting and layer tools, on the same 1920 × 1080 image and a product shot of that size, best of three after a warm-up, beside what they cost in Chrome on the interpreter and compiled ahead of time, and on desktop .NET on one thread. Those were measured on a slower machine than the table below: the benchmark above took about twice as long there, so compare the rows of this table with each other, the vibrance row being an older adjustment for reference.

WebAssembly: compile ahead of time

By default Blazor WebAssembly runs .NET code on an IL interpreter. Tight pixel loops are its worst case: every Vector4 operation RyuJIT turns into one SIMD instruction becomes a separate interpreted step. Bmage's loops are written around that, and the browser does the decoding, encoding and the editor's working copy, but what stays in .NET is still several times faster compiled ahead of time. Measured on a 1920 × 1080 image in Chromium, best of three after a warm-up:

OperationInterpreterAOTDesktop .NET
Resize to 1280, Lanczos3108 ms24 ms15 ms
Brightness11 ms3 ms1 ms
Four adjustments, fused118 ms23 ms25 ms
Gaussian blur, radius 8125 ms24 ms24 ms
Unsharp mask203 ms36 ms25 ms

The interpreter is 5 to 9× slower than desktop .NET; AOT is within 1 to 2.3× of it. A Debug build measured the same as Release on the interpreter, so the build configuration is not the issue. Hosting models shows how to switch it on, and how this site does.

<PropertyGroup>
  <RunAOTCompilation>true</RunAOTCompilation>
</PropertyGroup>

Resizing

Downscaling to 1280px on the long edge, and generating a 200px thumbnail (desktop .NET 10, AVX2):

Operation1920×10803840×21606000×4000
Box9.8 ms16.5 ms63.1 ms
Bilinear10.6 ms20.6 ms65.0 ms
Bicubic13.4 ms25.4 ms76.1 ms
Lanczos316.0 ms40.0 ms82.2 ms
Thumbnail to 200px (Auto)3.3 ms8.1 ms22.6 ms

Allocations stay between 23 KB and 185 KB regardless of source size, because pixel buffers are pooled and the resampler works a row at a time. Thumbnailing a 24 megapixel photo originally took 368 ms; an exact integer box prepass to within 2× of the target, finished by the requested filter, brought it to 52 ms and cut its allocations tenfold - with quality guarded by tests that a ramp stays linear and a fine checkerboard averages to grey rather than aliasing.

Operations and pipelines

Operation1920×10803840×2160
Crop0.06 ms0.28 ms
Rotate 90 (lossless)1.8 ms27.2 ms
Brightness (one pass)1.3 ms5.1 ms
Four adjustments, fused17.2 ms67.4 ms
Four adjustments, unfused18.4 ms75.2 ms
Gaussian blur, radius 825.3 ms99.5 ms
Unsharp mask27.3 ms107 ms
Rotate 37° (resampled)65.6 ms236 ms
  • Fusing adjustments is worth about 1.1×. Adjustments that map each channel from itself, such as brightness, contrast and gamma, run through 256-entry tables whether fused or not, so fusing mostly saves passes over the pixels.
  • A right angle is 9 to 37× cheaper than an arbitrary one, because it moves pixels rather than resampling them. The optimiser routes multiples of 90° to the lossless path automatically.
  • Blur is independent of radius: three box passes approximating a gaussian, so radius 40 costs the same as radius 4. Below a radius of about 2, where whole-pixel boxes would miss by more than 10%, a sampled gaussian of at most 13 taps runs instead.
  • Resize then crop fuses into one resample of only the surviving region - 25.8 ms against 62.2 ms on a 4000 × 3000 source, bit-identical.

Codecs

Operation1024×7683840×2160Allocated (4K)
PNG decode8.7 ms88 ms37 MB
JPEG decode7.4 ms77 ms14 MB
JPEG encode, quality 8511.5 ms122 ms6.7 MB
PNG encode, level 119.2 ms198 ms98 MB
PNG encode, level 668.0 ms720 ms46 MB

PNG compression level matters more than anything else here: level 6 is 3.6× slower than level 1 for a modest size gain. Level 1 suits a preview that is on screen for a moment; the default of 6 suits the file the user keeps.

Threads, workers and memory

Every number above is on one thread, the default. Operations whose rows are independent - resampling, rotation and warps, per-pixel adjustments, convolution, median, vignette, masks, layer compositing and drawing images - split their rows over MaxDegreeOfParallelism threads, each band with scratch of its own, so the pixels are identical however many run; a test asserts it for every one of them. Measured on a 10 core, 20 thread desktop:

Operation1 thread20 threadsSpeed-up
Downscale 6000×4000 to 1280, Lanczos3251 ms54 ms4.7×
Rotate 3840×2160 by 37°, bicubic450 ms74 ms6.1×
Saturation and hue, 3840×216095 ms15 ms6.3×
Median, radius 1, 1920×1080132 ms16 ms8.5×
Exposure through a gradient mask, 3840×216040 ms7.6 ms5.2×
Two masked layers composited, 3840×2160487 ms83 ms5.9×

Blurs and sharpening stay on one thread: they stream rows through running sums, and splitting them would change the sums and so the pixels. One thread is the default because it is right for a server, where requests and circuits already run side by side, and for the browser, where WebAssembly runs .NET on the page's thread; a desktop app, a background job or a WebAssembly app built with WasmEnableThreads should raise it.

// For every operation the services run: processors, loaders and editors.
builder.Services.AddBitBmageServices(options =>
{
    options.MaxDegreeOfParallelism = Environment.ProcessorCount;
    options.MaxCodecWorkers = 2;          // the browser codecs' Web Workers; 0 keeps them on the page
});

// For one run:
using var result = pipeline.Execute(image, new PipelineExecutionOptions { MaxDegreeOfParallelism = 8 });

// In a batch every image in flight uses that many: MaxConcurrency × MaxDegreeOfParallelism threads.

Web Workers

In the browser, decoding, encoding and building the editor's working copy run in a small pool of Web Workers, so the page keeps painting and handling input while a photo decodes, and the several decodes a batch makes spread over several cores. The pool starts with one worker and grows, while all are busy, to one per spare core and at most four. Files and pixels are transferred, not copied. Without OffscreenCanvas, or where a Content Security Policy blocks workers, the same work runs on the page's thread. MaxCodecWorkers caps the pool, and 0 turns it off.

Memory you can predict

A pipeline estimates the most pixel memory its run will hold before it runs, following the buffers each step keeps - a test holds the estimate between what a run really allocates and twice that. A BudgetedPixelAllocator enforces a ceiling: every buffer and mask an operation makes comes from the allocator in its context, so one budget bounds a request, a session or a batch, on any number of threads, and an allocation that would pass it is refused with ImageMemoryBudgetExceededException before it is made. A batch can also admit images by their expected peak with MaxMemoryBytes - see Batch processing.

var estimate = pipeline.EstimateMemory(new Size(6000, 4000));
estimate.PeakBytes;     // the most pixel memory the run holds at once, its result included
estimate.OutputBytes;   // the result

// A hard ceiling: an allocation past it throws ImageMemoryBudgetExceededException before it is made.
var budget = new BudgetedPixelAllocator(256L * 1024 * 1024);
using var result = pipeline.Execute(source, new PipelineExecutionOptions { Allocator = budget });
Console.WriteLine($"{budget.PeakBytes / 1048576.0:0.0} MB at the peak");

Interactive editing

The editor works on a proxy no larger than 2048px on its longest edge and replays the edit at full resolution on export: for a 24 megapixel photo, a 25× reduction in the pixels each preview touches - the difference between a slider that tracks your finger and one that does not. History stores descriptions, not pixels: a test asserts that 100 undo steps on a 6000 × 4000 image stay under 1 MB, where pixel snapshots would need about 4.8 GB.

Guidance

  • Cap dimensions before anything else. Every later operation costs proportionally less.
  • Group adjustments together. Consecutive per-pixel operations fuse into one pass; a geometric operation between them prevents that.
  • Prefer right angles. Rotate(90) takes the lossless path; Rotate(90.5) resamples the whole image.
  • Keep batch concurrency low, or budget it. Each worker holds a decoded image; four on 24 megapixel photos is roughly 400 MB of live pixels. MaxMemoryBytes lets the size of each image decide instead.
  • Add threads where one image at a time should finish sooner - a desktop app or a background job - not on a busy server.
  • Dispose buffers, and use object URLs, not data URLs.
ImagePipeline.Create().AutoOrient().MaxSize(1920, 1920).Sharpen(0.3f)
An unhandled error has occurred. Reload 🗙