4 ms·
It's time consuming because the compiler tries to optimize the code. A compiler that doesn't try to optimize can be amazingly fast like Python's compiler or lik
by bjourne 4y ago
It's time consuming because the compiler tries to optimize the code. A compiler that doesn't try to optimize can be amazingly fast like Python's compiler or like how Go's compiler used to be. Neither attempted many optimizations.
Compiler optimizations are time consuming because they are trying to find solutions to NP-hard problems. Since solving NP-hard problems optimally is intractable compilers use heuristics. So the more optimization the user asks for the more time consuming heuristics are executed. One such problem is register allocation. Your CPU has a finite number of registers that should be used for an almost infinite number of variables. Figuring out a good register allocation is very difficult.
It also gets more difficult because modern compilers are optimizing larger contexts. Rust, C++, and other languages with functional features rely heavily on function inlining. So a function you write that is five lines may internally be expanded by the compiler to hundreds of lines.
- retrac 4y ago> NP-hard I wrote a toy compiler once as an educational project. I accidentally invented super-optimization, too. Felt so clever for a whole hour or so. It was guaranteed to produce the optimal program that can be represented in the intermediate representation I used. Unfortunately, this only works for programs with up to six operations with current hardware; more than about ten would require more RAM than can fit in the universe. Finding shortcuts to prune down that problem is the black art of practical optimization.