(Edit 2: see the bottom of this thread, the problem has been mostly fixed by Cloudflare. Amazing response, thank you.)
(Edit 1: this is a much better demonstration of the problem)
I have a worker (code here) that renders html templates.
Much of the logic is written in rust, this is mainly to allow the templates to be rendered using the tera template rendering library.
Everything seemed to be going well…
However there’s a big problem: running the worker is VERY slow - rough 2.5 to 3s extra time per request. That’s after trying everything to reduce the size of the compiled wasm, before responses where ~5s!
That might not sound like that much but when Cloudflare workers advertise adding only a few milliseconds to the response time; 3 seconds is ~1000x slower than expected.
These slow response are when the worker is not “hot” e.g. loaded in memory (I’ve added a header to show when this is the case). When the worker is hot, the response time drops to a more reasonable few 10s of milliseconds. However only around 1 in 20 requests when making requests continuously is in memory - I would estimate you’d need to be making ~100/s to each data centre to have a good chance that most requests hit a hot worker.
What am I doing wrong? How can this be fixed? Is the slowness here in loading the worker code from disk, or running some initialisation code that is run before the worker is executed?
If this is just “how it is” Cloudflare worker are effectively useless for running webassembly.
A few things to note:
- In this PR I’ve tried everything to reduce the size, hence the marginally improve response time discussed above. There’s now no
Your built project has grown past the 1MiB size limit..., instead I get
Built successfully, built project size is 604 KiB.(though
ls -lh workershows
module.wasmis 2.9M - this is also weird)
- I’ve tried every combination of the following to reduce size, nothing has made a significant difference:
- other compile time options like
lto = true
wasm-snipon the generated
- The slow response time is not in executing the rust code, or even running
await import('../pkg')- I’ve inserted a short circuit here and the response time when hitting the short circuit is basically the same
- This worker is implemented using
type = "webpack"in
wasm-pack-pluginwebpack plugin to compile the wasm, but that’s not the problem either - I tried
type = "rust", see this branch, but the performance is the same