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

What is incremental compliation and why is it difficult? #30

Open
expikr opened this issue Jul 20, 2024 · 2 comments
Open

What is incremental compliation and why is it difficult? #30

expikr opened this issue Jul 20, 2024 · 2 comments

Comments

@expikr
Copy link

expikr commented Jul 20, 2024

What are some design decisions taken by Zig toward that goal and how does it do so? And what exactly is hot reloading and where does it fit among these concepts?

@Paul-Dempsey
Copy link

Hot reloading is orthogonal to incremental compilation. It means being able to load modified, rebuilt code without restarting the application. This usually means that the unit of reloading is a dynamic library.

Incremental compilation at it's root is about caching and as we know from Phil Karlton, cache invalidation is one of the hardest problems in computer science (along with naming things). Hard to say what design decisions Zig will take on the way to realizing incremental compilation, since it has not been realized yet.

@lumi2021
Copy link

lumi2021 commented Jan 28, 2025

in short details (yes this is short lol this is a really complex subject and i really reccomend you to study more about if you have interest),

Incremental compilation

is the hability of cache/save compilation data during build and only recompile what is modified. e. g.:

zig-cache/
 |- main.o
 '- foo.o
main.zig
foo.zig

zig-cache contains the binary for EACH script, instead of the entire binary of the entire project. each script can be linked with each other using a linker, but not executed, as they are object files.
when you edit, for instance, foo.zig, the compiler will ignore the entire process of main.zig as it's already cached and instead, just link the result of foo.zig with main.o. If you think about a project with hundreds of files, you can see how it can make compilation time really faster thank building everything again and again.

Why is it difficult?

  • Incremental compilation works at binary level, what means a complete diferent way to handle references, mainly between files. Dependences in binary are usually static and needs to the entire project to be processed and reprocessed to link them.
  • Cache validation and invalidation is a really hard work. Think about your code as a jigsaw puzzle that when complete, it will generate a image that is your program. Now, thinks that you need to change a really tiny detail on the puzzle's image. Usually, you whould need to produce a entire new collection of pieces. With incremental compilation instead, you have to find only the pieces that have this change and replace them. Now thinks that one piece can depend directly of each other and, even did not changing what a piece represents, it is still uncompatible with the new pieces. This among hundreds or milions of pieces. This is what the compiler need to do with every change in your code.
  • Optimizations are harder to do with incremental compilation, as they need to understand the ENTIRE program logic to be done. Instead, it has only a piece of your code and needs to decode it to understand what is going on and how the right optimizations can be made.

Hot reloading

is the hability of change the code while the aplication is running, making it unecessary to close and compile everything again to see little changes being aplied. as an example, imagine that i have the following code:

while (true) {
   Std.Debug.print("Hello, World!", .{});
}

in the console, i will see the following result repeating again and again:

Hello, World!
Hello, World!
Hello, World!

With hot reloading, still having the aplication running, i can edit the code to:

while (true) {
   Std.Debug.print("Hello, World! This was hot reloaded!", .{});
}

and now, the console will be printing the edited message instead of the old one, without needing to open it again:

...
Hello, World!
Hello, World!
Hello, World! This was hot reloaded!
Hello, World! This was hot reloaded!

(don't confuse with it be running another routine. It's still the same, only the binary instructions for it execution has changed)

What one have to do with another

Mainly, nothing. Both are pretty the same thing and at the same time, complete different things. Here are a table explaining more preciselly the difference of both (dot (.) means the same as the right collumn):

incrementel comp hot reloading
Objective Allows more dynamic and faster code debugging .
When Only compile time Only runtime
How Cache precompiled data Modify the executable binaries
Makes compilation faster? Yes Not necessarelly
Applying changes The program needs to be recompiled to apply changes The program do not need to be recompiled or restarted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants