Generate script resource preview without parsing #87625
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
During the last days of development of Godot 4.2 we were fighting a lot with deadlocks during the editor startup.
One of the sources of these deadlocks was the editor resource preview generator. Specifically, the one implementation responsible for generating previews for script files. Specifically specifically, GDScript files. The cause for this issue was in the way GDScript files resolve their dependencies when loaded.
Essentially, loading a script containing references to other resources, such as preloads of packed scenes, will trigger a load for those resources as well. And to generate a preview of a script we would load it completely and fully. Which means attempting to generate a preview of a script may trigger a load of a packed scene, which may need a preview of its own, which will deadlock the generator.
The sad part is that we don't need a fully loaded script. We only take the source code of the script (plus a reference to its
ScriptLanguage
). This is something we can do by simply reading the file as text.Which is what this PR does. It makes sure that instead of loading a script resource we just read it as text and use the file's extension to get the corresponding
ScriptLanguage
, cheap and simple. The rest is the same as before.There is no noticeable performance difference, unfortunately. At least not with an example I could fabricate to test this with. YMMV.
Some deviations are possible depending on how busy the editor is in each particular moment.
There is no visible difference in the generated file either:
Before
After
I also added a small benchmark for the generator to test the timings. I decided to keep it for this PR, but as a separate commit and as verbose prints. If this is excessive I can drop it. Note that I couldn't use
print_verbose
here becauseprint_verbose
is undefined by the includedvariant_utility.h
header. Probably something worth addressing?