openscad / openscad-wasm

Webassembly port of OpenSCAD
GNU General Public License v2.0
193 stars 25 forks source link

How small can I get this? #16

Open wormyrocks opened 5 months ago

wormyrocks commented 5 months ago

Hopefully should be reasonable simple to answer - I'm trying to build a proof-of-concept hardware demo that implements a mass storage device with a portable OpenSCAD compiler. No UI, fonts, etc., are required, just a single .HTML file where you open it, click a button, and it spits out an STL from a hardcoded SCAD model. Since I'm not assuming the presence of a web server, I can't fetch OpenSCAD from a CDN because of CORS policy, and anyway I want it to work offline. My target for the entire package is 2 megabytes, as that's the size of the NOR flash I built the hardware with. I see that openscad.wasm can LZMA compress down to 1.7 MBytes, and I imagine there's some way to unpack that blob on page load. So it all seems possible. But I'd like a bit more headroom. What are the build-time flags I should be switching off to get the size down even further?

t-paul commented 5 months ago

I'm not aware of any easy wins. What might make a difference is telling the compiler to optimize for size, but I don't know how well that is supported for WASM and it's quite some work to get that into the whole build process including all the libraries.

DSchroer commented 5 months ago

For this since its all in existing hardware, how come you need to use wasm and web vs directly compiling OpenSCAD for the platform? Wouldn't you be able to achieve much better performance and size characteristics?

wormyrocks commented 5 months ago

OpenSCAD doesn't need to, and can't, run on the hardware (it's a 50 cent microcontroller), but the hardware does generate SCAD data, and enumerates itself as a USB mass storage device. We could just directly serve .scad files, but I thought it would be snazzier to serve a static, offline webapp that embeds the generated data along with an OpenSCAD compiler, then spits out meshes on demand. The micro is not powerful enough to generate meshes on its own. FWIW, if I lzma-compress the .wasm file, then base64 it, I end up with a 2.3MB JSON string, which I assume can be bodged into the rollup configuration somehow... so I'm within spitting distance. And wasm-opt -Oz is able to knock about 100K off the .wasm file.

DSchroer commented 5 months ago

I see. Sorry I assumed the device needed to run it not just serve it. I guess you could try building your own version with a different set of compiler flags to try and squeeze a bit out. I'm assuming that you are using a fixed .scad file so you could even go so far as try to strip out features that your file does not use and rebuild the wasm.