Repository navigation
perf: discover tools and resources via TypeCache instead of scanning every assembly - #1358
ananttheant wants to merge 2 commits into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Team Run ID: 📒 Files selected for processing (1)
Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review. 📝 WalkthroughWalkthroughTool and command discovery now uses Unity ChangesTypeCache-first discovery
Estimated code review effort: 3 (Moderate) | ~20 minutes Merge Risk: ⚪ Minimal · up to The PR replaces repeated assembly scans with indexed type discovery while retaining a fallback for an entirely empty index, and the supplied EditMode and compile checks pass. No actionable merge-blocking risk remains beyond normal checks and review. Suggested reviewers: Sequence Diagram(s)sequenceDiagram
participant CommandRegistry
participant TypeCache
participant LoadedAssemblies
CommandRegistry->>TypeCache: Get tool and resource types
alt both TypeCache results are empty
CommandRegistry->>LoadedAssemblies: Enumerate non-dynamic loaded types
LoadedAssemblies-->>CommandRegistry: Return reflected types
else cached types found
TypeCache-->>CommandRegistry: Return cached types
end
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Linked Issues checkExplanation The changes satisfy issue
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
…every assembly CommandRegistry.AutoDiscoverCommands walked every type of every loaded assembly and then ran two full attribute-matching passes over that list, on every domain reload. ToolDiscoveryService.DiscoverAllTools was worse: it queried TypeCache first and then ran the same exhaustive AppDomain walk anyway, unconditionally, merging the two results. Both now read from TypeCache, Unity's prebuilt attribute index, and keep the exhaustive walk only as a fallback for when the index comes back empty — the domain-reload state the old comments were guarding against. ResourceDiscoveryService already trusted TypeCache alone, so this brings the other two in line with it. Measured in TestProjects/UnityMCPTests (193 assemblies, 44,195 types) on Unity 2022.3.62f1: the exhaustive scan takes 310.63 ms versus 0.01 ms for TypeCache, and both find exactly the same 54 types. Real projects carry more assemblies, which is where the multi-second reload cost reported in the issue comes from. Closes CoplayDev#1336
RegisterCommandType lets a duplicate command name overwrite the previously registered handler, so registration order decides which type wins a collision. Neither TypeCache nor the exhaustive fallback documents an order, while the assembly scan they replace was stable within a build -- so a name collision could otherwise resolve differently between domain reloads. Orders both discovery paths by Type.FullName (ordinal). ~54 types, so the cost is noise against the ~310ms this PR removes. Added by a maintainer while rebasing this PR onto beta; the same fix was raised in review on CoplayDev#1364.
db7eb72 to
4d01462
Compare
|
Superseded by #1364, which landed the same TypeCache switch — closing this. Yours is the better change: you dropped the exhaustive fallback entirely rather than keeping it for the empty-index case, and the For whatever it is worth, the numbers I measured in |
Description
CommandRegistry.AutoDiscoverCommandswalks every type of every loaded assembly and then runs two full attribute-matching passes over that list, once per domain reload.ToolDiscoveryService.DiscoverAllToolsis worse: it queriesTypeCachefirst and then runs the same exhaustiveAppDomainwalk anyway — unconditionally, merging and de-duplicating the two results — so the fast path never actually saved anything.ResourceDiscoveryServicealready trustsTypeCachealone, so this brings the other two in line with it.Measured in this repo's own
TestProjects/UnityMCPTestson Unity 2022.3.62f1 — 193 non-dynamic assemblies, 44,195 types, averaged over 5 runs after warm-up:TypeCacheBoth find exactly the same 54 types, which is the important part: this is a pure cost reduction, not a change in what gets discovered. That is 310 ms per domain reload in a near-empty project; the multi-second figure in #1336 comes from real projects carrying far more assemblies.
Type of Change
Changes Made
CommandRegistry.AutoDiscoverCommandsreads tools and resources fromTypeCache. The old assembly walk moves into aGetAllLoadedTypes()helper, reached only when the index returns nothing at all for this domain.ToolDiscoveryService.DiscoverAllToolsruns theAppDomainscan only whenTypeCachecame back empty, instead of always running it and merging.CommandRegistry.ShouldFallBackToExhaustiveScan(int, int)pure predicate expressing that rule, following the existingShouldAbandonBusyPort/ShouldKeepWaitingForReadypattern, plus an EditMode test covering all four combinations.The one behavioural trade-off worth your call: the old code always merged the exhaustive scan in, so a type that
TypeCachesomehow missed while other types resolved would still have been found. Now a non-empty index is treated as authoritative. I chose this because it keeps a safety net for the "index not ready" case the original comments describe while removing the cost from the normal path — but if you would rather always merge, or gate the fallback differently (say, per-attribute rather than on the combined count), that is a small change toShouldFallBackToExhaustiveScanand its call site.Compatibility / Package Source
file:(local clone, branchperf/typecache-command-discoveryoffbeta)Packages/packages-lock.json: n/a (file reference)TypeCacheisUnityEditorAPI available well below this package's 2021.3 floor, so no version gating is needed.Testing/Screenshots/Recordings
Full EditMode suite on 2022.3.62f1: 1121 passed, 0 failed (66 pre-existing ignores). The existing
CommandRegistryTests.AutoDiscovery_RegistersAllBuiltInToolsandToolDiscoveryServiceTestsare the real regression guard here — they assert the built-in tools still resolve, and they pass unchanged.Documentation Updates
Related Issues
Closes #1336
Additional Notes
The benchmark above came from a throwaway test I did not commit; it timed both strategies in the same editor session and asserted their results matched. Happy to add it as a permanent
[Explicit]test if that is useful to you, though wall-clock assertions in CI tend to be flaky, which is why I left it out.Summary by CodeRabbit