libvips demand-driven execution model
libvips is not a traditional image library that reads in rows and processes them eagerly one step at a time. It is a demand-driven execution engine — operations you declare on an image are evaluated together only when a downstream consumer requests output pixels. This design is both the source of libvips’ speed and low memory footprint and the core mental model for reasoning about behaviour across language bindings.
You built a pipeline, but pixels have not been evaluated
Section titled “You built a pipeline, but pixels have not been evaluated”In language bindings, opening a file and declaring a resize or blur normally reads only enough header bytes to identify the format and metadata, then records a processing graph in memory: input.jpg → Load → Resize → GaussianBlur → (waiting for demand). The final output pixels have not been computed and written yet. By contrast, a single vips CLI command includes its output target on the command line, so it builds and evaluates the pipeline within that process run.
image, err := vips.NewImageFromFile("input.jpg")if err != nil { return err}defer image.Close()
if err := image.Resize(0.5, vips.KernelLanczos3); err != nil { return err}if err := image.GaussianBlur(3.0); err != nil { return err}// Only the native operation graph has been built so far; pixels are pulled// by region when Export* or SaveToWriter* is called.const pipeline = sharp('input.jpg') .resize({ width: 960, kernel: sharp.kernel.lanczos3 }) .blur(3.0);// Only the pipeline configuration on the Sharp instance has been recorded;// libvips evaluates it when await pipeline.toFile(...), await pipeline.toBuffer(),// or a writable stream pipe consumes the instance.image = pyvips.Image.new_from_file("input.jpg")resized = image.resize(0.5, kernel="lanczos3")blurred = resized.gaussblur(3.0)# Each call returns a new pyvips.Image graph node; evaluation starts when# write_to_file, write_to_buffer, or a terminal statistics method is called.# A single CLI command already specifies the output target, so the process# builds the graph and pulls pixels through to output.webp immediately:vips gaussblur input.jpg 'output.webp[Q=82]' 3.0Where demand comes from
Section titled “Where demand comes from”Exporting, streaming to a target, or computing image statistics acts as a downstream sink that requests regions from the output node back toward the loader:
| Trigger category | govips v2.19.0 | sharp 0.35.5 | pyvips 3.2.0 | vips CLI 8.17.3 |
|---|---|---|---|---|
| Encode to file or memory buffer | ExportJpeg(), ExportWebp(), ExportNative(), etc. |
toFile(), toBuffer() |
write_to_file(), write_to_buffer(), write_to_memory() |
Output file path argument on the command line |
| Stream to a write target | SaveToWriter() / format variants, TranscodeStream() |
pipeline.pipe(writable) |
write_to_target(target) |
Output target argument (such as a .webp stream target) |
| Downstream composition or statistics | Used as an input to Composite, Join, etc. and then exported, or Average() |
stats(), or used as an overlay in composite() and exported |
Used as an input to composite, bandjoin, etc. and exported, or avg(), min(), max() |
Statistics subcommands such as vips avg and vips min |
Line cache
Section titled “Line cache”libvips does not cache every full intermediate image in memory at once. It maintains a line buffer cache — the small set of rows needed to satisfy the current region request.
This means:
- If you read top-to-bottom sequentially, libvips only needs to keep a small window of rows in memory.
- If you need random access (for example, a 90° rotation needs the last row first), libvips may need to buffer the full input, or fail with
out of order readwhen opened in sequential mode.
Sequential vs random access
Section titled “Sequential vs random access”libvips supports two access modes:
| Mode | Behaviour | Peak RAM |
|---|---|---|
| Random (default) | Any region may be read in any order | Cached regions of the input |
| Sequential | Top-to-bottom, single-pass only | Dominated by image width, operation windows, and codec buffering |
Sequential mode is opt-in and restricts downstream operations — rotation, vertical flip, and other non-linear reads will fail unless the image is materialised (decoded and buffered in memory or a temporary file) first. Each entry point exposes sequential access through its own option:
- govips v2.19.0:
params.Access.Set(vips.AccessSequential)onImportParams. - sharp 0.35.5:
{ sequentialRead: true }in the constructor options. - pyvips 3.2.0:
pyvips.Image.new_from_file("input.jpg", access="sequential"). - vips CLI 8.17.3:
'input.jpg[access=sequential]'on the input filename.
Why operation order affects memory
Section titled “Why operation order affects memory”When product requirements allow blurring or convolving at the reduced resolution, downsampling first means every subsequent operation evaluates far fewer pixels:
image, err := vips.NewImageFromFile("input.jpg")if err != nil { return err}defer image.Close()
// When blurring at reduced resolution is acceptable, downsample before blurring.if err := image.Resize(0.1, vips.KernelLanczos3); err != nil { return err}if err := image.GaussianBlur(5.0); err != nil { return err}data, _, err := image.ExportWebp(vips.NewWebpExportParams())if err != nil { return err}return os.WriteFile("output.webp", data, 0o644)// sharp uses a fixed pipeline order on a single instance: even if .blur(5.0)// is chained before .resize(194), sharp still resizes before blurring.await sharp('input.jpg') .resize({ width: 194, kernel: sharp.kernel.lanczos3 }) .blur(5.0) .webp({ quality: 82 }) .toFile('output.webp');image = pyvips.Image.new_from_file("input.jpg")# pyvips builds graph nodes in call order: resizing before gaussblur evaluates# the blur on the reduced pixel grid.out = image.resize(0.1, kernel="lanczos3").gaussblur(5.0)out.write_to_file("output.webp", Q=82)# Each separate CLI command writes its output; when chaining steps via a# temporary file, shrinking first reduces both the intermediate file and the blur cost:vips resize input.jpg step1.v 0.1 --kernel lanczos3vips gaussblur step1.v 'output.webp[Q=82]' 5.0Common misconceptions
Section titled “Common misconceptions”- Myth 1: “Initialising the runtime or constructing an image object decodes the whole image.”
vips.Startup()(govips) only initialises the libvips runtime;NewImageFromFile,sharp(input), andpyvips.Image.new_from_filenormally read only header metadata on demand-driven paths until an export pulls pixels (whereas APIs such asLoadImageFromReaderin govips withoutAccessSequentialmaterialise upfront). - Myth 2: “The image is processed right before
Close()or garbage collection.” Pixel evaluation happens on demand during export or write;image.Close()(govips) and object finalisers only release native image references. - Myth 3: “Every operation creates a complete intermediate image.” Within a single process pipeline, libvips evaluates chained operations by region; however, chaining multiple separate
vipsCLI commands via temporary files materialises a full output file at each process boundary. - Myth 4: “Method chaining order always equals libvips execution order.”
govipsandpyvipsappend graph nodes in call order, whereassharpschedules operations on a single instance according to its fixed pipeline stages.
Risk notes
Section titled “Risk notes”- Out of Order Read: Random-access operations on sequential images fail, and the error is typically deferred until the export sink pulls pixels.
- Cache Miss Penalty: Random access on large images is significantly slower than sequential access.
- Binding default differences: File loaders, stream readers, and buffer inputs across govips, sharp, pyvips, and the CLI do not share identical default buffering or materialisation boundaries; always check the specific entry point’s contract.
Learn about sequential access, random access, and materialisation in detail.
Sources
Section titled “Sources”- libvips How libvips works, reviewed 2026-10-15
- govips v2.19.0 README Streaming I/O section
- sharp 0.35.5 API documentation (Input / Operations / Output)
- pyvips 3.2.0 Image API documentation
- libvips source
conversion/sequential.c