Skip to content

Perf: N member uses on a class with N members is cubic (merge_canonical_members per resolve_member_id) #86

Backlog

Description

@mareklangiewicz

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/f2000

Wall 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)

  1. In merge_canonical_members, dedup through a HashSet<(id, file_id)> of raw. That makes it O(N) per call, O(N²) overall.
  2. In resolve_member_id, for a name key, ask members_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

  1. mareklangiewicz commented on Nov 24, 2026

    @mareklangiewicz
    OwnerAuthor

    Fix 2 landed on the fork: 7f7ae3a (installed, gated; not pushed yet).

    In resolve_member_id's last loop, a Name key now asks members_of_owner_named instead of scanning the whole members_of_owner. Why the two lists are the same: each OwnerMembers bucket's by_key lists hold the same members as all, in the same insertion order, keyed by the member's own LuaMemberKey. The canonical (module) bucket is built the same way, and merge_canonical_members dedups by (id, file_id), so filtering by key commutes with the merge. Other keys keep the full scan. Fix 1 (a HashSet in merge_canonical_members) was not needed for this repro and is not done.

    Release, one outcome run each, capped unit, quiet machine:

    N body upstream 48ef52a fork 5340582 fork 7f7ae3a
    2000 return 1 (control) 0.32 s 0.35 s 0.34 s
    1000 return C.x 0.70 s 0.61 s 0.26 s
    2000 return C.x 4.27 s 3.11 s 0.41 s
    2000 return self.x 4.44 s 3.31 s 0.64 s
    4000 return self.x 32.75 s 22.95 s 1.95 s

    self.x still roughly triples per doubling, so a quadratic part remains. It is small at these sizes and not chased (#34 policy).

    • Test: assign_type_mismatch_test::test_many_member_reads_on_a_module_class_stay_fast has 1500 self.x methods and an ntest timeout of 5 s. It also asserts that C.x still resolves to its @field (a ---@type string read reports assign-type-mismatch). Green debug build: 0.80 / 0.75 / 0.73 s. --red-revert=7f7ae3aa gave 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.
    • Corpus: 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.
    • Gated path: gndx emmylua test 2286 passed. lint PASSES (fmt, clippy). install --with-gate: CANDIDATE PASSES, switched to 0.30.0-main-7f7ae3aa. gndx gate all PASS (luagnd 4131, myhypr 571, neospector 1275 nodes, all emmylua CLEAN). gndx nvim dev check emmylua CLEAN.

    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.

  2. mareklangiewicz commented on Nov 24, 2026

    @mareklangiewicz
    OwnerAuthor

    /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 the local s = C.x line, with only that checker enabled. Under check_source (every checker) the 1500 methods take more than 5 s in a debug build. 146 assign_type_mismatch_test tests pass, lint PASSES.

    • Fix 1 measured, parked. A HashSet<(SemanticId, FileId)> dedup in merge_canonical_members made no difference against 7f7ae3a (release, outcome):

      fixture 7f7ae3a + HashSet
      N=2000 C.x 0.40 s 0.43 s
      N=4000 self.x 2.02 s 2.09 s
      N=4000 obj:mI() (member_type_via_owner_method) 1.72 s 1.64 s

      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_owner hot.

    • Not done, and why:

      • member_closure_across_files, member_type_via_owner and member_type_via_owner_method loop over the full members_of_owner filtered by member.name == name. They cannot switch to the named bucket as they are: name there is member_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 by LuaMemberKey. That changes matching outside this diff, and the instance-read fixture above shows no slowdown on this path today.
      • A query-level members_of_owner_key beside the model's SemanticModel::members_of_owner_key. The model version routes through its own caches, so one would not replace the other.
      • The constant costs per call (SmolStr clones, (owner, SmolStr::new(name)) cache keys, logical_module_owner_id per call).
    • self.x still roughly triples per doubling (0.65 s → 2.09 s for 2000 → 4000). Its cause is elsewhere, not in these merges. Not chased.

  3. added 2 commits that reference this issue on Nov 24, 2026
  4. mareklangiewicz commented on Nov 24, 2026

    @mareklangiewicz
    OwnerAuthor

    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:

    N body main 1460735 PR
    2000 return 1 0.34 s 0.34 s
    2000 return C.x 3.55 s 0.42 s
    4000 return self.x 25.61 s 1.99 s
    • With main's query.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.
    • Corpus up-14607355-1124 → upnamed-1124 on 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).
    • The fork corpus, which also covers the trees main crashes on, had 0 changed lines.

    Next: CI and bot reviews, then mark it ready (speedup rule).

  5. mareklangiewicz commented on Nov 24, 2026

    @mareklangiewicz
    OwnerAuthor

    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).

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

    needs-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions