Skip to content

engine: compile apply-to-all equations to loops, not one op per element #1025

Closed as not planned

Description

@bpowers

Every apply-to-all or per-element array equation compiles to one assignment per element, so bytecode size and VM work grow with the number of array elements rather than the number of equations.
The proposal is to compile an apply-to-all equation to a VM loop over its elements instead.

Example

C-LEARN has 934 variables and 5,215 array elements (30,123 with LTM).
The motivating case is four C-LEARN equations of this shape:

sorted target active[COP,Target] = VECTOR ELM MAP(Target is Active[COP,t1], Target Order[COP,Target])

(likewise sorted target type, value, year).
Per-element compilation sets up one VECTOR ELM MAP per COP element instead of sharing work.
On a compiler-cleanup branch that was later replaced by PR #1040, this measured +6.9% run time on plain C-LEARN and +1.0% with LTM.
Whether the same cost exists on main has not been measured.

Why it matters

Only run time; results are correct.
C-LEARN is the only model where this has been measured.

Fix

First, measure plain and LTM C-LEARN run time on main and profile how much goes to unrolled array equations.
Only if that is significant, compile the sorted target * shape as a loop: a destination-writing form of the VM's existing iteration opcodes (BeginIter, LoadIterViewAt, StoreIterElement, NextIterOrJump, EndIter), with wasm parity.
Any equation the loop form cannot handle exactly (EXCEPT, element maps, subranges, members of a simultaneous-equation cycle, ...) stays per-element; the decision must use the resolved subscripts, not a second resolution path.

Pointers

Activity

  1. added
    enhancement
    engine
    on Aug 25, 2026
  2. bpowers commented on Aug 27, 2026

    @bpowers
    OwnerAuthor

    Updated the issue body after the Phase 6b implementation and adversarial review. It now reflects the post-subscript-resolution materializer, makes batched array-producing builtins part of acceptance, and records the exact plain/LTM artifact and runtime residuals carried forward from PR #1032.

  3. changed the title [-]engine: lower plain apply-to-all equations to VM loops instead of compile-time per-element unrolling[/-] [+]engine: compile apply-to-all equations to loops, not one op per element[/+] on Oct 12, 2026
  4. added
    wontfix
    on Oct 12, 2026
  5. bpowers commented on Oct 12, 2026

    @bpowers
    OwnerAuthor

    Closing as won't fix.

    Its +6.9% run-time figure was measured on PR #1032, which was closed and replaced by #1040 (whose body reports run time going down), so the cost on main is unknown. Adding a second lowering tier conflicts with keeping the compiler simple; reopen if a profile on main shows unrolled array equations dominating run time.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    engineenhancementwontfix

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions