Guidedog Manual 1.0.0-beta.16
Language
On this page
Guidedog / Documentation 1.0.0-beta.16

Memory and ownership

Guidedog’s memory design rests on Rust ownership, not on allocators. Every buffer has one owner; borrows end before the owner changes; dropping a value releases it. On top of that, two separate mechanisms bound the work a document can cause: Limits inside a conversion, and a coarse budget for what the host holds.

The host budget counts sources, includes and outputs before they are read. Each conversion runs in a reused workspace bounded by Limits.
Fig. 35 Limits bound a conversion; the budget bounds what the host holds.

Ownership replaces handles

A Workspace owns the node tables, the text pool, the source map, the diagnostics, and the output buffer of one conversion at a time. clear() empties its Vec and String buffers and keeps their capacity, so a worker that converts many documents stops growing them once they fit the largest page. The readers and renderers keep their working storage in the same workspace (see below), so a repeated conversion allocates nothing at all for most reader and renderer pairs. Each rendering worker owns its own workspace; there is no shared mutable engine state and no global pool.

What a conversion returns borrows the workspace: Document<'ws> and the output &str cannot outlive the next conversion, and a stale or foreign handle is a compile error rather than a run-time check. A value that must outlive the conversion is copied into an owned String or Box<str> by the code that keeps it. Shared immutable data (an included file read once and used by several documents) is an Arc<str>.

No memory is planned ahead. Drop releases what an owner holds, and Vec grows as the content needs, so no caller sizes an arena before a session or pairs an owner with a release call.

Working state between conversions

Readers and renderers are stateless descriptors, but parsing and rendering need working storage: line tables, delimiter stacks, inline item lists, anchor tables, table layouts. We keep that storage in the workspace between conversions instead of building it afresh each time. The workspace holds a guidedoc::Scratch store, a pool of values per Rust type, and the contexts lend it out:

#[derive(Default)]
struct TextWork {
    out: String,
    frames: Vec<Frame>,
}

fn render(ctx: &mut RenderContext<'_>) -> Result<(), Fail> {
    let mut work: TextWork = ctx.take_scratch(); // what the last render left
    work.out.clear(); // clear, keep the capacity
    // ... render with work ...
    ctx.put_scratch(work); // the next render takes it again
    Ok(())
}

Taking a value moves it out of the store, so it borrows nothing and the adapter keeps using its context freely. A nested parse of the same reader (a directive body, a table cell) takes another value of the same type, and every value given back is kept, so each nesting level finds its own next time. Once a type’s pool exists, taking and putting back do not allocate. A value that is not given back (an early return on a failure) is simply dropped; the next conversion makes a new one.

Three small tools make the storage reusable:

  • Scratch::take and Scratch::put (ReadContext::take_scratch, RenderContext::take_scratch) keep one struct per adapter: the HTML renderer’s anchor table and walk stack, the Typst renderer’s marks, the reStructuredText reader’s line pools and grid tables, the resolver’s name tables.
  • guidedoc::NameMap replaces HashMap<String, V> for names that are rebuilt every conversion (reference targets, link definitions, slugs, substitutions). It copies every key into one shared String, so clearing it keeps the storage; a HashMap would free each key and allocate it again.
  • guidedoc::recycle keeps a buffer of borrowed values, such as Vec<Line<'s>> whose lines borrow one conversion’s source, as an empty Vec<Line<'static>> between conversions. The standard library collects an emptied vector into one of a same-sized type in place, so no allocation happens.

The anchor table follows the same idea: build_anchors_into rebuilds a table in the storage of the last one, and the table finds ids through an open-addressing index over its own id text rather than a map with String keys. Sorting uses sort_unstable, which does not allocate, wherever the keys are distinct.

tests/integration/tests/allocations.rs counts the allocations of a repeated conversion for every reader and renderer pair and fails if a count grows or exceeds its recorded ceiling. Most pairs allocate nothing. The few allocations that remain are deliberate:

  • A pool warms up over the first conversions. Buffers lent from a pool (the text renderer’s layout strings, the reStructuredText writer’s inline buffers) are taken in a different order by different uses, so a buffer may still grow once in the second conversion; from the third on it fits.
  • Lossy output owns its text. When the reStructuredText writer meets something reStructuredText cannot express, such as an image inside a Markdown paragraph, it reports a note whose arguments the report owns, and it builds the substitution definition’s options as owned strings. These paths are rare and bounded by the document.
  • Output that grows past the largest document seen so far allocates once more, as every Vec does. So do new sources: an included file read for this conversion is an Arc<str> that the conversion owns.

Allocator selection

