Repository navigation
perf(semantic_db): a name-keyed member use reads the owner's by-name bucket - #1458
mareklangiewicz wants to merge 3 commits into
Conversation
…bucket `resolve_member_id`'s last loop scanned every member of each resolved owner to find the one whose key matches, and `members_of_owner` merges a module owner's canonical bucket with a nested `iter().any` dedup, O(N^2) per call. N member reads on an N-member module class were O(N^3) per file: 4000 methods reading `self.x` took 25.6 s, now 2.0 s. For a `Name` key it now asks `members_of_owner_named`. Each owner bucket's by-key list holds the same members, in the same order, as its full list filtered by key, and the canonical merge dedups by `(id, file_id)`, so filtering commutes with it. Other keys keep the full scan. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018NxUUPwEiW13FMdt1RXj4N
There was a problem hiding this comment.
Review Summary
The performance optimization is sound in principle: switching a name-keyed lookup from a full scan to a by-name bucket is the right fix for the O(N³) blowup. The test is well-constructed and the enable_only guard is a nice touch for debug builds. A few points warrant attention.
1. Behavioral equivalence assumption is unverified (correctness risk — medium)
The comment asserts members_of_owner_named yields "the same members, in the same order" as the full list filtered by name. This is the critical invariant, but the diff doesn't show the implementation of members_of_owner_named. If the bucket is, e.g., an IndexMap keyed differently, a HashSet, or sorted differently from members_of_owner, the returned order could differ.
- Impact: the
member.id.clone()return and downstreammember_factsselection may pick a different member from before when multiple members share a name (overloads, merged declarations, base/derived members with the same name). Resolution results could silently change. - Recommendation: confirm ordering parity explicitly, and ideally add a test with duplicate method names across declarations to lock it in.
2. name.clone() inside the per-use loop (minor perf regression risk)
LuaMemberKey::Name(name) => members_of_owner_named(db, resolved, name.clone()),This is inside a loop over resolve_owner_set, so the name is cloned once per owner. That's usually cheap, but consider passing &str (or &LuaMemberKey) if members_of_owner_named can take a borrowed key — it avoids per-owner allocation in what is a hot path (this is the very function being optimized for performance).
3. The test doesn't actually prove the old code was O(N³) / that this is a regression guard for the right thing
- The test validates correctness (
C.xresolves to the@field, one diagnostic at the expected line) and speed (#[ntest::timeout(5000)]). - Concern: a 5s timeout is very loose. The stated failure mode was O(N³) over 1500 methods; if the optimization regressed to an intermediate O(N²), the test might still pass comfortably. Consider tightening the class size or timeout so the test fails fast on a real regression. Wall-clock timeouts are also flaky on loaded CI machines — a few microseconds-per-op bound is more robust, or at minimum document the expected margin.
- The comment "3 header lines … then
local s = C.x" assumes exact line counting.assert_eq!(lines, [METHODS + 4])is correct given the constructedsrc, but it's brittle to any future header change. Fine for a test, just note the coupling.
4. Test relies on enable_only side effect — isolation
ws.analysis.diagnostic.enable_only(...) mutates the analysis config on the shared ws. Since ws is local to the test this is fine, but if VirtualWorkspace is reused/cached across tests, this could leak state. Confirm new() gives a fresh instance (it appears to, so likely fine).
5. Missing coverage for the fallback path
The _ => members_of_owner(db, resolved) arm (index keys, method keys, etc.) is unchanged but now adjacent to new logic. Since the hot path was name-based, existing tests likely cover index keys — but it's worth a quick check that no test regressed on member resolution for integer/other keys, given this function is central.
6. Minor style: comment density
The two comments are helpful and accurate. Just make sure the "same members, in the same order" claim in the code comment matches reality (see #1) — an inaccurate comment here is worse than none, as it asserts an invariant reviewers will trust.
Verdict
Direction is correct and the fix is valuable. Blocking concern: verify ordering/identity parity of members_of_owner_named vs. filtered members_of_owner (especially under duplicate names), and consider tightening the timeout to make the regression guard meaningful. The clone() and test-brittleness items are non-blocking nits.
…d by name Pins the invariant `resolve_member_id` now relies on, for a module owner with a member another file adds through `require` (so the canonical merge runs), repeated `f` declarations, and an `@field` beside a runtime member of one name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018NxUUPwEiW13FMdt1RXj4N
|
Thanks. Point by point:
|
|
please resolve the conflict |
Upstream deleted semantic_db/phase_tests/ (92d2e12, "clean and migrate test"); this branch's p10 test (p10_named_members_are_the_full_list_filtered_by_name) moves to semantic_db/tests/query_tests.rs as test_named_members_are_the_full_list_filtered_by_name, on that file's setup() plus the main workspace at portable_test_root(). In assign_type_mismatch_test.rs both sides appended a test; kept both. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018NxUUQZHt6a5njk8trY5BZ
Summary
Resolving a member use is cubic in the size of a module class.
resolve_member_idends by scanning every member of each resolved owner for the one whose key matches, andmembers_of_ownermerges a module owner's canonical bucket with a nestediter().anydedup, O(N²) per call:A
Namekey now reads the owner's by-name bucket:// semantic_db/query.rs, resolve_member_id for resolved in resolve_owner_set(db, owner.clone()) { - for member in members_of_owner(db, resolved).iter() { + let members = match &member_use.key { + LuaMemberKey::Name(name) => members_of_owner_named(db, resolved, name.clone()), + _ => members_of_owner(db, resolved), + }; + for member in members.iter() {The two lists hold the same members. Each
OwnerMembersbucket'sby_keylists hold the same members asall, in the same insertion order, keyed by the member's ownLuaMemberKey. The canonical bucket is built the same way, and the merge dedups by(id, file_id), so filtering by key commutes with it. The key check inside the loop stays. Other keys keep the full scan.Evidence
emmylua_checkwith an empty config on one generated file:---@class C,---@field x integer,local C = {}, N one-line methods,return C. CPU time, one run each:return 1(no member read)return C.xreturn C.xreturn self.xreturn self.xtest_many_member_reads_on_a_module_class_stay_fast(new,check/test/assign_type_mismatch_test.rs) has 1500self.xmethods and a 5 sntesttimeout. It asserts that the one assign-type-mismatch is onlocal s = C.x(---@type string), soC.xstill resolves to its@field. With main'squery.rsthe test binary timed out at 5.00 s in 5 of 5 runs.After: it passes in 0.71 to 0.74 s (debug build). All 1994
emmylua_code_analysistests pass, as docargo fmtandclippy.emmylua_check, empty config, 54 Neovim plugin trees that finish on main): 57305 → 57306. The 11 changed lines areassign-type-mismatchon record literals in blink.cmp, diffview.nvim and gitsigns.nvim, files where main itself varies run to run (three runs of main differ by 9 to 13 hits there). Total time over those trees, one run each: 54.0 s → 53.4 s.Merge Danger
Door: two-way
A different list from the same index on one existing path. Reverting it brings back the scan.
Blast Radius: member resolution
The named bucket is the full list filtered by key, so the member found is the same. One difference is possible in principle: when an owner's by-name index bucket is empty, the named path falls back to the owner file's
FileFacts, while the full path does so only when the whole owner bucket is empty. The corpus above shows no changed diagnostic.🤖 Generated with Claude Code
https://claude.ai/code/session_018NxUUPwEiW13FMdt1RXj4N