News for hackers. Stuff that matters.
Re:Tree-Shaking: A Reference Guide by scoopr on Wednesday May 26, 2021 @04:12AM (#27287754)
The Web development scene is sometimes just so foreign thing to me. I've many times wondered, what does tree-shaking mean when listed in some features in some framework. I think I've looked up it in several occasions and it hasn't stuck with me. It throws me off as it sounds like some kind of ast fuzzing or whatever (perhaps like heckle in ruby).

There is a perfectly good name for this that is used in many other contexts, called "dead code elimination", which is also fairly self-explanatory if you haven't come across the term before.

Re:Tree-Shaking: A Reference Guide by cakoose on Wednesday May 26, 2021 @05:26AM (#27288186)
Adding to the confusion is this often-cited article: https://medium.com/@Rich_Harris/tree-shaking-versus-dead-cod...

That article claims that "tree shaking" is different because:

> Rather than excluding dead code, we’re including live code.

That's a misunderstanding of the term "dead code elimination". Classic dead code elimination techniques have always worked by first identifying potentially live code and then eliminating the rest.

Re:Tree-Shaking: A Reference Guide by junon on Wednesday May 26, 2021 @06:14AM (#27288505)
Yeah this is just Rich Harris trying to sound smart it seems. I always had a problem with that exact quote because it's just another web-delusional attempt at reinventing the wheel with a flashier name.

Tree Shaking is dead code removal. They're one and the same.

Re:Tree-Shaking: A Reference Guide by rich_harris on Wednesday May 26, 2021 @10:05AM (#27290632)
FWIW, this Twitter thread addresses this issue: https://twitter.com/Rich_Harris/status/1305925515472261122.

I certainly didn't _invent_ the term 'treeshaking', I merely (unwittingly) helped popularise it in the JS ecosystem. The relevant parts of the Twitter thread:

> the reality is that at the time i wrote that, when Rollup was in its early stages, minifiers empirically didn't eliminate as much code as Rollup did

> i think it's worth looking at this from the perspective of the respective terms' connotations, rather than the strictly technical definition (which i agree has been overstated; mea culpa for my part in that). DCE is something you do to programs; treeshaking is what you do to libraries. It may be the same thing, but the prevalence of the idea of treeshaking has put the onus on library authors to make their libraries DCE-able, in a way that has coincided with the adoption of ESM

Re:Tree-Shaking: A Reference Guide by agumonkey on Wednesday May 26, 2021 @04:24AM (#27287814)
afaik tree-shaking is an old old term, it might be less obvious in the web context because it's probably inherited from 70s programming languages (lisp, smalltalk or similar, i can't really say).
Re:Tree-Shaking: A Reference Guide by juki on Wednesday May 26, 2021 @05:30AM (#27288208)
Yes, in image based languages DCE is used inside functions to eliminate things like branches that can never be executed, but a function that is never called is not dead code since it might get called later. Tree shaking is used to optimize the final deliverable by saying that there will be no more changes or `eval`s, so anything that isn't reachable from the root(s) can be eliminated.
Re:Tree-Shaking: A Reference Guide by funcDropShadow on Wednesday May 26, 2021 @06:28AM (#27288594)
Whether dead-code elimination would remove whole functions depends on some basic assumption of the tool doing it --- usually a compiler. Some languages are considered closed worlds whereas others are usually considered as open worlds, e.g. Smalltalk, Java and Lisp. Under an open world assumption, everything that could be called by some code added later has to be retained. Some tools nevertheless apply a closed-world assumption to an "open world language" and take additionaly arguments what to consider entry points that should be retained. An example for such a tool is Proguard in the Java world.