Is there a list of magic tricks to improve Rust (web server) performance?
Here are some I'm aware of:
Compile in release mode (obvious):
cargo run --release
Build Configuration: Adding the following to your Cargo.toml (can be around a 10-20% runtime performance improvement):
[profile.release]
lto = true
codegen-units = 1
panic = "abort" # May want to exclude this oneDatabases: If you're using Postgres, modifying
postgresql.confwith:synchronous_commit = off
It is impossible for synchronous_commit=off to cause data loss, and disabling it can significantly improve latency. Edit: If you perform actions against an external system based on the success / failure of a database query, you may not want to disable this. Double Edit: Maybe just disregard this one!! Lol, I think it's fine for my use-case but yeah this one isn't as "free" as the others for sure. It has upsides and downsides so do your own research with this one.
Memory Allocator: Switching to mimalloc (or jemalloc, or another). Example: simply add
mimalloc = "0.1"to your Cargo.toml and then add this to the top of main.rs:#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;Disable speculative execution mitigations: Assuming you don't run untrusted code on your server, you can disable these (Spectre, meltdown, etc.) to improve performance by 25% or so:
nospectre_v1 nospectre_v2 pti=off mds=off tsx_async_abort=off
HashMaps: Use FxHashMap instead of the normal
HashMapwhen users can't influence the keys, as the normalHashMap's SipHash hashing function DoS resistance isn't needed in that case. Ahash is also very nice as it is the "fastest, DOS resistant hash currently available in Rust"
These are just the ones off the top of my head. Basically just "free" ways to improve the performance of Rust programs. Are there any other ones that I'm missing? I'm developing a web server and I'd like to scale off of a single computer for as long as possible.