From: "J.A. Magallón" <jamagallon@ono.com>
To: "Linux-Kernel, " <linux-kernel@vger.kernel.org>
Subject: Inlining can be _very_bad...
Date: Thu, 29 Mar 2007 01:18:38 +0200 [thread overview]
Message-ID: <20070329011838.6e832615@werewolf-wl> (raw)
[-- Attachment #1: Type: text/plain, Size: 1941 bytes --]
Hi all...
I post this here as it can be of direct interest for kernel development
(as I recall many discussions about inlining yes or no...).
Testing other problems, I finally got this this issue: the same short
and stupid loop lasted from 3 to 5 times more if it was in main() than
if it was in an out-of-line function. The same (bad thing) happens if
the function is inlined.
The basic code is like this:
float data[];
[inline] double one()
{
double sum;
sum = 0;
for (i=0; i<SIZE; i++) sum += data[i];
return sum;
}
int main()
{
gettimeofday(&tv0,0);
for (i=0; i<SIZE; i++)
s0 += data[i];
gettimeofday(&tv1,0);
printf("T0: %6.2f ms\n",elap(tv0,tv1));
gettimeofday(&tv0,0);
s1 = one();
gettimeofday(&tv1,0);
printf("T1: %6.2f ms\n",elap(tv0,tv1));
}
The times if one() is not inlined (emt64, 2.33GHz):
apolo:~/e4> tst
T0: 1145.12 ms
S0: 268435456.00
T1: 457.19 ms
S1: 268435456.00
With one() inlined:
apolo:~/e4> tst
T0: 1200.52 ms
S0: 268435456.00
T1: 1200.14 ms
S1: 268435456.00
Looking at the assembler, the non-inlined version does:
.L2:
cvtss2sd (%rdx,%rax,4), %xmm0
incq %rax
cmpq $268435456, %rax
addsd %xmm0, %xmm1
jne .L2
and the inlined
.L13:
cvtss2sd (%rdx,%rax,4), %xmm0
incq %rax
cmpq $268435456, %rax
addsd 8(%rsp), %xmm0
movsd %xmm0, 8(%rsp)
jne .L13
It looks like is updating the stack on each iteration...This is -march=opteron
code, the -march=pentium4 is similar. Same behaviour with gcc3 and gcc4.
tst.c and Makefile attached.
Nice, isn't it ? Please, probe where is my fault...
--
J.A. Magallon <jamagallon()ono!com> \ Software is like sex:
\ It's better when it's free
Mandriva Linux release 2007.1 (Cooker) for i586
Linux 2.6.20-jam06 (gcc 4.1.2 20070302 (prerelease) (4.1.2-1mdv2007.1)) #1 SMP PREEMPT
[-- Attachment #2: Makefile --]
[-- Type: application/octet-stream, Size: 307 bytes --]
PROG=tst
SRCS=tst.c
CC=gcc4 -m64 -march=opteron -O2
#CC=gcc4 -m32 -march=pentium4 -O2
#CC+=-DINLINE
LIBS=
OBJS=$(SRCS:.c=.o)
ASMS=$(SRCS:.c=.s)
all: $(PROG) $(ASMS)
$(PROG): $(OBJS)
$(CC) -o $@ $(OBJS) $(LIBS)
.c.o:
$(CC) -c $<
.c.s:
$(CC) -c -S $<
clean:
@rm -f $(PROG) $(OBJS) $(ASMS) core tags
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: tst.c --]
[-- Type: text/x-csrc; name=tst.c, Size: 958 bytes --]
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#define SIZE 256*1024*1024
#define elap(t0,t1) \
((1000*t1.tv_sec+0.001*t1.tv_usec) - (1000*t0.tv_sec+0.001*t0.tv_usec))
double one();
float *data;
#ifdef INLINE
inline
#endif
double one()
{
int i;
double sum;
sum = 0;
asm("#FBGN");
for (i=0; i<SIZE; i++)
sum += data[i];
asm("#FEND");
return sum;
}
int main(int argc,char** argv)
{
struct timeval tv0,tv1;
double s0,s1;
int i;
data = malloc(SIZE*sizeof(float));
for (i=0; i<SIZE; i++)
data[i] = 1;
gettimeofday(&tv0,0);
s0 = 0;
asm("#MBGN");
for (i=0; i<SIZE; i++)
s0 += data[i];
asm("#MEND");
gettimeofday(&tv1,0);
printf("T0: %6.2f ms\n",elap(tv0,tv1));
printf("S0: %0.2lf\n",s0);
gettimeofday(&tv0,0);
s1 = one();
gettimeofday(&tv1,0);
printf("T1: %6.2f ms\n",elap(tv0,tv1));
printf("S1: %0.2lf\n",s1);
free(data);
return 0;
}
next reply other threads:[~2007-03-28 23:18 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-03-28 23:18 J.A. Magallón [this message]
2007-03-29 1:29 ` Benjamin LaHaise
2007-03-29 17:52 ` Adrian Bunk
2007-03-29 22:01 ` J.A. Magallón
2007-03-29 22:28 ` Adrian Bunk
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20070329011838.6e832615@werewolf-wl \
--to=jamagallon@ono.com \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®