lib/protocol: Require at least 3.125% savings from compression (#8133)

* lib/protocol: Require at least 3.125% savings from compression

The new lz4 library doesn't need its output buffer to be the maximum
size, unlike the old one (which would allocate if it weren't). It can
take a buffer that is of a smaller size and will report if compressed
data can fit inside the buffer (with a small chance of reporting a false
negative). Use that property to our advantage by requiring compressed
data to be at most n-n/32 = .96875*n bytes long for n input bytes.

* lib/protocol: Remove unused receivers

To make DeepSource happy.

* lib/protocol: Micro-optimize lz4Compress

Only write the length if compression was successful. This is a memory
write, so the compiler can't reorder it.

Only check the return value of lz4.CompressBlock.  Length-zero inputs
are always expanded by LZ4 compression (the library documents this),
so the check on len(src) isn't needed.
This commit is contained in:
greatroar
2022-01-24 19:36:58 +01:00
committed by GitHub
parent 1af87577e1
commit a0fd619df3
2 changed files with 18 additions and 15 deletions
+1 -1
View File
@@ -466,7 +466,7 @@ func TestWriteCompressed(t *testing.T) {
t.Error("received the wrong message")
}
hdr := Header{Type: c.typeOf(msg)}
hdr := Header{Type: typeOf(msg)}
size := int64(2 + hdr.ProtoSize() + 4 + msg.ProtoSize())
if c.cr.tot > size {
t.Errorf("compression enlarged message from %d to %d",