Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GDScript 2.0: .gdc bytecode files not created with export option #59241

Closed
KoBeWi opened this issue Mar 17, 2022 · 6 comments · Fixed by #71752
Closed

GDScript 2.0: .gdc bytecode files not created with export option #59241

KoBeWi opened this issue Mar 17, 2022 · 6 comments · Fixed by #71752

Comments

@KoBeWi
Copy link
Member

KoBeWi commented Mar 17, 2022

Godot version

5d806b4

System information

Windows 10 x64

Issue description

This option
image
does not work. The pck/zip still contains original .gd files, with no .gdc.

Steps to reproduce

  1. Export project with Compiled Bytecode option (enabled by default)
  2. Look for gdc files in the exported project

Minimal reproduction project

No response

@KoBeWi KoBeWi added this to the 4.0 milestone Mar 17, 2022
@Calinou Calinou changed the title gdc files not created with export option GDScript 2.0: .gdc bytecode files not created with export option Mar 17, 2022
@vnen
Copy link
Member

vnen commented Apr 5, 2022

This option needs to be removed, this feature won't be added to Godot 4.0.

For the record, my plan is to add an intermediate representation, so we can store compiled scripts in the export, which is a much better solution than the current one (present in 3.x). The problem is that this won't happen before 4.0 is out. But adding the mentioned feature here would be a good amount of effort for a half-measure that would be obsoleted in the next Godot version.

@Koalamana9
Copy link

@vnen Could you explain in more detail what intermediate representation means and how this new method differs from the current script tokenization in 3.x? Thanks!

@vnen
Copy link
Member

vnen commented Jan 16, 2023

Could you explain in more detail what intermediate representation means and how this new method differs from the current script tokenization in 3.x? Thanks!

A bit late, but here's an explanation.

What is done in 3.x is converting the tokens in the file to a binary format. For example, if in the source script you have var x = 1 it is converted to TK_PR_VAR TK_IDENTIFIER("x") TK_OP_EQUAL TK_CONSTANT(1) (names here for visualization, in the file it's only their numeric representation). When loading this the tokenizer can skip actually looking for the source string, so it doesn't have to deal with whitespace or comments for instance. Given the binary data has a strict format, it's much faster to tokenize than looking at the source code.

That's the only thing done though. The tokenization phase is almost free in this case but the script still has to be parsed and compiled when loading.

With an intermediate representation, the script is stored as a file already tokenized, parsed, and compiled. For a similar example, the line var x = 1 would be stored as something like stack(0) = constant(1) (local variable names don't need to be stored, they are referred by their stack addresses). When loading, it only needs to convert from the IR to the actual in-memory bytecode used by the GDScript interpreter. This process is similar to loading the .gdc file, except it doesn't have to go through the more demanding parser and compiler steps.

This has more benefits besides loading performance:

  1. We could potentially remove the GDScript parser and compiler on release builds, leaving the executable smaller.
  2. This also applies to type information. A lot is needed in release because GDScript has to figure things out when compiling. If it doesn't need to do so, then this information is not necessary after export.
  3. The parser can become more thorough in type-checking without worrying about load times in release builds.
  4. The code is somewhat obfuscated, for people who worries about having scripts peeked or stolen. It ignores local variable names and the instructions are much simpler, similar to assembly, which makes it harder to follow (and this after the effort of converting the binary format to something readable).

@Calinou
Copy link
Member

Calinou commented Jan 17, 2023

We could potentially remove the GDScript parser and compiler on release builds, leaving the executable smaller.

This may have to remain something done in custom export template builds regardless, as it'll prevent hot-loading of custom hand-written GDScript code in exported projects (which can be desired in some situations). That said, this is an uncommon use case, so maybe it's OK to have this be something you explicitly have to compile support for.

@Riteo
Copy link
Contributor

Riteo commented Jan 17, 2023

@Calinou we could just keep that for debug releases. That might be a problem if the editor can also run release binaries (can it?), but I think that this would be a nice compromise.

@vnen
Copy link
Member

vnen commented Jan 17, 2023

That might be a problem if the editor can also run release binaries (can it?), but I think that this would be a nice compromise.

@Riteo AFAIK the editor doesn't run any template executable, it always runs the same executable as the editor itself.

Also, my idea was to make it optional, I should've made that clear. We could consider whether or not we want to remove it from default/official builds but we can take that decision when the time comes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Status: Done
Development

Successfully merging a pull request may close this issue.

5 participants