Skip to contentSkip to conversation

x86_64/lib: improve the performance of memmove

PATCH George Spelvin 1 participant 1 message Sep 16, 2010 Btrfs file system

lore
Share & tools
lore
#0 lore raw

Review

Quoted 13 lines

void *memmove(void *dest, const void *src, size_t count) {

(quoted diff) +1 -4
 	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;