Rust 1.100 stabilizes allocator-aware Vec and Box operations through std::alloc::Allocator. This selects how a collection obtains storage; it does not make existing allocations faster by itself. Global uses the program’s global allocator, while System explicitly selects the platform allocator. Applications could already choose a process allocator through GlobalAlloc before Rust 1.100.

We keep the default allocator and reusable workspaces. The allocation regression test checks zero repeated allocations for the HTML and Typst sample conversions through all four built-in readers. A different allocator cannot remove work that those conversions already avoid. Making every workspace table allocator-generic would also change the public API, without controlling the allocations in String, the embedded Typst compiler or Graphviz’s C libraries. The host’s budget therefore remains separate from allocation policy.

We measure fresh and reused conversion storage with the measure_conversion example:

cargo run --release -p guidedoc-examples --example measure_conversion -- \
    document.rst rst html 1000 15

Each CSV row records one batch, its input and output sizes and elapsed nanoseconds. Input loading and the first warm conversion occur outside the timer. Fresh batches include workspace construction and destruction; reused batches keep the same workspace. We keep the source, build profile and linked libraries fixed, alternate compiler versions and avoid competing builds when comparing results. Whole-command measurements additionally include process startup, file access and publication. The Rust 1.100 and Graphviz 16.1.1 measurements and allocator decision are recorded in Rust 1.100, Graphviz 16.1.1 and allocation measurements.

Limits bound the work

guidedoc::Limits holds the bounds of one conversion: nodes, text bytes, output bytes, reports, nesting depth, work steps, stack bytes, and includes. Input-driven growth uses try_reserve, so an oversized document produces a Capacity or Limit result that names what ran out, never a panic or an abort. --max-depth, --max-nodes and --stack-kib set these bounds for a build; changing one invalidates cached reading and output.

Recursion is bounded the same way. Workers run on threads with a known 8 MiB stack (gd_host::run_workers, run_with_stack for the main command), and the stack limit of a conversion must fit inside it, so depth and stack capacity agree by construction.

The budget is coarse

gd_host::Budget counts what the host holds that it can size before allocating: sources, included files, compiled diagram and musical score images (with their layout scratch), and the PDF the embedded Typst compiler returns. A Ledger reserves bytes before the read and releases its charges when it is dropped, so a charge cannot outlive the storage it pays for. Rendering units that wait for room join the budget through a guard (Budget::join) and wait for each other without deadlock. limits_within lowers a conversion’s Limits so they fit the budget.

A refused request reports the operation that needed the memory and suggests a --budget that fits, with the machine’s free memory as advice where it is known (GUIDEDOG_AVAILABLE_MIB overrides the probe, which otherwise reads /proc/meminfo). A probe that cannot learn the free memory says so; an unknown amount is never treated as zero.

Single allocations are not metered, and working memory never spills to disk. The global allocator aborts on exhaustion instead of returning a refusal to each caller, and the engine’s growth is already bounded by Limits, so a meter on every allocation would add cost without adding a bound.

Reports outlive workspaces

A report shown after a conversion ends owns its title, source excerpt, and hint: a gd_report::Report holds String values, never a borrow of a workspace. Source spans use byte offsets internally. User-facing columns count Unicode scalars. Terminal markers account for display width separately.

The remaining boundary

Limits and the budget cover Guidedog’s own work. Native code manages its own memory: Graphviz (linked through gd-graphdog), the tree-sitter parsers, and the embedded Typst compiler run in the host process, outside these bounds. The typst program backend and installed plugins run as child processes with their own address spaces. These are different isolation boundaries.

Large PDF books can consume much more memory than the host budget, because the Typst compiler’s memory is outside it. We use operating-system limits when a hard bound is needed. See Error messages for the user’s choices when the budget is reached.

Native music scratch

The native engraver separates retained buffer growth from temporary layout storage. gd_engrave::Options::scratch borrows reserve and release callbacks from the host ledger. A skyline owns its live segment reservation; replacement segments and boundary buffers remain charged while the old skyline is still alive. Spacing sort buffers, candidate rods and break-choice tables use scoped reservations that release on success and on failure. The host ledger’s discharge operation returns these bytes immediately, so repeated merges consume their peak live memory rather than the sum of every intermediate allocation. Existing worker capacity is charged for each engraving because the callback can belong to a different ledger on the next call. Scratch charges reach the build’s shared budget, like every other reservation, so a score near a tight budget can be refused or accepted depending on what other threads hold at that moment; the drawing never changes, only whether it fits, and a refusal reports a budget that fits. An HTML score with a narrow edition engraves twice on one ledger, and the second estimate is charged while the first is still held; the double count is conservative and ends when the score’s ledger is released.