| 1 |
//go:build amd64 && !appengine && !noasm && gc |
| 2 |
|
| 3 |
package s2 |
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"math/rand" |
| 8 |
"testing" |
| 9 |
) |
| 10 |
|
| 11 |
// mixedRunsForSkip builds total bytes of alternating incompressible and |
| 12 |
// compressible runs of runLen each. |
| 13 |
// |
| 14 |
// Long runs are the point. The adaptive skip only grows past the clamp after a |
| 15 |
// long stretch with no matches, so shapes with short noise runs -- and uniform |
| 16 |
// shapes of either kind -- cannot distinguish a clamped encoder from an |
| 17 |
// unclamped one. On uniform text the two agree either way. |
| 18 |
func mixedRunsForSkip(rng *rand.Rand, total, runLen int) []byte { |
| 19 |
words := []string{"lorem", "ipsum", "dolor", "sit", "amet", "consectetur"} |
| 20 |
out := make([]byte, 0, total+2*runLen) |
| 21 |
for len(out) < total { |
| 22 |
noise := make([]byte, runLen) |
| 23 |
rng.Read(noise) |
| 24 |
out = append(out, noise...) |
| 25 |
txt := make([]byte, 0, runLen+16) |
| 26 |
for len(txt) < runLen { |
| 27 |
txt = append(txt, words[rng.Intn(len(words))]...) |
| 28 |
txt = append(txt, ' ') |
| 29 |
} |
| 30 |
out = append(out, txt[:runLen]...) |
| 31 |
} |
| 32 |
return out[:total] |
| 33 |
} |
| 34 |
|
| 35 |
// TestBetterGoSkipMatchesAsm requires encodeBlockBetterGo to produce exactly |
| 36 |
// what the assembly produces on blocks above 64KB. |
| 37 |
// |
| 38 |
// Both clamp the adaptive scan skip at 100. Before the Go side did, it emitted |
| 39 |
// 0.10%-0.25% more bytes on these shapes, because after a long matchless |
| 40 |
// stretch its unclamped skip strode past the start of the next compressible |
| 41 |
// region and lost the matches there. |
| 42 |
// |
| 43 |
// Byte equality is a deliberately strong assertion: it is what the clamp buys, |
| 44 |
// and it is currently true, so anything that breaks it is a divergence worth |
| 45 |
// looking at rather than noise. Only the >64KB path is covered -- |
| 46 |
// encodeBlockBetterGo64K is a different configuration (skipLog 6 rather than 7) |
| 47 |
// and does not clamp. |
| 48 |
func TestBetterGoSkipMatchesAsm(t *testing.T) { |
| 49 |
for _, runLen := range []int{16 << 10, 64 << 10, 256 << 10, 1 << 20} { |
| 50 |
src := mixedRunsForSkip(rand.New(rand.NewSource(1)), 4<<20, runLen) |
| 51 |
if len(src) <= 64<<10 { |
| 52 |
t.Fatalf("block must exceed 64KB to reach encodeBlockBetterGo") |
| 53 |
} |
| 54 |
asm := make([]byte, MaxEncodedLen(len(src))) |
| 55 |
goOut := make([]byte, MaxEncodedLen(len(src))) |
| 56 |
nAsm := encodeBlockBetter(asm, src) |
| 57 |
nGo := encodeBlockBetterGo(goOut, src) |
| 58 |
if nGo != nAsm || !bytes.Equal(goOut[:nGo], asm[:nAsm]) { |
| 59 |
t.Errorf("noise runs of %d bytes: Go emitted %d bytes, assembly %d (%+.3f%%) -- is the skip clamp still in encodeBlockBetterGo?", |
| 60 |
runLen, nGo, nAsm, 100*float64(nGo-nAsm)/float64(nAsm)) |
| 61 |
} |
| 62 |
} |
| 63 |
} |