4 ms·
A "Sufficiently Advanced Compiler" is probably a magical pipedream though. No compiler has precognition or is directly cabled into the developers brain (Though
by zaarn 9y ago
A "Sufficiently Advanced Compiler" is probably a magical pipedream though.
No compiler has precognition or is directly cabled into the developers brain (Though I wish it was) to tell what they want to express.
The compiler might not be able to tell a preallocation is worth the effort if it cannot determine how often a function is called. Preallocs waste some startup time on the first time compared to dynamically allocating on the stack or heap and throwing away afterwards and also leaks memory.
The compiler would have to be able to tell wether or not a function will be called often and is timing sensitive or whether it is called rarely and latency doesn't matter or any of the millions of variation in the spectrum between those two extremes.
A perfect compiler would have to (essentially) solve the halting problem by being able to tell how programs will behave all the time.
- monsieurbanana 9y agoA perfect compiler would compile natural languages to computer instructions. AKA the real "sudo make me a better facebook".
- MaxBarraclough 9y agoNot really. Natural language isn't anywhere near as precise as programming language code. Some programming languages permit different implementations to behave differently (argument evaluation order in C for example), but natural language is a whole different league. What you describe would be an extremely impressive AI system, but I wouldn't call it a compiler. There's an XKCD for everything: https://xkcd.com/568/ https://xkcd.com/568/
- bdamm 9y agoIndeed. At that level you might as well just order "Go make me some money." and the problem is that everyone else has that tool, too.
- tankenmate 9y agoGo in effect does pre-allocs in that it does variable liveness analysis and any variables that are allocated within a function but who's address isn't assigned to another variable that is from outside the function or returned from the function is allocated on the stack. This means it is automatically freed when the function returns (or stack frame is unwound). This also means that you can be certain that any state changes are from within the current function or any of its children if passed to a child.
- weberc2 9y agoYeah, I know this optimization as escape analysis, but it's pretty easily defeated IIRC. I don't think it even applies across compilation units. In some ways, it's nice that the escape analysis is naive--it makes it easy to reason about and it behaves somewhat intuitively; however, in other cases it means unnecessary allocations.
- riwsky 9y agoPrecognition, mind-reading, and solving the halting problem are indeed beyond modern compilers - but thankfully, some clever people have given us other options for achieving the goals you describe: https://en.wikipedia.org/wiki/Profile-guided_optimization https://en.wikipedia.org/wiki/Profile-guided_optimization https://en.wikipedia.org/wiki/Just-in-time_compilation https://en.wikipedia.org/wiki/Just-in-time_compilation https://en.wikipedia.org/wiki/Partial_evaluation#Futamura_projections https://en.wikipedia.org/wiki/Partial_evaluation#Futamura_pr...