If I use native
in an image built by Dockerhub, I guess this will use the spec of the machine used by Dockerhub, and this will impact the image binary available for download?
That's true. When the docker image is built, it is done on the host machine and using its resources, so -march=native
and -mtune=native
will take the specs of the host machine.
For building docker images that may be used by a wide audience, and making them work as on many (X86) targets as possible, it's best to use a common instruction set. If you need to specify march
and mtune
, these would probably be the safest choice:
-march=x86-64 -mtune=generic
There may be some performance hits compared to -march=native -mtune=native
in certain cases, but fortunately, on most applications, this change could go almost unnoticed (specific applications may be more affected, especially if they depend on a small piece of kernel functions that GCC is able to optimize well, for example by utilizing the CPU vector instruction sets).
For reference, check this detailed benchmark comparison by Phoronix:
GCC Compiler Tests At A Variety Of Optimization Levels Using Clear Linux
It compares about a dozen benchmarks with GCC 6.3 using different optimization flags. Benchmarks run on an Intel Core-I7 6800K machine, which supports modern Intel instruction sets including SSE, AVX, BMI, etc. (see here for the complete list). Specifically, -O3
vs. -O3 -march=native
is the interesting metric.
You could see that in most benchmarks, the advantage of -O3 -march=native
over -O3
is minor to negligible (and in one case, -O3
wins...).
To conclude, -march=x86-64 -mtune=generic
is a decent choice for Docker images and should provide good portability and a typically minor performance hit.