Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
222 changes: 138 additions & 84 deletions README.md

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions _generate/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2358,7 +2358,8 @@ func (o options) emitLiteral(name string, litLen, retval, dstBase, litBase reg.G
func (o options) genEmitRepeat() {
TEXT("emitRepeat", NOSPLIT, "func(dst []byte, length int) int")
Doc("emitRepeat writes a repeat chunk and returns the number of bytes written.",
"Length must be at least 4 and < 1<<32", "")
"Length must be at least 4 and < 1<<32.",
"dst must have room for the encoded bytes and one more: 3-byte lengths are stored with 4 bytes.", "")
Pragma("noescape")

dstBase, length, retval := GP64(), GP64(), GP64()
Expand Down Expand Up @@ -2452,7 +2453,7 @@ func (o options) emitRepeatLong(name string, length reg.GPVirtual, retval reg.GP
// emitCopy writes a copy chunk and returns the number of bytes written.
//
// It assumes that:
// dst is long enough to hold the encoded bytes
// dst has room for the encoded bytes and one more: 3-byte lengths are stored with 4 bytes
// 1 <= offset && offset <= math.MaxUint32
// 4 <= length && length <= 1 << 24

Expand All @@ -2461,7 +2462,7 @@ func (o options) genEmitCopy() {
TEXT("emitCopy", NOSPLIT, "func(dst []byte, offset, length int) int")
Doc("emitCopy writes a copy chunk and returns the number of bytes written.", "",
"It assumes that:",
" dst is long enough to hold the encoded bytes",
" dst has room for the encoded bytes and one more: 3-byte lengths are stored with 4 bytes",
" 1 <= offset && offset <= math.MaxUint32",
" 4 <= length && length <= 1 << 24", "")
Pragma("noescape")
Expand All @@ -2483,7 +2484,7 @@ func (o options) genEmitCopyLits2() {
TEXT("emitCopyLits2", NOSPLIT, "func(dst, lits []byte, offset, length int) int")
Doc("emitCopyLits2 writes a copy chunk and returns the number of bytes written.", "",
"It assumes that:",
" dst is long enough to hold the encoded bytes",
" dst has room for the encoded bytes and one more: 3-byte lengths are stored with 4 bytes",
" 1 <= offset && offset <= 65536",
" 4 <= length && length <= MaxBlockSize", "")
Pragma("noescape")
Expand Down Expand Up @@ -2519,7 +2520,7 @@ func (o options) genEmitCopyLits3() {
TEXT("emitCopyLits3", NOSPLIT, "func(dst, lits []byte, offset, length int) int")
Doc("emitCopyLits3 writes a copy chunk and returns the number of bytes written.", "",
"It assumes that:",
" dst is long enough to hold the encoded bytes",
" dst has room for the encoded bytes and one more: 3-byte lengths are stored with 4 bytes",
" 1 <= offset && offset <= (1<<21)",
" 4 <= length && length <= MaxBlockSize", "")
Pragma("noescape")
Expand Down
2 changes: 1 addition & 1 deletion _generate/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (

require (
github.com/klauspost/asmfmt v1.3.2 // indirect
github.com/klauspost/cpuid/v2 v2.4.0 // indirect
github.com/klauspost/cpuid/v2 v2.4.2 // indirect
golang.org/x/mod v0.38.0 // indirect
golang.org/x/sync v0.22.0 // indirect
golang.org/x/sys v0.47.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions _generate/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/honeycombio/avo v0.6.1-0.20261002035755-423abca816c4 h1:/JvaVg+U86gTB
github.com/honeycombio/avo v0.6.1-0.20261002035755-423abca816c4/go.mod h1:UovHDPo7jXYhI/boktf60RaGgzELEnl2+I5T+pLzN04=
github.com/klauspost/asmfmt v1.3.2 h1:4Ri7ox3EwapiOjCki+hw14RyKk201CN4rzyCJRFLpK4=
github.com/klauspost/asmfmt v1.3.2/go.mod h1:AG8TuvYojzulgDAMCnYn50l/5QV3Bs/tp6j0HLHbNSE=
github.com/klauspost/cpuid/v2 v2.4.0 h1:S6Hrbc7+ywsr0r+RLapfGBHfyefhCTwEh3A0tV913Dw=
github.com/klauspost/cpuid/v2 v2.4.0/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU=
github.com/klauspost/cpuid/v2 v2.4.2 h1:xRw7IZEsAgVCp6trS/DgWbTAqpixhmywgVgBjYGg1bQ=
github.com/klauspost/cpuid/v2 v2.4.2/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU=
golang.org/x/mod v0.38.0 h1:MECBjubtXD7yj4HrhIUcywNaGeNVUdfVnxmPajOk4yk=
golang.org/x/mod v0.38.0/go.mod h1:V6Xz0pq8TQ3dGqVQ1FVHuelZpAL0uNhSkk9ogYP3c40=
golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek=
Expand Down
9 changes: 5 additions & 4 deletions asm_amd64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions cmd/mz/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ func mainCompress(args []string) {
faster = fs.Bool("1", false, "Compress faster, but with a minor compression loss")
_ = fs.Bool("2", true, "Default compression speed")
slower = fs.Bool("3", false, "Compress more, but a lot slower")
ultra = fs.Bool("ultra", false, "Compress the most, about 20 times slower than -3, up to 75 times on very compressible data. Needs about 180MB of memory per -cpu thread with 8MB blocks, 125MB with -classic")
classic = fs.Bool("classic", false, "Write classic MinLZ blocks, which all MinLZ decoders can read. Without it, SIMD blocks are written, which need a decoder for MinLZ format specification v1.1")
simdDelta = fs.Bool("simd.delta", false, "Store SIMD literals as deltas, mainly for binary data. Not with -classic, -0 or -3, which decides itself")
simdDelta = fs.Bool("simd.delta", false, "Store SIMD literals as deltas, mainly for binary data. Not with -classic, -0, -3 or -ultra, which decide themselves")
recomp = fs.Bool("recomp", false, "Recompress MinLZ, Snappy or S2 input")
blockSize = fs.String("bs", "8M", "Max block size. Examples: 64K, 256K, 1M, 8M. Must be power of two and <= 8MB")
index = fs.Bool("index", true, "Add seek index")
Expand Down Expand Up @@ -125,14 +126,17 @@ Options:`)
if *slower {
level = minlz.LevelSmallest
}
if *ultra {
level = minlz.LevelUltra
}
if *superFast {
level = minlz.LevelSuperFast
}
if *nocomp {
level = minlz.LevelUncompressed
}
if *simdDelta && (*classic || level == minlz.LevelUncompressed || level == minlz.LevelSmallest) {
exitErr(errors.New("-simd.delta cannot be used with -classic, -0 or -3"))
if *simdDelta && (*classic || level == minlz.LevelUncompressed || level == minlz.LevelSmallest || level == minlz.LevelUltra) {
exitErr(errors.New("-simd.delta cannot be used with -classic, -0, -3 or -ultra"))
}
if !*classic {
switch level {
Expand All @@ -144,6 +148,8 @@ Options:`)
level = minlz.LevelBalancedSIMD
case minlz.LevelSmallest:
level = minlz.LevelSmallestSIMD
case minlz.LevelUltra:
level = minlz.LevelUltraSIMD
}
if *simdDelta {
level |= minlz.LevelSIMDDelta
Expand Down
2 changes: 1 addition & 1 deletion cmd/mz/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestStatsSIMD(t *testing.T) {
data = append(data, words[rng.IntN(len(words))]...)
data = append(data, byte(' '+rng.IntN(3)))
}
for _, level := range []int{minlz.LevelSuperFastSIMD, minlz.LevelFastestSIMD | minlz.LevelSIMDDelta, minlz.LevelBalancedSIMD, minlz.LevelSmallestSIMD} {
for _, level := range []int{minlz.LevelSuperFastSIMD, minlz.LevelFastestSIMD | minlz.LevelSIMDDelta, minlz.LevelBalancedSIMD, minlz.LevelSmallestSIMD, minlz.LevelUltraSIMD} {
block, err := minlz.Encode(nil, data, level)
if err != nil {
t.Fatal(err)
Expand Down
46 changes: 46 additions & 0 deletions decode-simd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,52 @@ import (
"github.com/zeebo/xxh3"
)

// simdTablesBuilt holds the tables the encoders and decoders share, as built.
var simdTablesBuilt = struct {
layouts [len(simdLayouts)]simdLayout
offTab [len(simdOffTab)]uint32
codeBytes [len(simdCodeBytes)]int
}{simdLayouts, simdOffTab, simdCodeBytes}

func init() { reference.CheckTables = simdTablesChanged }

// simdTablesChanged returns an error naming the shared tables that no longer hold what they were built with.
// The encoders and decoders then agree on blocks that the reference rejects.
func simdTablesChanged() error {
b := &simdTablesBuilt
// The reference calls this for every block it rejects.
if simdLayouts == b.layouts && simdOffTab == b.offTab && simdCodeBytes == b.codeBytes {
return nil
}
var errs []error
diff := func(name string, k int, cur, built []byte) {
for i := range cur {
if cur[i] != built[i] {
errs = append(errs, fmt.Errorf("simdLayouts[%d].%s[%d] changed from %d to %d", k, name, i, built[i], cur[i]))
return
}
}
}
for k := range simdLayouts {
l, lb := &simdLayouts[k], &b.layouts[k]
diff("class", k, l.class[:], lb.class[:])
diff("in", k, l.in[:], lb.in[:])
if l.n != lb.n {
errs = append(errs, fmt.Errorf("simdLayouts[%d].n changed from %d to %d", k, lb.n, l.n))
}
}
for i, v := range simdOffTab {
if v != b.offTab[i] {
errs = append(errs, fmt.Errorf("simdOffTab[%d] changed from %#x to %#x", i, b.offTab[i], v))
break
}
}
if simdCodeBytes != b.codeBytes {
errs = append(errs, fmt.Errorf("simdCodeBytes changed from %v to %v", b.codeBytes, simdCodeBytes))
}
return errors.Join(errs...)
}

// The test block builder below is written from SPEC_SIMD.md, independently of the encoder.

// simdTestOp copies ll literals, then ml bytes from offset off back. off 0 repeats the offset.
Expand Down
34 changes: 26 additions & 8 deletions encode-simd.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ type simdEncoder struct {
// fastCode, when set, makes put build one table at most: code lengths for at least fastCode values,
// else class lengths. Building tables dominates the encoding of small blocks.
fastCode int
// freeTables costs new tables only their size, without simdTableBits: LevelUltraSIMD doesn't trade size for decode time.
// encodeBlockSIMD sets it, so it holds for the fallback parse of LevelUltraSIMD too.
freeTables bool
// litSave, when set, makes put store a literal block unless coding it saves more than 1/(1<<litSave)
// of its size, on top of the cost of its table. Literals that hardly compress take the longest to decode
// for the least gain, and stored literals are read in place.
Expand Down Expand Up @@ -142,8 +145,12 @@ func encodeBlockSIMD(dst, src []byte, level int) int {
e = new(simdEncoder)
}
e.out = dst[:0:len(dst)]
e.freeTables = level == LevelUltraSIMD
e.parse(src, level)
n := e.finish()
if level == LevelUltraSIMD {
n = e.ultraFallback(dst, src, n)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
// Don't keep the buffers alive in the pool.
e.src, e.tail, e.out = nil, nil, nil
simdEncoders.Put(e)
Expand Down Expand Up @@ -182,7 +189,7 @@ func (e *simdEncoder) parse(src []byte, level int) {
delta = -1
}
asm := simdEncAsm && delta == 0 && !simdNoOverlap && !simdLongRuns
e.splitLits = level == LevelSmallestSIMD
e.splitLits = level == LevelSmallestSIMD || level == LevelUltraSIMD
e.fastCode, e.litSave = 0, 0
e.noOverlap, e.budget = simdNoOverlap, false
switch level &^ LevelSIMDDelta {
Expand Down Expand Up @@ -228,6 +235,9 @@ func (e *simdEncoder) parse(src []byte, level int) {
default:
e.encodeBlockBetterGo(src)
}
case LevelUltraSIMD:
e.reset(src, 256, 0)
e.encodeBlockUltra(src)
default:
e.reset(src, 256, 0)
e.encodeBlockBest(src)
Expand Down Expand Up @@ -756,7 +766,7 @@ func (e *simdEncoder) split(lits []byte, start int) {
h[v] += uint64(f)
}
// An entry is about 3 bytes.
if c := cost[j] + 3*8 + simdLitBits(&h, min(i*simdLitSeg, len(lits))-j*simdLitSeg); c < cost[i] {
if c := cost[j] + 3*8 + simdLitBits(&h, min(i*simdLitSeg, len(lits))-j*simdLitSeg, e.tableBits()); c < cost[i] {
cost[i], from[i] = c, j
}
}
Expand All @@ -769,9 +779,9 @@ func (e *simdEncoder) split(lits []byte, start int) {
}

// simdLitBits estimates the size in bits of a literal block of n values with histogram h:
// stored, or its entropy plus the overhead of a block with a new table of about 16 bytes.
// stored, or its entropy plus the overhead of a block with a new table of about 16 bytes, which costs tableBits more.
// The entropy is cheaper than building the tables, which would dominate the encoding of literal-heavy data.
func simdLitBits(h *[256]uint64, n int) int {
func simdLitBits(h *[256]uint64, n, tableBits int) int {
if n < simdMinCoded {
return 8 * n
}
Expand All @@ -782,7 +792,15 @@ func simdLitBits(h *[256]uint64, n int) int {
e -= int(f) * simdLog2(f)
}
}
return min(8*n, max(e>>8, 0)+simdBlockBits+simdTableBits+16*8)
return min(8*n, max(e>>8, 0)+simdBlockBits+tableBits+16*8)
}

// tableBits returns what a new table costs on top of its size.
func (e *simdEncoder) tableBits() int {
if e.freeTables {
return 0
}
return simdTableBits
}

// simdEntropyBits returns a lower bound of the entropy in bits of the counts h of n values.
Expand Down Expand Up @@ -886,17 +904,17 @@ func (e *simdEncoder) put(recs []byte, k int, v []byte) []byte {
if e.fastCode == 0 || best == nil {
// No prefix code codes v in fewer bits than its entropy, so new tables that can't win with that aren't built.
// The entropy is at most simdMaxEntropy per value, so it is only computed when it can stop a table.
low := simdTableBits + blockBits
low := e.tableBits() + blockBits
if low < bestBits && bestBits <= low+(len(v)*simdMaxEntropy[k]+255)>>8+8*simdCodeBytes[k] {
low += simdEntropyBits(e.hist[:syms], len(v))
}
if len(v) >= e.fastCode && low+8*simdCodeBytes[k] < bestBits {
e.tmp[0].setCode(k, &e.hist)
try(&e.tmp[0], simdTableBits+8*e.tmp[0].n)
try(&e.tmp[0], e.tableBits()+8*e.tmp[0].n)
}
if (e.fastCode == 0 || len(v) < e.fastCode) && low+8*simdClassBytes[layout] < bestBits {
e.tmp[1].setClass(layout, &e.hist, syms)
try(&e.tmp[1], simdTableBits+8*e.tmp[1].n)
try(&e.tmp[1], e.tableBits()+8*e.tmp[1].n)
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions encode-simd_asm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,10 @@ func TestSIMDEncodeAsm(t *testing.T) {
parse, text := simdTestParseInput(rng), genEncText(rng, 1<<20)
gd, ad := make([]byte, MaxEncodedLen(1<<20)), make([]byte, MaxEncodedLen(1<<20))
for _, level := range simdLevels {
// LevelUltraSIMD has no assembly parser either, and is slow.
if level == LevelUltraSIMD {
continue
}
asm := level&LevelSIMDDelta == 0 && level != LevelSmallestSIMD
for _, n := range []int{4 << 10, 64 << 10, 64<<10 + 1, 1 << 20} {
for _, src := range [][]byte{parse[:n], text[:n]} {
Expand Down
26 changes: 23 additions & 3 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ const (
// See the README for details.
LevelSmallestSIMD = 7

// LevelUltra searches much harder for matches than LevelSmallest, and picks the combination
// of matches and literals that codes smallest. Output is on average 1-11% smaller than LevelSmallest,
// more with larger blocks. Compressing blocks of 2MB and more is about 20 times slower,
// and up to 75 times on very compressible data, such as logs.
// Compressing a block of 8MB takes about 90MB of memory.
LevelUltra = 8

// LevelUltraSIMD is LevelUltra for SIMD blocks. Output is on average 4-13% smaller than LevelSmallestSIMD,
// and practically never larger. Compressing a block of 8MB takes about 150MB of memory.
LevelUltraSIMD = 9

// LevelSIMDDelta will encode delta literals in SIMD mode.
// This is mainly useful for binary encoded data and has a small decode penalty.
// Use as a flag, ie LevelSIMDDelta | LevelBalancedSIMD.
Expand All @@ -95,6 +106,11 @@ const (
minCopy3Length = 64
)

// isSIMDLevel reports whether level writes SIMD blocks.
func isSIMDLevel(level int) bool {
return level >= LevelSuperFastSIMD && level != LevelUltra
}

// Encode returns the encoded form of src. The returned slice may be a sub-
// slice of dst if dst was large enough to hold the entire encoded block.
// Otherwise, a newly allocated slice will be returned.
Expand Down Expand Up @@ -137,7 +153,9 @@ func Encode(dst, src []byte, level int) ([]byte, error) {
n = encodeBlockBetter(dst[d:], src)
case LevelSmallest:
n = encodeBlockBest(dst[d:], src)
case LevelSuperFastSIMD, LevelFastestSIMD, LevelBalancedSIMD, LevelSmallestSIMD,
case LevelUltra:
n = encodeBlockUltra(dst[d:], src)
case LevelSuperFastSIMD, LevelFastestSIMD, LevelBalancedSIMD, LevelSmallestSIMD, LevelUltraSIMD,
LevelSuperFastSIMD | LevelSIMDDelta, LevelFastestSIMD | LevelSIMDDelta, LevelBalancedSIMD | LevelSIMDDelta:
d = 1
n = encodeBlockSIMD(dst[d:], src, level)
Expand All @@ -158,7 +176,7 @@ func Encode(dst, src []byte, level int) ([]byte, error) {
block := dst[d : d+n]
dst := make([]byte, len(src))
ret := 0
if level >= LevelSuperFastSIMD {
if isSIMDLevel(level) {
if _, b, err := simdHeader(block); err != nil || decodeSIMD(dst, b) != nil {
ret = 1
}
Expand Down Expand Up @@ -240,7 +258,9 @@ func TryEncode(dst, src []byte, level int) []byte {
n = encodeBlockBetter(dst[d:], src)
case LevelSmallest:
n = encodeBlockBest(dst[d:], src)
case LevelSuperFastSIMD, LevelFastestSIMD, LevelBalancedSIMD, LevelSmallestSIMD,
case LevelUltra:
n = encodeBlockUltra(dst[d:], src)
case LevelSuperFastSIMD, LevelFastestSIMD, LevelBalancedSIMD, LevelSmallestSIMD, LevelUltraSIMD,
LevelSuperFastSIMD | LevelSIMDDelta, LevelFastestSIMD | LevelSIMDDelta, LevelBalancedSIMD | LevelSIMDDelta:
d = 1
n = encodeBlockSIMD(dst[d:], src, level)
Expand Down
2 changes: 1 addition & 1 deletion encode_l3.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ func (e *simdEncoder) encodeBlockBest(src []byte) {
}
// Literals cost the mean code length of src, which fits the data better than a fixed cost.
litBits := 8 * 16
if n := e.litBits(src); n+simdBlockBits+simdTableBits < 8*len(src) {
if n := e.litBits(src); n+simdBlockBits+e.tableBits() < 8*len(src) {
litBits = 16 * n / len(src)
}

Expand Down
Loading
Loading