Skip to content

check: each assignment copies the compile-time value of every variable, so a long function checks in quadratic time #4234

Backlog

Description

@minzyfox

Each assignment to a variable copies the compile-time value of every variable in scope. So a function with many variables and many assignments checks in quadratic time:

fn main() -> i32 {
  let v0 : i64 = 0;
  let v1 : i64 = 1;
  // ... N lets
  let mut t : i64 = 0;
  t = t + v0;
  t = t + v1;
  // ... N assignments
  print(t);
  return 0;
}

type_check time with a release build on main c1c1398 (VX_PIPELINE_PHASES=1, -j 1):

variables type_check
1000 143 ms
2000 540 ms

A profile puts most of the time in check_assign_stmt → consteval_snapshot, and in hashing and HashMap::insert.

Cause: in check_assign_stmt (src/hir/stmt.rs), an assignment to a name or to an element calls consteval_snapshot() to evaluate the right side. consteval_snapshot (src/hir/check/control.rs) builds a new HashMap with every entry of every scope in self.consteval.env. A loop also takes a copy before it starts and another in settle_loop_consteval.

A possible fix: let eval_expr read the scopes of consteval.env directly, with no copy. A loop can record only the names that it assigns.

#944 is a similar cost for comptime blocks, in observe_comptime_block.

Activity

  1. added
    core-lang
    enhancement
    on Dec 3, 2026
  2. vmanvs commented on Dec 3, 2026

    @vmanvs
    Contributor

    I can take this on.

    My plan: let eval_expr read the scopes of consteval.env in place, innermost scope first, with no copy. Callers that keep a "before" state, such as loops and calls, keep a copy of only the names that they assign. I will give the type_check times of your example before and after the change.

  3. vmanvs commented on Dec 4, 2026

    @vmanvs
    Contributor

    I measured main before I changed the code. The issue's program is quadratic, but two other costs are in the same numbers.

    Setup. A release vxc at a45c8f1, on a GitHub runner (AMD EPYC 9V74, 4 cores, Ubuntu 26.04.1). VX_PIPELINE_PHASES=1 vxc -j 1 --emit-mlir, the type_check phase, the median of 5 runs. The slowest run was at most 7% above the fastest. Three shapes of program:

    • both: N variables and N assignments (the program of this issue)
    • many_vars: N variables and 1 assignment
    • many_assigns: 1 variable and N assignments
    N both many_vars many_assigns
    500 40.1 ms 12.7 ms 5.7 ms
    1000 157.3 ms 46.5 ms 14.9 ms
    2000 674.3 ms 186.2 ms 47.6 ms
    4000 2879.3 ms 822.8 ms 188.0 ms
    8000 12379.0 ms 3610.9 ms 755.5 ms

    All three shapes take about 4 times as long when N doubles. The script, every run and the machine details are in the artifact of https://github.com/vmanvs/Vx/actions/runs/47292518525.

    The two other costs

    1. many_vars is check: each let and each end of a scope scans every fact, so a long function checks in quadratic time #4237. Each new variable scans every fact in forget_value_facts_about. check: a long function no longer checks in quadratic time #4243 fixes it.
    2. many_assigns comes from name_the_old_value (src/hir/check/raw.rs). Each assignment runs expr_mentions on every fact to find the facts that name the variable. Only the newest fact names it. I found no issue for this. A map from each name to the facts that name it would fix it, as check: each let and each end of a scope scans every fact, so a long function checks in quadratic time #4237 suggests.

    In "both", each assignment pays for the copy of this issue and also for the scan of cost 2 over the facts of the lets. So removing the copy alone leaves a part of the quadratic time.

    The overlap with #4243. Its consteval_env_for copies only the names that an expression reads, for a let and an assert. This issue needs the same thing for an assignment, a loop and an expression statement. #4243 also changes src/hir/stmt.rs.

    Should I wait for #4243 and use consteval_env_for for the other callers? Or should I make the change now, with my own helper? I can also open an issue for cost 2.

  4. minzyfox commented on Dec 4, 2026

    @minzyfox
    ContributorAuthor

    Lets wait for #4243. Or you can meanwhile rebase/merge with that change and validate if it works with the other PR.

  5. hiraditya commented on Dec 5, 2026

    @hiraditya
    Collaborator

    The let path makes the same copy, but not through consteval_snapshot. check_let_decl_stmt and the assert check in src/hir/stmt.rs each build their own tmp_env from every scope of consteval.env. So a change to consteval_snapshot alone does not remove this cost.

    A function of N lines of the form let v_i = v_(i-1) + 1; takes these times to check in a debug vxc:

    N time
    2,000 1.3 s
    4,000 5.2 s
    8,000 20.9 s

    A profile of N = 8,000 puts 82% of the time in check_let_decl_stmt → HashMap::insert.

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

    core-langenhancementperformance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions