SerenityOS / jakt

The Jakt Programming Language
BSD 2-Clause "Simplified" License
2.77k stars 244 forks source link

codegen: Steps towards generating less C++ code #1540

Closed cg-jl closed 6 months ago

cg-jl commented 6 months ago

By adding a simple tracker to the codegen, it's now able to know the dependencies of what it's generating. This and topological ordering enables Jakt to reduce its dependency on a unified forward declaration, which contains a lot of declarations that may not be used by all the code, and triggers recompilations when some type is added across the entire project.

Separately, it avoids generating the contents of a .cpp file completely if no C++ code has been generated from it. This way the C++ compiler does not waste time processing header includes to discover that there is nothing to be emitted nor checked. I couldn't think of a solution to fully remove those files since CMake currently relies on those existing, given that codegen only knows what files it's going to produce when it has finished generating code. This means that currently empty files are emitted for files that are known to not have anything in them.

Applying this concept to both module implementations and specializations, we get an overall ~20% improvement in C++ compilation time for the full compiler, parsing times reduced by ~40% for the same case:

Analyzing build trace from 'main.bin'...
**** Time summary:
Compilation (100 times):
  Parsing (frontend):          103.6 s
  Codegen & opts (backend):    144.1 s
Analyzing build trace from 'reduced.bin'...
**** Time summary:
Compilation (100 times):
  Parsing (frontend):           65.5 s
  Codegen & opts (backend):    135.7 s

C++ compilation for single files like the ones run by Jakttest really benefits from this, now Jakttest seems to spend less time waiting for things to finish. Perhaps I could invest some time into Jakttest to measure these timings, since this time its output looked much more streamlined.