I remember reading somewhere that it's possible to compile a Rust program for a newer CPU to take advantage of more recent instruction sets and improve performance. However, I'm not sure about the specifics and would like to know more.
Are there any guides or resources available that explain how to compile Rust programs for newer CPUs? How much of a performance boost can you expect by using recent instruction sets? And what are the trade-offs or potential compatibility issues that you need to be aware of? Any information or guidance on this topic would be greatly appreciated.
AFAICT, there's no way to request a newer instruction set via Cargo.toml, but you can use a .cargo/config.toml to request newer instruction sets, using rustflags = ["-C", "target-cpu=TARGET_CPU"], where TARGET_CPU is the CPU you want as your baseline - use rustc --print --target-cpus to see the available options.
For 64 bit x86, note that as well as real CPUs, there's 3 "microarchitectural levels" - x86-64-v2 through x86-64-v4 - which are described in the Wikipedia article about x86-64.
Tradeoff for newer instruction sets is that older CPUs won't work with newer instruction sets - e.g. x86-64-v2 won't work on pre-2013 Intel Atom CPUs, or pre-2009 Intel Core or AMD CPUs. On the other hand, they sometimes (not always) allow the compiler to output better code that runs faster.
Note, too, that if you optimize for a specific CPU (i.e. the one in your machine), the compiler doesn't just choose newer instruction sets, it also takes advantage of CPU-specific quirks (if any) to get a small performance boost. On x86, using the native pseudo-CPU (meaning "whatever CPU the compiler is running on") can be worthwhile for binaries you don't distribute.
You need to know what CPU is in the server - you can use Intel Production Specifications to go from a model (e.g. "Xeon E3-1245 v2") to a code name (e.g. "Products formerly Ivy Bridge"), and from there guess what the CPU's name in the target-cpu list is likely to be (e.g. "ivybridge").
If you happen have rustc installed on the target machine nonetheless, just run rustc --print target-cpus on the target machine and there will be a line
Available CPUs for this target:
native - Select the CPU of the current host (currently <READ WHAT’S HERE>).
…
…