PEGO

A190: Evaluation Context Reuse

Every action and predicate needs an evaluation context, but its scratch identity is not part of the grammar result. Reusing one per parser avoids allocating a context at every evaluation.

Mechanism and Invariants

useCtx refreshes rule range, captured values and environment for the current evaluation. Nested lambda calls use their own preserved context when required. No context reference may outlive its allowed evaluation scope; values returned by constructors retain separate ownership.

Scope and State

Default per-parser context reuse in closure, both VMs, generated Go Node/typed Go and TypeScript. TypeScript reuses within one invocation, not across parser calls.

Reference Control

No maintained same-source fresh-context-only control exists.

Evidence and Implementation

Record 010 combines context reuse with token substring sharing and reports reduced allocation counts. That combined result cannot assign a separate context speedup, and direct typed code may avoid the context entirely.

Evidence records: 010.

Source owners: parser.useCtx, generated useCtx, typed context, Parser.useCtx.

The comparison policy distinguishes same-source controls, historical changes and full checkpoints.

Related mechanisms: A040, A130.

Optimization Catalog