Skip to content

Convert the expr codec from nightly coroutines to a manual iterator and push-struct - #126

Closed
MesTTo wants to merge 1 commit into
trueagi-io:mainfrom
MesTTo:codec-iterator
Closed

Convert the expr codec from nightly coroutines to a manual iterator and push-struct#126
MesTTo wants to merge 1 commit into
trueagi-io:mainfrom
MesTTo:codec-iterator

Conversation

@MesTTo

@MesTTo MesTTo commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

item_source, item_sink, and apply_e used nightly #[coroutine] generators. apply_e is
#[inline(never)] and received the sink as &mut Pin, so the coroutine resume stayed opaque
across that boundary (LLVM does not inline a resume across the Pin/poll edge), paying
state-machine overhead on a trivial per-item body.

This converts item_source to an ItemSource struct implementing Iterator, item_sink to an
ItemSink push-struct (push/finish; the old arity stack only existed to signal completion,
which apply_e never reads), and apply_e to take the sink directly, dropping the Coroutine/Pin
bound. The apply_e_clears_stacks_and_cycles_check macro keeps its by-name contract, so the
space.rs call sites are unchanged, and eval's quote loop becomes a plain for. Two
differentials pin the conversion: diff_item_sink_old_vs_new keeps a frozen copy of the old
coroutine sink and asserts byte-equal output and counts over a ground and adversarial corpus
(coreference, varrefs at the intro boundary, deep nesting, self-referential shapes), and
diff_apply_e_emit_vs_unify_oracle cross-checks the apply_e emit against the independent
Expr::unify substitution path.

Measured, min of 3 interleaved against the pre-change binary on one machine:

  • bench process_calculus: 31,771,686 -> 28,729,938 us, 9.6% faster
  • Nil's bfc chainer (the bench bfc program as a plain mork run MM2 file, proof size 19, 801
    steps, a 2.9M-atom space): 14,057 -> 11,279 ms, 19.8% faster
  • bench finite_domain: 71,632 -> 67,313 us

Byte-identical along the way: every kernel/resources program at several step depths, the bfc
reproduction, and a 4096-hub triangle produce identical result spaces against the pre-change
binary. mork-expr tests 17/17.

Independent of #124; the two touch disjoint files.

…h-struct

item_source/item_sink/apply_e used nightly #[coroutine] generators. Because
apply_e is #[inline(never)] and the sink was passed as &mut Pin into it, the
coroutine resume stayed opaque across that boundary (LLVM does not inline a
coroutine resume across the Pin/poll edge), paying state-machine overhead on
a trivial per-item body. Convert item_source to an ItemSource struct impl
Iterator and item_sink to an ItemSink push-struct (push/finish; the arity
stack was dead weight, since apply_e never reads the sink's completion);
apply_e drops the Coroutine/Pin bound and pushes. The macro's $es:ident
by-name contract keeps the space.rs call sites unchanged, and eval's quote
loop becomes a plain for.

Measured (min-of-3, interleaved against the pre-change binary):
bench process_calculus 31,771,686 -> 28,729,938 us, 9.6% faster; Nil's bfc
chainer at proof size 19 as a plain MM2 run 14,057 -> 11,279 ms, 19.8%
faster; bench finite_domain 71,632 -> 67,313 us.

Byte-identical: every kernel/resources program at several step depths, the
bfc MM2 reproduction, and a 4096-hub triangle produce identical result
spaces against the pre-change binary. Two expr differentials pin the
conversion: diff_item_sink_old_vs_new keeps a frozen reference coroutine
sink and asserts byte-equal output over a ground and adversarial corpus, and
diff_apply_e_emit_vs_unify_oracle cross-checks the apply_e emit against the
independent Expr::unify substitution path. mork-expr 17/17.
@Adam-Vandervorst

Copy link
Copy Markdown
Collaborator

Hi! Appreciate your work here. The MORK kernel is closed to constant-time speed-ups until the asymptotic runtime has no known deficiencies. This prevents us from locking out much more impactful changes. Thanks for your understanding.

@MesTTo

MesTTo commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

Closing per the maintainer's guidance that the MORK kernel is closed to constant-time speed-ups until the asymptotic runtime has no known deficiencies. The codec coroutine->iterator conversion is a constant-factor change, so parking it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants