From 54c902b3b520d03c7f4c5bab24713eb42e78ca34 Mon Sep 17 00:00:00 2001 From: Raul Fernandes Date: Thu, 24 Oct 2019 13:35:24 -0300 Subject: [PATCH] Optimize function xcrush_copy_bytes() Use memcpy to copy the bytes when we can assure that the memory areas does not overlap. When the areas overlap, copy the area that doesn't overlap repeatly. With this change, the copy is ~30x faster. --- libfreerdp/codec/xcrush.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/libfreerdp/codec/xcrush.c b/libfreerdp/codec/xcrush.c index 87a74a2..350801d 100644 --- a/libfreerdp/codec/xcrush.c +++ b/libfreerdp/codec/xcrush.c @@ -732,13 +732,27 @@ static int xcrush_generate_output(XCRUSH_CONTEXT* xcrush, BYTE* OutputBuffer, UI return 1; } -static size_t xcrush_copy_bytes(BYTE* dst, const BYTE* src, size_t num) +static INLINE size_t xcrush_copy_bytes(BYTE* dst, const BYTE* src, size_t num) { - size_t index; - - for (index = 0; index < num; index++) + size_t diff, rest, end, a; + if (src + num < dst || src > dst + num) + { + memcpy(dst, src, num); + } + else { - dst[index] = src[index]; + // src and dst overlaps + // we should copy the area that doesn't overlap repeatly + diff = (dst > src) ? dst - src : src - dst; + rest = num % diff; + end = num - rest; + for (a = 0; a < end; a += diff) + { + memcpy(&dst[a], &src[a], diff); + } + + if (rest != 0) + memcpy(&dst[end], &src[end], rest); } return num; -- 2.7.4