google / sxg-rs

A set of tools for generating signed exchanges at serve time.
Apache License 2.0
83 stars 20 forks source link

Optimize binary size #38

Open twifkak opened 3 years ago

twifkak commented 3 years ago

The compiled cloudflare_worker wasm is currently around 1.2MB. The opt-level and lto tricks didn't reduce that. It's clearly possible to make small wasm binaries. Investigate the easiest change possible to make this one smaller.

The twiggy command identified table[0] as the main culprit. I think that might have to do with the use of JS callbacks? Judging by the MDN article and Lin Clark's article.

Things I didn't try:

Articles I didn't read:

twifkak commented 3 years ago

I was wrong about table[0]. Useful output is available in a dev build, e.g.:

(cd cloudflare_worker; ~/.cache/.wrangler/wasm-pack-0.10.0/wasm-pack build --target no-modules --dev) && twiggy dominators cloudflare_worker/pkg/cloudflare_worker_bg.wasm | less

or likewise twiggy top

antiphoton commented 3 years ago

The wasm is reduced from 1.2 MB to 993 KB by removing disabling the pem parsing code

 fn get_der(pem_text: &str, expected_tag: &str) -> Vec<u8> {
+    return vec![];
-    for pem in ::pem::parse_many(pem_text) {
-        if pem.tag == expected_tag {
-            return pem.contents;
-        }
-    }
     panic!("The PEM file does not contains the expected block");
 }

We could find an alternative pem parser that takes less than 240 KB.