Repository navigation
Description
Part of #14 (speedup candidate for upstream; under the 2026-11-23 policy, speedups are not held back).
Repro
One file of N one-line methods on a ---@class table. The generator runs as written:
gen() { mkdir -p "$2"; { echo '---@class C'; echo '---@field x integer'; echo 'local C = {}'
for i in $(seq 1 "$1"); do echo "function C:m$i() $3 end"; done; echo 'return C'; } > "$2/a.lua"; }
gen 2000 /tmp/f2000 'return C.x'
/usr/bin/time -f '%es %MKB' emmylua_check /tmp/f2000Wall times, single runs on myxps (2026-11-24). Upstream = main-48ef52a0, fork = 0.30.0-main-53405821:
| N | body | upstream | fork |
|---|---|---|---|
| 1000 | return 1 |
0.23 s | 0.23 s |
| 2000 | return 1 |
0.33 s | 0.36 s |
| 1000 | return C.x |
0.83 s | 0.75 s |
| 2000 | return C.x |
6.08 s | 3.88 s |
| 1000 | return self.x |
0.94 s | 0.77 s |
| 2000 | return self.x |
5.50 s | 4.15 s |
| 4000 | return self.x |
42.3 s | 28.7 s |
| 8000 | return self.x |
>120 s | >120 s |
So every doubling costs about 6–8×, cubic-like, but only when the bodies read a member. --profile puts all checkers together at about 0.2 s of the 3.9 s, so the time goes in analysis, before checking.
Where (stack)
gndx emmylua stack <fork> self4k --after=12, the one busy thread:
semantic_db::query::merge_canonical_members
semantic_db::query::members_of_owner
semantic_db::query::resolve_member_id
semantic_db::index::references::build_file_references
semantic_db::update::rebuild_file_after_write
Why (hypothesis, no control yet)
build_file_references calls resolve_member_id once per member use. Its last loop (query.rs, for resolved in resolve_owner_set(..) { for member in members_of_owner(db, resolved) … }) asks for all of the owner's members, only to find the one whose key matches. members_of_owner → merge_canonical_members dedups the workspace index's bucket against raw and extra with nested .iter().any(..), which is O(N²) per call even when every member is already known and raw is returned unchanged. N uses × O(N²) = O(N³).
The code is the same upstream; the fork's lower constant comes from elsewhere, not measured. enclosing_closure_at's linear token_at_offset (the 1954Z handoff's suspect) is not what dominates here.
Possible fixes (unprobed)
- In
merge_canonical_members, dedup through aHashSet<(id, file_id)>ofraw. That makes it O(N) per call, O(N²) overall. - In
resolve_member_id, for a name key, askmembers_of_owner_named(db, resolved, name)instead of scanning every member. That bucket is already indexed by name. Each use then costs about O(1).
Both are small. (2) attacks the root, if the named bucket holds every member that member.key == member_use.key matches (unverified for non-name keys). Next: prototype them, time the table above, corpus diff against up-48ef52a0-1123.
Activity
mareklangiewicz commentedon Nov 24, 2026
Fix 2 landed on the fork: 7f7ae3a (installed, gated; not pushed yet).
In
resolve_member_id's last loop, aNamekey now asksmembers_of_owner_namedinstead of scanning the wholemembers_of_owner. Why the two lists are the same: eachOwnerMembersbucket'sby_keylists hold the same members asall, in the same insertion order, keyed by the member's ownLuaMemberKey. The canonical (module) bucket is built the same way, andmerge_canonical_membersdedups by(id, file_id), so filtering by key commutes with the merge. Other keys keep the full scan. Fix 1 (a HashSet inmerge_canonical_members) was not needed for this repro and is not done.Release, one
outcomerun each, capped unit, quiet machine:return 1(control)return C.xreturn C.xreturn self.xreturn self.xself.xstill roughly triples per doubling, so a quadratic part remains. It is small at these sizes and not chased (#34 policy).assign_type_mismatch_test::test_many_member_reads_on_a_module_class_stay_fasthas 1500self.xmethods and anntesttimeout of 5 s. It also asserts thatC.xstill resolves to its@field(a---@type stringread reports assign-type-mismatch). Green debug build: 0.80 / 0.75 / 0.73 s.--red-revert=7f7ae3aagave INCONCLUSIVE (timeout only, as lab: test --red-*: a filter red only by an ntest timeout reads NOT RED when another filter failed #85 now reports it). The red binary run alone timed out at 5.00 s in 5 of 5 runs, at load about 2.fork-53405821-1124→fix86-1124: 56762 → 56762 hits, 0 changed lines, 60 of 60 trees. Summed check time 62.0 s → 62.0 s: the corpus has no tree of this shape.gndx emmylua test2286 passed.lintPASSES (fmt, clippy).install --with-gate: CANDIDATE PASSES, switched to0.30.0-main-7f7ae3aa.gndx gate allPASS (luagnd 4131, myhypr 571, neospector 1275 nodes, all emmylua CLEAN).gndx nvim dev check emmyluaCLEAN.Next: push the fork,
tracker state --write, and port it upstream (same code at 48ef52a). Under the speedup policy that is a PR made ready when done, after a three-way check on an upstream-main branch.mareklangiewicz commentedon Nov 24, 2026
/simplify pass on 7f7ae3a → c06f116 (test and comments only; the installed engine is unchanged).
Test. The oracle was
!has_no_diagnostic, which holds for any assign-type-mismatch in the file. It now asks for exactly one, on thelocal s = C.xline, with only that checker enabled. Undercheck_source(every checker) the 1500 methods take more than 5 s in a debug build. 146assign_type_mismatch_testtests pass, lint PASSES.Fix 1 measured, parked. A
HashSet<(SemanticId, FileId)>dedup inmerge_canonical_membersmade no difference against 7f7ae3a (release,outcome):C.xself.xobj:mI()(member_type_via_owner_method)It builds a set on every call for every module owner, and nothing showed it paying for that, so it was reverted. Revisit when a fixture shows
members_of_ownerhot.Not done, and why:
member_closure_across_files,member_type_via_ownerandmember_type_via_owner_methodloop over the fullmembers_of_ownerfiltered bymember.name == name. They cannot switch to the named bucket as they are:namethere ismember_use.key.to_path()/get_path_part(), so an integer key arrives as"[1]"and matches an integer member only through that string. The sound form is a lookup byLuaMemberKey. That changes matching outside this diff, and the instance-read fixture above shows no slowdown on this path today.members_of_owner_keybeside the model'sSemanticModel::members_of_owner_key. The model version routes through its own caches, so one would not replace the other.SmolStrclones,(owner, SmolStr::new(name))cache keys,logical_module_owner_idper call).self.xstill roughly triples per doubling (0.65 s → 2.09 s for 2000 → 4000). Its cause is elsewhere, not in these merges. Not chased.perf(semantic_db): a name-keyed member use reads the owner's by-name …
test(#86): pin the one assign-type-mismatch on `C.x`; shorter comment…
mareklangiewicz commentedon Nov 24, 2026
Upstream: draft PR 1458 (
perf-member-use-named, commit 80b5270 on main 1460735). The fix is re-measured on main, not carried over from the fork:return 1return C.xreturn self.xquery.rs, the test binary timed out at 5.00 s in 5 of 5 runs; the PR's passes in 0.71 to 0.74 s. 1994 tests pass, and fmt and clippy pass.up-14607355-1124→upnamed-1124on the 54 trees main finishes: 57305 → 57306 hits, 11 changed lines. All 11 are on record literals in files where main alone varies run to run:corpus repeat upnoise-1124, 3 runs, 9 to 13 hits differ in blink.cmp, diffview and gitsigns. Main still lacks the fork A two-target assign-type-mismatch is reported on either line, run to run #59 fix (upstream 1437).Next: CI and bot reviews, then mark it ready (speedup rule).
mareklangiewicz commentedon Nov 24, 2026
Upstream 1458 is marked ready (2026-11-24). CI: 15 pass, 1 skipped (release). The bot review is answered. Its equivalence point got a test,
p10_named_members_are_the_full_list_filtered_by_name(upstream 5350e92, fork 49f4422).