godot icon indicating copy to clipboard operation
godot copied to clipboard

Generate script resource preview without parsing

Open YuriSizov opened this issue 1 year ago • 0 comments
trafficstars

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.

Before:

Generated 'res://OneBadScript.gd' preview in 5148 usec
Generated 'res://OneBadScript.gd' preview in 4171 usec
Generated 'res://OneBadScript.gd' preview in 5058 usec
Generated 'res://OneBadScript.gd' preview in 4574 usec

After:

Generated 'res://OneBadScript.gd' preview in 5484 usec
Generated 'res://OneBadScript.gd' preview in 4343 usec
Generated 'res://OneBadScript.gd' preview in 4933 usec
Generated 'res://OneBadScript.gd' preview in 4096 usec

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 before After 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 because print_verbose is undefined by the included variant_utility.h header. Probably something worth addressing?

YuriSizov avatar Jan 26 '24 18:01 YuriSizov