Re: [PATCH 2/3] netfilter : 3 patches to boost ip_tables performance

Willy Tarreau
2005-09-23T04:05:07Z

On Thu, Sep 22, 2005 at 03:05:50PM +0200, Eric Dumazet wrote:
(...)
> It was necessary to get the best code with gcc-3.4.4 on i386 and
> gcc-4.0.1 on x86_64
>
> For example :
>
> bool1 = FWINV(ret != 0, IPT_INV_VIA_OUT);
> if (bool1) {
>
> gives a better code than :
>
> if (FWINV(ret != 0, IPT_INV_VIA_OUT)) {
>
> (one less conditional branch)
>
> Dont ask me why, it is shocking but true :(

I also noticed many times that gcc's optimization of "if (complex condition)"
is rather poor and it's often better to put it in a variable before. I even
remember that if you use an intermediate variable, it can often generate a
CMOV instruction on processors which support it, while it produces cond tests
and jumps without the variable. Generally speaking, if you want fast code,
you have to write it as a long sequence of small instructions, just as if
you were writing assembly. As you said, shocking but true.

BTW, cheers for your optimizations !

Regards,
Willy