]> Cypherpunks repositories - gostls13.git/commitdiff
compress/lzw: clarify code invariants
authorNigel Tao <nigeltao@golang.org>
Thu, 12 Sep 2019 07:56:57 +0000 (17:56 +1000)
committerNigel Tao <nigeltao@golang.org>
Sat, 14 Sep 2019 02:23:31 +0000 (02:23 +0000)
This follows on from https://go-review.googlesource.com/c/go/+/191358
which was submitted as a comment-only change.

Benchmarks don't show any significant change:

compress/lzw
name            old speed      new speed      delta
Decoder/1e4-56  92.8MB/s ± 1%  92.7MB/s ± 1%   ~     (p=1.000 n=5+5)
Decoder/1e5-56   100MB/s ± 1%   100MB/s ± 1%   ~     (p=0.746 n=5+5)
Decoder/1e6-56   101MB/s ± 1%   101MB/s ± 1%   ~     (p=0.381 n=5+5)

image/gif
name                old speed      new speed      delta
Decode-56           63.2MB/s ± 1%  63.2MB/s ± 1%   ~     (p=0.690 n=5+5)

Change-Id: Ic36b5410cb06ca258da32e40da1f1ff6c44cff86
Reviewed-on: https://go-review.googlesource.com/c/go/+/194938
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/compress/lzw/reader.go

index 912b7d3f40d994a2a18841f53037c59addfd97f7..f08021190c2cfbb277bffe8d15d99746f8d56304 100644 (file)
@@ -199,6 +199,9 @@ loop:
                }
                d.last, d.hi = code, d.hi+1
                if d.hi >= d.overflow {
+                       if d.hi > d.overflow {
+                               panic("unreachable")
+                       }
                        if d.width == maxWidth {
                                d.last = decoderInvalidCode
                                // Undo the d.hi++ a few lines above, so that (1) we maintain
@@ -207,7 +210,7 @@ loop:
                                d.hi--
                        } else {
                                d.width++
-                               d.overflow <<= 1
+                               d.overflow = 1 << d.width
                        }
                }
                if d.o >= flushBuffer {