Optimization by make.conf in FreeBSD
Nowadays all desktop OS and majority of server OS are using a compiled software. This software is built on old instruction set of CPU which is supported by every modern CPU. A bad thing is that the old instruction set doesn't use the advantages of new CPUs and works slowly.
Compiled software is perfect for the desktop systems where majority of users are not advanced users. And they don't have a deep knowledge in computer.
Compiled software has two distinct advantages. The first is a fast installation of software where the time which is spent on their installation is equal to the time which spent on copying and extracting files from the installation package. The second - it can work on any type of CPU.
But, such a software has one big disadvantage - work speed. It can't be used by servers which have high load.
In such case we need to optimize the software. It can be done only if we compile software from the source code. But, in OS like FreeBSD we can even compile the kernel of an operation system. That means if we compile kernel and all software with CPU optimization then everything will work really fast. Such a kernel and software will use all the advantages of your CPU.
In FreeBSD it can be done by setting CPU type and also the level of optimization in config-file make.conf. This file should be placed in /etc/make.conf.
So, /etc/make.conf will look like this:
CPUTYPE?=core2 CFLAGS= -O3 -pipe -funroll-loops COPTFLAGS= -O2 -pipe -funroll-loops NO_PROFILE=true BUILD_OPTIMIZED=YES BUILD_STATIC=YES WITHOUT_DEBUG=YES #turn off graphic for server WITHOUT_X11=YES WITHOUT_FONTCONFIG=YES
- CPUTYPE? is a type of CPU, all available values are listed in the file /usr/share/examples/etc/make.conf
- CFLAGS is the variable that sets parameters (flags) of optimization for the compiler.
- COPTFLAGS is same like CFLAGS, but only for kernel compilation.
- NO_PROFILE is set to disable the compilation of profiled libraries.
- BUILD_OPTIMIZED is set to enable compiler optimizations. It's used for better speed.
- BUILD_STATIC is set to build static versions of the binaries. It's used for even better speed.
In CFLAGS and COPTFLAGS the most important part is O. There are few levels of optimization. They are: -O0 (without optimization), -O1, -O2 and -O3 (O3 - the highest level).
-O turns on the optimization. It tries to reduce the size of the code and accelerates the work speed of the program. Accordingly, time of compilation increases.
-O2 optimizes even more. GCC does almost all the supported optimizations which don't include time reduction of execution by increasing the code length. If we compare -O2 with -O, this option will increase the compilation time, but also the code's efficiency.
-O3 turns on all -O2 optimizations and also some optimizations which increase binary size. It can cause errors during compilation in old programs. If this happens then it is recommended to reduce the level of optimization to O2.
Note: configuration of make.conf makes sense if FreeBSD was just installed on your computer. Then all software will be built with optimization. Also it's recommended to build kernel and world when make.conf is configured.