How to best ensure target-cpu=native?

I know I can do this on the command line using

$ RUSTFLAGS='-C target-cpu=native' cargo bench

or whatever, but then I have to remember to do this every time. I also know that I can do this on a given account on a given computer by editing ~/.cargo/config to contain

rustflags = ["-Ctarget-cpu=native"]

but then another account won't be using this option, or another computer (e.g. belonging to one of my students). I'd love to hear if there is a relatively fool-proof way to ensure that my code will always be run with native instructions (which not infrequently gives a 10% speed boost).

Would it be an option to include this in your .profile or .bashrc?

I think that would be equivalent to using ~/.cargo, but it does make me think that I could cover students using my computers but editing /etc/bash/bashrc or something. But it still doesn't cover any other computers. :slightly_frowning_face:

You can use a .cargo/config inside your project directory, though there are currently some caveats to be aware of, which can cause Cargo to ignore such config files in some cases.

A more foolproof solution might be to use dynamic CPU feature detection. This will work even when the program is built on one computer and run on another. It would also let you distribute pre-compiled binaries that support older CPUs without losing performance on newer ones.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.