Mirror of the lttng-dev mailing list
 help / color / mirror / Atom feed
From: compudj@krystal.dyndns.org (Mathieu Desnoyers)
Subject: [ltt-dev] [PATCH] LTTng optimize write to page function
Date: Wed, 4 Feb 2009 00:44:22 -0500	[thread overview]
Message-ID: <20090204054422.GD8231@Krystal> (raw)
In-Reply-To: <20090204132458.ECC0.KOSAKI.MOTOHIRO@jp.fujitsu.com>

* KOSAKI Motohiro (kosaki.motohiro at jp.fujitsu.com) wrote:
> Hi
> 
> > +static inline void ltt_relay_do_copy(void *dest, const void *src, size_t len)
> > +{
> > +	switch (len) {
> > +	case 1:	*(u8 *)dest = *(const u8 *)src;
> > +		break;
> > +	case 2:	*(u16 *)dest = *(const u16 *)src;
> > +		break;
> > +	case 4:	*(u32 *)dest = *(const u32 *)src;
> > +		break;
> > +#if (BITS_PER_LONG == 64)
> > +	case 8:	*(u64 *)dest = *(const u64 *)src;
> > +		break;
> > +#endif
> > +	default:
> > +		memcpy(dest, src, len);
> > +	}
> > +}
> 
> hm, interesting.
> 
> IIRC, few month ago, linus said this optimization is not optimazation.
> lastest gcc does this inlining automatically.
> (but I can't point its url, sorry)
> 
> Is this result gcc version independent? and can you send
> the difference of gcc assembly outout?

Here we go :

x86_64
gcc (Debian 4.3.2-1) 4.3.2 (haven't tried other compiler versions)
kernel 2.6.29-rc3

char dataout[100];
char datain[100];

int sizea = 8;

void testfct_ltt(void)
{
        asm ("/* begin */");
        ltt_relay_do_copy(dataout, datain, sizea);
        asm ("/* end*/");
}

Turns into a jump table :

        movslq  sizea(%rip),%rdx
        cmpq    $8, %rdx
        jbe     .L15
.L6:
        movq    $datain, %rsi
        movq    $dataout, %rdi
        call    memcpy
        .p2align 4,,10
        .p2align 3
.L7:
[...]
.L15:
        jmp     *.L12(,%rdx,8)

        .section        .rodata
        .align 8
        .align 4
.L12:
        .quad   .L7
        .quad   .L8
        .quad   .L9
        .quad   .L6
        .quad   .L10
        .quad   .L6
        .quad   .L6
        .quad   .L6
        .quad   .L11
        .text
        .p2align 4,,10
        .p2align 3
.L11:
        movq    datain(%rip), %rax
        movq    %rax, dataout(%rip)
        jmp     .L7
        .p2align 4,,10
        .p2align 3
.L8:
        movzbl  datain(%rip), %eax
        movb    %al, dataout(%rip)
        jmp     .L7
        .p2align 4,,10
        .p2align 3
.L9:
        movzwl  datain(%rip), %eax
        movw    %ax, dataout(%rip)
        jmp     .L7
        .p2align 4,,10
        .p2align 3
.L10:
        movl    datain(%rip), %eax
        movl    %eax, dataout(%rip)
        jmp     .L7
        .size   testfct_ltt, .-testfct_ltt
        .p2align 4,,15


void testfct_memcpy(void)
{
        asm ("/* begin */");
        memcpy(dataout, datain, sizea);
        asm ("/* end */");
}

Turns into a function call because the size is not statically known :

        movslq  sizea(%rip),%rdx
        movq    $datain, %rsi
        movq    $dataout, %rdi
        call    memcpy


Below, when a constant is passed, both behave similarly :

void testfct_ltt_const(void)
{
        asm ("/* begin */");
        ltt_relay_do_copy(dataout, datain, 8);
        asm ("/* end*/");
}

        movq    datain(%rip), %rax
        movq    %rax, dataout(%rip)


void testfct_memcpy_const(void)
{
        asm ("/* begin */");
        memcpy(dataout, datain, 8);
        asm ("/* end */");
}

        movq    datain(%rip), %rax
        movq    %rax, dataout(%rip)


Therefore, I agree that when memcpy is passed a constant, it will do
the same as my ltt_relay_do_copy. However, when we know we usually
expect sizes of 1, 2, 4 and 8 bytes (unknown at compile-time), the jump
table saves the costly function call to memcpy.

Mathieu

-- 
Mathieu Desnoyers
OpenPGP key fingerprint: 8CD5 52C3 8E3C 4140 715F  BA06 3F25 A8FE 3BAE 9A68




  reply	other threads:[~2009-02-04  5:44 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-02-03 17:10 Mathieu Desnoyers
2009-02-03 17:47 ` [ltt-dev] [PATCH] LTTng optimize write to page function (v2) Mathieu Desnoyers
2009-02-04  1:12 ` [ltt-dev] [PATCH] LTTng optimize write to page function Lai Jiangshan
2009-02-04  2:35   ` Mathieu Desnoyers
2009-02-04  3:53     ` Mathieu Desnoyers
2009-02-04  4:07     ` Zhaolei
2009-02-04  4:53       ` Mathieu Desnoyers
2009-02-04  4:34 ` KOSAKI Motohiro
2009-02-04  5:44   ` Mathieu Desnoyers [this message]
2009-02-04  8:09     ` KOSAKI Motohiro
2009-02-04 17:54       ` Mathieu Desnoyers

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=20090204054422.GD8231@Krystal \
    --to=compudj@krystal.dyndns.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