Skip to content

Optimize

8 exports Added in v1.0.0 Source

Instances

Deep

Added in v1.0.0 Source

Signature

declare const Deep: FusionDepth;

Shallow

Added in v1.0.0 Source

Signature

declare const Shallow: FusionDepth;

Model

Deep interface

Added in v1.0.0 Source

Instructs the document fusion optimizer to recurse into all leaves of the document tree, including different layout alternatives and all location-sensitive values (i.e. those created by nesting), which cannot be fused before, but only during, the layout process. As a result, the performance cost of using deep document fusion optimization is often hard to predict and depends on the interplay between page layout and the document that is to be pretty printed.

This value should only be utilized if profiling demonstrates that it is significantly faster than using Shallow.

Signature

interface Deep {
  readonly _tag: "Deep";
}

FusionDepth type

Added in v1.0.0 Source

Represents an instruction that determines how deeply the document fusion optimizer should traverse the document tree.

Signature

type FusionDepth = Shallow | Deep;

Optimize interface

Added in v1.0.0 Source

Represents optimization of a given document tree through fusion of redundant document nodes.

Signature

interface Optimize<A> {
  (depth: FusionDepth): Doc<A>;
}

Shallow interface

Added in v1.0.0 Source

Instructs the document fusion optimizer to avoid diving deeply into nested documents, fusing mostly concatenations of text nodes together.

Signature

interface Shallow {
  readonly _tag: "Shallow";
}

Optimization

optimize

Added in v1.0.0 Source

The optimize function will combine text nodes so that they can be rendered more efficiently. An optimized document is always laid out in an identical manner to its un-optimized counterpart.

When laying a Doc out to a SimpleDocStream, every component of the input document is translated directly to the simpler output format. This sometimes yields undesirable chunking when many pieces have been concatenated together.

It is therefore a good idea to run fuse on concatenations of lots of small strings that are used many times.

Signature

declare const optimize: {
  (depth: FusionDepth): <A>(self: Doc<A>) => Doc<A>;
  <A>(self: Doc<A>, depth: FusionDepth): Doc<A>;
};

Example

import * as Doc from "@effect/printer/Doc"
import * as Optimize from "@effect/printer/Optimize"

// The document below contains a chain of four entries in the output `DocStream`
const inefficient = Doc.hsep([Doc.char("a"), Doc.char("b"), Doc.char("c"), Doc.char("d")])

// However, the above document is fully equivalent to the tightly packed
// document below which is only a single entry in the output `DocStream` and
// can be processed much more efficiently.
const efficient = Doc.text("abcd")

// We can optimize the `inefficient` document using `Optimize`
Optimize.optimize(Optimize.Deep)(inefficient)

Other

Optimize

Added in v1.0.0 Source