On 2026-10-08 04:49, [email protected] wrote:
This patch adds an RVV-based implementation for the overlap-copy path in
inflate_codes() on RISC-V systems with vector support.

The change is limited to the overlap-copy slow path in inflate, and the
original scalar byte-copy behavior is kept unchanged for non-RVV builds.
Thanks for pointing out the significant performance issue there. However, 
before resorting to RISC-V specific improvements, I'd like to fix the problem 
in a platform-independent way and then see whether RISC-V specific changes are 
still needed. To that end, I installed into gzip master on Savannah the 
attached patches, which use memcpy and memmove instead of RISC-V builtins. (I'm 
assuming memcpy and memmove are tuned for the particular RISC-V platform we're 
running on.) If you're still interested in RISC V improvements I suggest 
starting from this patch.

In a brief look at the patch, I worry a bit about store-to-load forwarding stalls 
when 1 < w - d < VLEN/8, and about the latency of the vector loop when e is 
small. Also, I assume you rejected wider vector registers due to the rarity of them 
helping in practice. Either way, it'd be helpful to see performance figures on real 
hardware.

Anyway, what we probably really should do is drop all this and use zlib 
instead. A bigger change, I know.
From 23a2a86ef7fb1381ceff499d03a55c05476d856d Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Sat, 14 Nov 2026 23:11:35 -0800
Subject: [PATCH] gzip: inflate slide speedup

* inflate.c (inflate_codes): Tweak performance for
typical machines with 128-bit load/stores.
---
 NEWS      |  5 +++++
 inflate.c | 34 ++++++++++++++++++++++++++++++----
 2 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/NEWS b/NEWS
index 5faa0b2..d53141f 100644
--- a/NEWS
+++ b/NEWS
@@ -14,6 +14,11 @@ GNU gzip NEWS                                    -*- outline -*-
   like Turbo C.
   [16-bit MS-DOS has not worked since gzip-1.3]
 
+** Speedups
+
+  gzip -d now copies memory faster from its internal window, improving
+  performance by 15% in some simple benchmarks.
+
 
 * Noteworthy changes in release 1.15 (2026-09-20) [stable]
 
diff --git a/inflate.c b/inflate.c
index 12dec09..f68fbc7 100644
--- a/inflate.c
+++ b/inflate.c
@@ -590,18 +590,44 @@ inflate_codes(struct huft *tl, struct huft *td, int bl, int bd)
       do {
         n -= (e = (e = WSIZE - ((d &= WSIZE-1) > w ? d : w)) > n ? n : e);
 #ifndef DEBUG
-        if (e <= (d < w ? w - d : d - w))
         {
-          memcpy(slide + w, slide + d, e);
+          /* Speedup for typical platforms, which can move 16 bytes with
+             a single load/store.  It is OK to move more bytes than
+             necessary so long as they are present in the source and fit
+             in the destination buffer, as excess bytes copied to the
+             destination are ignored.  */
+          enum { MOVE_BYTES = 16 };
+
+          if (e <= MOVE_BYTES
+              && (w <= d
+                  ? w + MOVE_BYTES <= d && d <= WSIZE - MOVE_BYTES
+                  : d + MOVE_BYTES <= w && w <= WSIZE - MOVE_BYTES))
+            memcpy (slide + w, slide + d, MOVE_BYTES);
+          else if (w <= d || e <= w - d)
+            memmove (slide + w, slide + d, e);
+          else
+            {
+              /* A repeating sequence, where memmove does not suffice.  */
+              unsigned int ee = e, ww = w;
+              do
+                {
+                  unsigned int chunk = ee < ww - d ? ee : ww - d;
+                  memcpy (slide + ww, slide + d, chunk);
+                  ww += chunk;
+                  ee -= chunk;
+                }
+              while (ee);
+            }
+
           w += e;
           d += e;
         }
-        else                      /* do it slow to avoid memcpy() overlap */
-#endif
+#else
           do {
             slide[w++] = slide[d++];
             Tracevv((stderr, "%c", slide[w-1]));
           } while (--e);
+#endif
         if (w == WSIZE)
         {
           flush_output(w);
-- 
2.53.0

From 3c44961ec300520b115a9055c30c1169bd29273e Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Sun, 15 Nov 2026 18:28:53 -0800
Subject: [PATCH] gzip: memset speedup in inflate_codes

* inflate.c (inflate_codes): Use memset to speed up common
case where w - d == 1.
---
 inflate.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/inflate.c b/inflate.c
index e44f90a..5cc25bd 100644
--- a/inflate.c
+++ b/inflate.c
@@ -602,6 +602,8 @@ inflate_codes(struct huft *tl, struct huft *td, int bl, int bd)
                   ? w + MOVE_BYTES <= d && d <= WSIZE - MOVE_BYTES
                   : d + MOVE_BYTES <= w && w <= WSIZE - MOVE_BYTES))
             memcpy (slide + w, slide + d, MOVE_BYTES);
+          else if (w - d == 1)
+            memset (slide + w, slide[d], e);
           else if (w <= d || e <= w - d)
             memmove (slide + w, slide + d, e);
           else
-- 
2.53.0

Reply via email to