Subject: compiler/optimizer question...
To: None <>
From: Garrett D'Amore <>
List: tech-kern
Date: 03/17/2006 23:37:47
I guess I could probably check this out myself, but I've been
considering techniques to reduce code size, and I ran into a construct
like this in tcp_input:

#define TCP_FIELDS_TO_HOST(th)                                          \
do {                                                                    \
        NTOHL((th)->th_seq);                                            \
        NTOHL((th)->th_ack);                                            \
        NTOHS((th)->th_win);                                            \
        NTOHS((th)->th_urp);                                            \
} while (/*CONSTCOND*/ 0)

This is used in several places in tcp_input.

I'm thinking that a better way to express this might be:

static void
tcp_fields_to_host(struct tcphdr *th)

The question I have is, will the compiler do The Right Thing in this
case, and inline this in such a way that it doesn't have to use a
function call.  (I.e. there is theoretically enough info here that the
compiler can insert *one* section of code storing only the return
address, do a jump and jump back, giving most of the performance benefit
of inline (I guess you have an instruction pipeline stall on the
returning jump), without actually sticking the entire section of code
*inline* repeatedly (i.e. keeping the code size small.)

I guess "inline" or "__inline" could be added too to force the compiler
to inline, and maybe it could do a better job then of deciding how to
inline and reuse code if possible than the macro expansion would allow
for.  (I have a sneaking suspicion that using macros to inline
functionality always loses over declaring an inline function, because
the compiler doesn't get to see that the code is common in all places.

Thanks for your thoughts -- I'm just trying to think of ways to make the
code leaner & meaner.  :-)

Garrett D'Amore, Principal Software Engineer
Tadpole Computer / Computing Technologies Division,
General Dynamics C4 Systems
Phone: 951 325-2134  Fax: 951 325-2191