]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
x86-64: Avoid rep movsb with short distance [BZ #27130]
authorH.J. Lu <hjl.tools@gmail.com>
Tue, 12 Jan 2021 13:15:49 +0000 (05:15 -0800)
committerH.J. Lu <hjl.tools@gmail.com>
Tue, 12 Jan 2021 14:09:23 +0000 (06:09 -0800)
When copying with "rep movsb", if the distance between source and
destination is N*4GB + [1..63] with N >= 0, performance may be very
slow.  This patch updates memmove-vec-unaligned-erms.S for AVX and
AVX512 versions with the distance in RCX:

cmpl $63, %ecx
// Don't use "rep movsb" if ECX <= 63
jbe L(Don't use rep movsb")
Use "rep movsb"

Benchtests data with bench-memcpy, bench-memcpy-large, bench-memcpy-random
and bench-memcpy-walk on Skylake, Ice Lake and Tiger Lake show that its
performance impact is within noise range as "rep movsb" is only used for
data size >= 4KB.

(cherry picked from commit 3ec5d83d2a237d39e7fd6ef7a0bc8ac4c171a4a5)

NEWS
sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S

diff --git a/NEWS b/NEWS
index 5712eecde810ff4d77020ce40cc30a4f951d057d..105fafb44c1f790dd2567b4242a16e164f243c6d 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -57,6 +57,7 @@ The following bugs are resolved with this release:
   [25933] Off by one error in __strncmp_avx2
   [25966] Incorrect access of __x86_shared_non_temporal_threshold for x32
   [25976] nss_compat: internal_end*ent may clobber errno, hiding ERANGE
+  [27130] "rep movsb" performance issue
 
 \f
 Version 2.30
index 29cab02f7941632c57470808dc3234dbb498a4bb..95e9bb0e5d370cfa63b91a441a70b4a55458ea7f 100644 (file)
 # define REP_MOVSB_THRESHOLD   (2048 * (VEC_SIZE / 16))
 #endif
 
+/* Avoid short distance rep movsb only with non-SSE vector.  */
+#ifndef AVOID_SHORT_DISTANCE_REP_MOVSB
+# define AVOID_SHORT_DISTANCE_REP_MOVSB (VEC_SIZE > 16)
+#else
+# define AVOID_SHORT_DISTANCE_REP_MOVSB 0
+#endif
+
 #ifndef PREFETCH
 # define PREFETCH(addr) prefetcht0 addr
 #endif
@@ -257,7 +264,21 @@ L(movsb):
 #  error Unsupported REP_MOVSB_THRESHOLD and VEC_SIZE!
 # endif
        jb      L(more_8x_vec_backward)
+# if AVOID_SHORT_DISTANCE_REP_MOVSB
+       movq    %rdi, %rcx
+       subq    %rsi, %rcx
+       jmp     2f
+# endif
 1:
+# if AVOID_SHORT_DISTANCE_REP_MOVSB
+       movq    %rsi, %rcx
+       subq    %rdi, %rcx
+2:
+/* Avoid "rep movsb" if RCX, the distance between source and destination,
+   is N*4GB + [1..63] with N >= 0.  */
+       cmpl    $63, %ecx
+       jbe     L(more_2x_vec)  /* Avoid "rep movsb" if ECX <= 63.  */
+# endif
        mov     %RDX_LP, %RCX_LP
        rep movsb
 L(nop):