Why dmog streams

dmog writes a translation as it reads the document, which keeps memory flat, and that choice explains its refusals, its partial output, and --whole.

dmog starts writing before it has finished reading. A translation of a large document produces its first lines while the rest of the document is still arriving, and the memory it uses does not grow with the document.

What streaming buys #

A tool that reads a whole document before writing needs memory in proportion to the document, and gives nothing back until the end. For a configuration file, that is fine. For a log of a million records, or a document arriving down a pipe from another program, it is not.

dmog reads a document a window at a time. Each format's transducer part turns the text into events as the parser goes, the events flow through the translation, and the target's render writes them as text. Behind the stream, the parser lets go of what has been written. A document ten times as long takes about the same memory.

What streaming costs #

Some documents cannot be written before they have been read to the end.

A repeated key is one. When a JSON object holds name twice, the format's own reading keeps the last value. A stream has already written the first value by the time the second arrives, and cannot take it back. So dmog refuses the document with DUPLICATE_MEMBER instead of writing a value the format would not keep.

A YAML stream of several documents is another. A tree has one root, and a stream of several documents has several. A feed whose own fields come after its first entry is a third, because those fields are written before the entries.

For each of these, the refusal's message names --whole. --whole reads the document first and writes the value the format's reading builds, which settles every one of these cases, at the cost of holding the document in memory. dmog does not fall back to it silently, because a command that sometimes uses memory in proportion to its input is a command nobody can size.

Two formats are always read whole. An arithmetic expression is only complete at its end, because a later operator can take a finished operation as its term. A version string is only a version once all of it has been accepted. dmog formats gives each the reason, in its whole field.

Partial output, and whole records #

A stream can fail part way: a bad record near the end, or a limit reached. By then, some output has been written. dmog says so with "output":"partial" in the failure, so a script knows the output is incomplete.

What it leaves behind is whole records. A renderer marks where each record of its text ends, such as each row of a CSV file or each value inside a JSON document's root. The writer holds a record's text until its end is marked, and drops what it holds when the run fails. A half-written row never reaches the output.

The writer holds at most 32 KiB of an open record. A record longer than that is written as it comes, so that one huge value, such as a style sheet written as a single JSON object, still streams. A failure inside such a record cuts it. The limit trades a guarantee about long records for a guarantee about memory, and memory wins.

How the streaming is held #

Every format the formats table lists is tested to stream, in every build: a document of it must produce its first output before the end of its input arrives, and memory over a document ten times as long must stay within a small bound of the memory over the original. A format that fails either test is recorded as not streaming, with the reason. For what a failure tells a script, see Handle dmog's failures in a script.

This page describes dmog 0.4.0. Edit this page · This page as Markdown