Climate-Change Driven Development
Published 2024-01-17
Modern hardware performs billions of instructions per second. Why the fuck do things get slower as tech seemingly advances?
I’m not talking about:
- Planned obsolescense giving an excuse to write new code that happens to be slower on older devices
- UI/UX advances making everything animated
I’m talking about:
- We use hundreds of dependencies
- We offload things that would otherwise be compile-time to run-time
It’s that second point that gets me the most. In this example, I will describe the state of Cloudflare Workers.
We use an advertised blazing fast router. Epic. wait what… we construct it at run-time? Like holy shit guys, can’t you see this can be compiled into switch statement(s) on string literals which V8 had optimisations for since a very long time? We’re already using a build step for file-based routing.
Sure, people say this is premature optimisation and its a tiny constant cost - but it’s still a cost!! It would be so easy to eliminate!! And only one person needs to write the build tool for everyone else to use without worry!!
But what about dynamic path segments?
- You, probably
It’s still doable. One approach would be to split the path on /
(utilising the maxSplit parameter, since we know that at build-time), do switches for the individual segments, skipping the dynamic segments. Another way would be to get the indices of the /
and switch on the slices of the constant segments. Pick your poisson. You are doing this at build-time, you have information.
Imagine if we didn’t have to compile a regular expression for every request. We won’t get charged for the CPU time, and we reduce global emissions.
That’s right - reducing global emissions directly correlates to more money in your wallet.
I hope this gives a new meaning to “ship fast”.