Skip to content

libvips demand-driven execution model

Copy page

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.

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

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 read when opened in sequential mode.

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) on ImportParams.
  • 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.

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)
  • 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), and pyvips.Image.new_from_file normally read only header metadata on demand-driven paths until an export pulls pixels (whereas APIs such as LoadImageFromReader in govips without AccessSequential materialise 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 vips CLI commands via temporary files materialises a full output file at each process boundary.
  • Myth 4: “Method chaining order always equals libvips execution order.” govips and pyvips append graph nodes in call order, whereas sharp schedules operations on a single instance according to its fixed pipeline stages.
  • 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.