5 ms·
That's standard dead code elimination, and that's only true in static linking. Dynamic linking, which is far more popular, will load the entire library into mem
by jdmichal 9y ago
That's standard dead code elimination, and that's only true in static linking. Dynamic linking, which is far more popular, will load the entire library into memory. And then you get interesting hybrids like Java, where libraries are dynamically linked but only the classes that are actually used are loaded. [0]
The equivalent for dynamic languages is tree shaking, and is a harder or impossible problem depending on the language. When you can do something like the following in JavaScript, how do you determine what's dead or alive?
var fn = document.getElementById("function").value;
var input = document.getElementById("input").value;
var output = functions[fn](input);
document.getElementById("output").value = output;
[0] EDIT: But all the method and fields within a class are loaded, regardless of whether they are used. Which is why I called it a hybrid.
- Spivak 9y agoLanguage theory question: In the case of JS is there a safe, but still useful, subset of the language where dead code elimination could be improved? Ignore if it can be detected or not, we'll pretend that the author has to certify that they followed your subset and didn't do X, Y, or Z. If they disobeyed your rules then all bets are off and the result is undefined.
- majewsky 9y agoI'm afraid it won't help much to restrict the language. For optimal dead-code elimination (aka tree shaking), you need human-level intelligence. Here's an example I posted yesterday in the thread about iOS app bloat: https://news.ycombinator.com/item?id=14618324 https://news.ycombinator.com/item?id=14618324