Re: [PATCH] x86_64/lib: improve the performance of memmove

From: George Spelvin
Date: Thu Sep 16 2010 - 08:19:54 EST


> 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;
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/