# Optimizations



## Weinter (Nov 20, 2008)

Is it safe to use -O3 on /etc/make.conf?
Any other Optimizations that is great on AMD Processors?


----------



## vermaden (Nov 20, 2008)

For some apps like mplayer -O3 can do good things, for most of the apps -O3 even will slow them down in many cases.


----------



## lme@ (Nov 20, 2008)

Please don't use build optimizations in make.conf. The standard compile options are already chosen wisely.


----------



## Weinter (Nov 20, 2008)

lme@ said:
			
		

> Please don't use build optimizations in make.conf. The standard compile options are already chosen wisely.



I don't understand why can you elaborate ? Thanks :e

Also why not processor optimizations


----------



## lme@ (Nov 20, 2008)

Because when you use optimizations you can get very subtle problems and you'll never find out where the problems come from.
Apart from that you probably won't get any support from the FreeBSD devs until you re-compiled everything with the standard compile options.


----------



## Mel_Flynn (Nov 20, 2008)

And ports that can safely be compiled with optimizations, will have a config option for it, either via the OPTIONS dialog or WITH_OPTIMIZED_CFLAGS tunable.


----------



## kamikaze (Nov 20, 2008)

I did a lot of benchmarking and I have to say, it doesn't make sense to optimize beyond the default FreeBSD CFLAGS. The only thing that makes sense is setting CPUTYPE properly. Applications where more optimizations make sense (e.g. multimedia applications) normally offer additional optimizations as recommended by the developers through the options framework.


----------



## graudeejs (Nov 20, 2008)

Make this sticky!!! i assure you, many will ask this question.


----------



## Weinter (Nov 21, 2008)

This is my make.conf so far everything works

CPUTYPE?= k8
CFLAGS+= -O2 -pipe
COPTFLAGS+= -O2 -pipe
MAKEOPTS+= -j3


----------



## graudeejs (Nov 21, 2008)

why does ppl, like writing default flags to make.conf?
Like it speeds things up?
I have tried Gentoo linux to many times, to tell you this:
Forget about it. It just ain't worth.
not to mention, that your flags didn't improve anything at all


----------



## Weinter (Nov 21, 2008)

killasmurf86 said:
			
		

> why does ppl, like writing default flags to make.conf?
> Like it speeds things up?
> I have tried Gentoo linux to many times, to tell you this:
> Forget about it. It just ain't worth.
> not to mention, that your flags didn't improve anything at all



You mean it doesn't?!!
I thought these were documented in GCC?


----------



## Mel_Flynn (Nov 21, 2008)

This isn't very smart:



			
				Weinter said:
			
		

> CPUTYPE?= k8
> CFLAGS+= -O2 -pipe
> COPTFLAGS+= -O2 -pipe
> MAKEOPTS+= -j3


From /usr/share/mk/sys.mk:

```
.if defined(%POSIX)
CC              ?=      c89
CFLAGS          ?=      -O
.else
CC              ?=      cc
CFLAGS          ?=      -O2 -fno-strict-aliasing -pipe
.endif
```

So, CFLAGS is overridden by you:
*-O2 without -fno-strict-aliasing is NOT supported on FreeBSD and will cause programs to miscompile or misbehave.*

The only compiler flag that makes sense to put in /etc/make.conf is:

```
CXXFLAGS+=-Wno-deprecated
```

As this gets rid of useless warnings about char to string conversions in c++ code.

COPTFLAGS is used by the kernel, as seen in /usr/src/sys/conf/kern.pre.mk. Nowhere else. Unless you're a kernel hacker who knows what he's doing, stay away from it. Here's how it's set up:

```
.if ${CC} == "icc"
COPTFLAGS?=     -O
.else
. if defined(DEBUG)
_MINUS_O=       -O
. else
_MINUS_O=       -O2
. endif
. if ${MACHINE_ARCH} == "amd64"
COPTFLAGS?=-O2 -frename-registers -pipe
. else
COPTFLAGS?=${_MINUS_O} -pipe
. endif
. if !empty(COPTFLAGS:M-O[23s]) && empty(COPTFLAGS:M-fno-strict-aliasing)
COPTFLAGS+= -fno-strict-aliasing
. endif
.endif
.if !defined(NO_CPU_COPTFLAGS)
. if ${CC} == "icc"
COPTFLAGS+= ${_ICC_CPUCFLAGS:C/(-x[^M^K^W]+)[MKW]+|-x[MKW]+/\1/}
. else
COPTFLAGS+= ${_CPUCFLAGS}
. endif
.endif
```


----------



## fender0107401 (Nov 21, 2008)

Weinter said:
			
		

> This is my make.conf so far everything works
> 
> CPUTYPE?= k8
> CFLAGS+= -O2 -pipe
> ...



Gentoo or FreeBSD?


----------



## fender0107401 (Nov 21, 2008)

*This is my /etc/make.conf*

Hi all,

This is my /etc/make.conf file, maybe anyone can give me some suggestion, the compilation flag always confuse me.



> ################################################################################
> # cpu type
> CPUTYPE=nocona
> 
> ...


----------



## Weinter (Nov 21, 2008)

Mel_Flynn said:
			
		

> This isn't very smart:
> 
> 
> From /usr/share/mk/sys.mk:
> ...



But i used += which adds to the compilation options not overwrite it?!


----------



## kamikaze (Nov 21, 2008)

Is this a contest in displaying the most nonsensical make.conf lines?

Seriously just set CPUTYPE?=<mycpu> and select OPTIMIZED_CFLAGS when an options dialogue offers it and you've done everything that makes sense.


----------



## vermaden (Nov 21, 2008)

I use that */etc/make.conf*:
http://toya.net.pl/~vermaden/text/make.conf


----------



## Mel_Flynn (Nov 21, 2008)

Weinter said:
			
		

> But i used += which adds to the compilation options not overwrite it?!



You're right. No idea why you'd do that as it simply doubles flags already there. Makes compilation slightly slower as the argument list of the compile command grows.
CPUTYPE is the only one that makes sense, but it has the drawback that if you backup and restore your machine to another machine with different cpu type (because your machine went kaput), you will have to recompile everything or hit signal 4 a lot.

Any programmer knows that the best thing to speed up your program is to upgrade your hardware. The rest is marginal in comparison.


----------



## oliverh (Nov 21, 2008)

CPUTYPE can be of some help in terms of 3-5%, but it can be a problem too (had some with Pentium4 some years ago). Most of the time the bottleneck lies in different areas and it yields no miracles as some people believe.


----------



## s-tlk (Nov 21, 2008)

I'm programming numerical software and optimize them. I can tell you we got a lot of trouble with different kinds of optimizations. You have to know what the program is doing and what the flags are doing with the program when you set them. There are some 'global' flags which increase in most cases the performance, but most of the flags are for a special purpose. The worst thing what could happen, when you are using aggressive optimizations is, that the program misscalculates. And I believe I must not tell you that this can cause weird results, which are extremly difficult to locate, especially by sensitve parts of the system like kernel, base system...
So, switch on brain, before switching on optimization flags. 

The best rise in performance you can get, besides buying new hardware, is to design your programs well and perhaps use a specific compiler. For example the Intel fortran compiler is much faster then gfortran.


----------



## Maurovale (Nov 27, 2008)

kamikaze said:
			
		

> Is this a contest in displaying the most nonsensical make.conf lines?
> 
> Seriously just set CPUTYPE?=<mycpu> and select OPTIMIZED_CFLAGS when an options dialogue offers it and you've done everything that makes sense.



This is everything I put in my make.conf, I do not recommend put any other thing in where, sooner or later you will have problems...


----------

