Live data from Hacker News

Tree-shaking, the horticulturally misguided algorithm (2023)

wingolog.org

1–10 of 151 posts

Re: Tree-shaking, the horticulturally misguided algorithm (2023)

#2
Tree-shaking is such a bad misnomer. Virgil's compiler calls this "reachability analysis" and it's built into the compilation model. The compiler will parse and typecheck a program's (and libraries' code), and run initializers, but after that the compilation proceeds by exploring from the main entrypoint(s) and only reachable code is analyzed and ends in the final binary. It will happily generate a program (without runtime system) that just consists of a single main function. The runtime system is only necessary for stacktraces and GC, and can be omitted if desired.

Re: Tree-shaking, the horticulturally misguided algorithm (2023)

#3
I've kept openEtG's wasm blob (card game engine) 1. avoid floats (fixed point arithmetic saved quite a bit of space)

2. avoid hashmaps (originally used hashmaps since it was easy to port JS maps to, have since ported everything to vecs)

3. avoid strings (for awhile there were no strings, but eventually brought it in for display logic)

4. use a small allocator, like talc

5. avoid dependencies. I only use rand & fxhash. I should probably get rid of rand (fxhash only used to hash game state to check for desyncs)

6. avoid generic diversity. I try to keep a small set of instantiated types, for example Vec is there so no need to bring in Box or anything. Getting away from floats/hashmaps helped reduce type diversity

7. design algorithms with size in mind, I have a couple lookup tables where I pack bits https://github.com/serprex/openEtG/blob/2011007dec2616d1a24d... encodes an adrenaline mechanic where multiple attacks give lower attack power creatures more attacks than higher attack power creatures. Care was taken comparing how much decoding logic cost compared to storing unpacked values. AI evaluation uses 6 bit fixed precision because 64 encodes more efficiently than 128 in webassembly

Similarly there's a targeting mechanism with AND/OR & predicates. I used to have an AST like format with each predicate getting an enum & AND/OR being a slice of expressions. Now each expression is 32 bit integers encoding expression in polish notation, AND/OR have 2 bit codes & predicates are 6 bits (polish notation won over reverse polish here because with polish notation I was able to have AND/OR short circuit evaluation)

Re: Tree-shaking, the horticulturally misguided algorithm (2023)

#4
> Wasm makes it thinkable to do DOM programming in languages other than JavaScript

Does it really? AFAIK, if I want to do any kind of DOM manipulation in say, rust, I need bindings that will basically serialize calls to be done on the JS side. So with the current incarnation of wasm, I believe you're still stuck with JS.

Re: Tree-shaking, the horticulturally misguided algorithm (2023)

#6
> Wasm makes it thinkable to do DOM programming in languages other than JavaScript

Can't help but picking this out for correction - people have been doing it for a long time in compile-to-JS languages - eg ClojureScript, TypeScript, ReasonML and many others.

And people have also been compiling native-ish stuff for the web a long time before Wasm through asm.js & emscripten, like C and through that C-based languages such as Python: https://en.wikipedia.org/wiki/Asm.js#Adoption

Re: Tree-shaking, the horticulturally misguided algorithm (2023)

#7

> Wasm makes it thinkable to do DOM programming in languages other than JavaScript Does it really? AFAIK, if I want to do any kind of DOM manipulation in say, rust, I need bindings that will basically serialize calls to be done on the JS side. So with the current incarnation of wasm, I believe you're still stuck with JS.

Important to include the preceding "With GC,"

In theory you can import DOM functions from runtime & call with references to dom objects now, bypassing JS to call directly into runtime (I say in theory because I'm not in the know whether this is actually possible, but GC at least brings prerequisite mechanisms to get to that point)

Re: Tree-shaking, the horticulturally misguided algorithm (2023)

#8
Maybe off-topic.

But can't you use WASM to create GUI's like Photoshop, with no JavaScript or DOM?

Isn't the bigger goal of GUI's on WASM is we can jettison JavaScript/DOM and go back to writing GUI's like 10-20 years ago, with simpler libraries. Like SKIA, or something. Using non-web GUI libraries, since they could be compiled to WASM and run in web.

EDIT: Native. I mean pre-web, when GUI libraries were native, everything ran locally.

Seemed like WASM would let apps be built like that again, but would be in browser for deployment.

Re: Tree-shaking, the horticulturally misguided algorithm (2023)

#9
Isn't the tree shaking metaphor based on how some fruit trees are harvested? When you shake the tree, the ripe fruit falls off. It's not the best metaphor in that with fruit harvesting you want the fruit, whereas when storing a serialized image you discard what falls off, but alas.

Re: Tree-shaking, the horticulturally misguided algorithm (2023)

#10
post #3

I've kept openEtG's wasm blob (card game engine) 1. avoid floats (fixed point arithmetic saved quite a bit of space) 2. avoid hashmaps (originally used hashmaps since it was easy to port JS maps to, have since ported everything to vecs) 3. avoid strings (for awhile there were no strings, but eventually brought it in for display logic) 4. use a small allocator, like talc 5. avoid dependencies. I only use rand & fxhash…

Wow! Do you have numbers anywhere showing the space saved? Especially for step 6, using a Vec as a Box, I would not expect that to save much.
Post reply on HN