dannymcgee / vscode-wgsl

Rich language support for WebGPU Shading Language
Apache License 2.0
14 stars 2 forks source link

`naga_oil` support: Import resolution #6

Closed dannymcgee closed 6 months ago

dannymcgee commented 6 months ago

Features that are currently working

Features that likely need upstream changes (e.g. in naga_oil)

Types of imports that currently resolve to the correct declarations

Import types that will require additional work

Housekeeping

server::utils::find_decl is probably the fugliest abstraction I've ever written — it's awkward to use and hard to read and reason about. Some high-level refactoring might be a good idea — maybe a PostUpdate system to find the declarations for all identifier tokens and store them in a HashMap or something after a file has been parsed and validated?

Update: There is now a PostUpdate system that runs through every identifier token of every parsed WGSL file to find its declaration location. If found, it adds:

Once this is done, feature providers like hover, go-to-definition, and find-all-references can just look up the result in the corresponding hashmap.

However, the current implementation as written is actually slower than the previous solution, because it's now doing this every single update. Updates occur whenever a notification or request is received from the client. Since every WGSL document read involves a separate notification, the system re-processes every new and existing document every time a new document is discovered and parsed, which happens for every WGSL document in the workspace on startup.

One option for optimization would be to wait until all pending documents are read and parsed before running this system for the first time. However, we would still end up re-processing every document in the workspace every time any document is updated (i.e. while the user is typing), which is less than ideal.

A more robust optimization (but trickier to implement) would be to wait until the first time we receive a request for one of the features dependent on this processing, and thereafter only re-process documents which have changed or whose dependencies have changed (the latter bit being the tricky part).

Update 2: The optimizations mentioned above are now done, and as a bonus, each document should now have Dependencies and Dependents components (both wrappers around EntityHashSets), which will likely come in handy for other purposes in the future.