You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
check: each assignment copies the compile-time value of every variable, so a long function checks in quadratic time #4234
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.
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.
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)
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.
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.
Activity
vmanvs commentedon Dec 3, 2026
I can take this on.
My plan: let
eval_exprread the scopes ofconsteval.envin 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 thetype_checktimes of your example before and after the change.vmanvs commentedon Dec 4, 2026
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, thetype_checkphase, the median of 5 runs. The slowest run was at most 7% above the fastest. Three shapes of program: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
forget_value_facts_about. check: a long function no longer checks in quadratic time #4243 fixes it.name_the_old_value(src/hir/check/raw.rs). Each assignment runsexpr_mentionson 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_forcopies only the names that an expression reads, for aletand anassert. This issue needs the same thing for an assignment, a loop and an expression statement. #4243 also changessrc/hir/stmt.rs.Should I wait for #4243 and use
consteval_env_forfor the other callers? Or should I make the change now, with my own helper? I can also open an issue for cost 2.minzyfox commentedon Dec 4, 2026
Lets wait for #4243. Or you can meanwhile rebase/merge with that change and validate if it works with the other PR.
hiraditya commentedon Dec 5, 2026
The
letpath makes the same copy, but not throughconsteval_snapshot.check_let_decl_stmtand the assert check insrc/hir/stmt.rseach build their owntmp_envfrom every scope ofconsteval.env. So a change toconsteval_snapshotalone 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:A profile of N = 8,000 puts 82% of the time in
check_let_decl_stmt→HashMap::insert.