linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "George Spelvin" <linux@horizon.com>
To: andi@firstfloor.org, miaox@cn.fujitsu.com
Cc: linux@horizon.com, linux-btrfs@vger.kernel.org,
	linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] x86_64/lib: improve the performance of memmove
Date: 16 Sep 2010 08:13:09 -0400	[thread overview]
Message-ID: <20100916121309.8926.qmail@science.horizon.com> (raw)

>  void *memmove(void *dest, const void *src, size_t count)
>  {
>  	if (dest < src) {
>  		return memcpy(dest, src, count);
>  	} else {
> -		char *p = dest + count;
> -		const char *s = src + count;
> -		while (count--)
> -			*--p = *--s;
> +		return memcpy_backwards(dest, src, count);
>  	}
>  	return dest;
>  }

Er... presumably, the forward-copy case is somewhat better optimized,
so should be preferred if the areas don't overlap; that is, dest >
src by more than the sount.  Assuming that size_t can hold a pointer:

	if ((size_t)src - (size_t)dest >= count)
		return memcpy(dest, src, count);
	else
		return memcpy_backwards(dest, src, count);

Or, using GCC's arithmetic on void * extension,
	if ((size_t)(src - dest) >= count)
		... etc.

If src == dest, it doesn't matter which you use.  You could skip the
copy entirely, but presumably that case doesn't arise often enough to
be worth testing for:

	if ((size_t)(src - dest) >= count)
		return memcpy(dest, src, count);
	else if (src - dest != 0)
		return memcpy_backwards(dest, src, count);
	else
		return dest;

             reply	other threads:[~2010-09-16 12:13 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-09-16 12:13 George Spelvin [this message]
  -- strict thread matches above, loose matches on Subject: below --
2010-09-16  6:48 [PATCH] x86_64/lib: improve the performance of memmove Andi Kleen
2010-09-16  7:16 ` Miao Xie
2010-09-16  8:40   ` Andi Kleen
2010-09-16  9:29     ` Miao Xie
2010-09-16 10:11       ` Andi Kleen
2010-09-16 10:47         ` Miao Xie
2010-09-16 11:47           ` Miao Xie
2010-09-17  0:55   ` ykzhao
2010-09-17  3:37     ` Miao Xie
2010-09-16  6:31 Miao Xie

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=20100916121309.8926.qmail@science.horizon.com \
    --to=linux@horizon.com \
    --cc=andi@firstfloor.org \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=linux-ext4@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=miaox@cn.fujitsu.com \
    /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;
as well as URLs for NNTP newsgroup(s).