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:
| Operation | Interpreter | AOT | Desktop .NET |
|---|---|---|---|
| Resize to 1280, Lanczos3 | 108 ms | 24 ms | 15 ms |
| Brightness | 11 ms | 3 ms | 1 ms |
| Four adjustments, fused | 118 ms | 23 ms | 25 ms |
| Gaussian blur, radius 8 | 125 ms | 24 ms | 24 ms |
| Unsharp mask | 203 ms | 36 ms | 25 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):
| Operation | 1920×1080 | 3840×2160 | 6000×4000 |
|---|---|---|---|
| Box | 9.8 ms | 16.5 ms | 63.1 ms |
| Bilinear | 10.6 ms | 20.6 ms | 65.0 ms |
| Bicubic | 13.4 ms | 25.4 ms | 76.1 ms |
| Lanczos3 | 16.0 ms | 40.0 ms | 82.2 ms |
| Thumbnail to 200px (Auto) | 3.3 ms | 8.1 ms | 22.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
| Operation | 1920×1080 | 3840×2160 |
|---|---|---|
| Crop | 0.06 ms | 0.28 ms |
| Rotate 90 (lossless) | 1.8 ms | 27.2 ms |
| Brightness (one pass) | 1.3 ms | 5.1 ms |
| Four adjustments, fused | 17.2 ms | 67.4 ms |
| Four adjustments, unfused | 18.4 ms | 75.2 ms |
| Gaussian blur, radius 8 | 25.3 ms | 99.5 ms |
| Unsharp mask | 27.3 ms | 107 ms |
| Rotate 37° (resampled) | 65.6 ms | 236 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
| Operation | 1024×768 | 3840×2160 | Allocated (4K) |
|---|---|---|---|
| PNG decode | 8.7 ms | 88 ms | 37 MB |
| JPEG decode | 7.4 ms | 77 ms | 14 MB |
| JPEG encode, quality 85 | 11.5 ms | 122 ms | 6.7 MB |
| PNG encode, level 1 | 19.2 ms | 198 ms | 98 MB |
| PNG encode, level 6 | 68.0 ms | 720 ms | 46 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:
| Operation | 1 thread | 20 threads | Speed-up |
|---|---|---|---|
| Downscale 6000×4000 to 1280, Lanczos3 | 251 ms | 54 ms | 4.7× |
| Rotate 3840×2160 by 37°, bicubic | 450 ms | 74 ms | 6.1× |
| Saturation and hue, 3840×2160 | 95 ms | 15 ms | 6.3× |
| Median, radius 1, 1920×1080 | 132 ms | 16 ms | 8.5× |
| Exposure through a gradient mask, 3840×2160 | 40 ms | 7.6 ms | 5.2× |
| Two masked layers composited, 3840×2160 | 487 ms | 83 ms | 5.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.
MaxMemoryByteslets 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)