2022-07-03 10:08:30 +00:00
|
|
|
//go:build !appengine && !noasm && gc
|
|
|
|
// +build !appengine,!noasm,gc
|
|
|
|
|
|
|
|
package s2
|
|
|
|
|
2024-10-14 09:46:02 +00:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/klauspost/compress/internal/race"
|
|
|
|
)
|
2024-03-11 10:51:13 +00:00
|
|
|
|
2023-03-30 21:39:55 +00:00
|
|
|
const hasAmd64Asm = true
|
|
|
|
|
2024-10-14 09:46:02 +00:00
|
|
|
var encPools [4]sync.Pool
|
|
|
|
|
2022-07-03 10:08:30 +00:00
|
|
|
// encodeBlock encodes a non-empty src to a guaranteed-large-enough dst. It
|
|
|
|
// assumes that the varint-encoded length of the decompressed bytes has already
|
|
|
|
// been written.
|
|
|
|
//
|
|
|
|
// It also assumes that:
|
2023-02-27 09:21:58 +00:00
|
|
|
//
|
2022-07-03 10:08:30 +00:00
|
|
|
// len(dst) >= MaxEncodedLen(len(src)) &&
|
2023-02-27 09:21:58 +00:00
|
|
|
// minNonLiteralBlockSize <= len(src) && len(src) <= maxBlockSize
|
2022-07-03 10:08:30 +00:00
|
|
|
func encodeBlock(dst, src []byte) (d int) {
|
2024-03-11 10:51:13 +00:00
|
|
|
race.ReadSlice(src)
|
|
|
|
race.WriteSlice(dst)
|
|
|
|
|
2022-07-03 10:08:30 +00:00
|
|
|
const (
|
|
|
|
// Use 12 bit table when less than...
|
|
|
|
limit12B = 16 << 10
|
|
|
|
// Use 10 bit table when less than...
|
|
|
|
limit10B = 4 << 10
|
|
|
|
// Use 8 bit table when less than...
|
|
|
|
limit8B = 512
|
|
|
|
)
|
|
|
|
|
|
|
|
if len(src) >= 4<<20 {
|
2024-10-14 09:46:02 +00:00
|
|
|
const sz, pool = 65536, 0
|
|
|
|
tmp, ok := encPools[pool].Get().(*[sz]byte)
|
|
|
|
if !ok {
|
|
|
|
tmp = &[sz]byte{}
|
|
|
|
}
|
|
|
|
race.WriteSlice(tmp[:])
|
|
|
|
defer encPools[pool].Put(tmp)
|
|
|
|
return encodeBlockAsm(dst, src, tmp)
|
2022-07-03 10:08:30 +00:00
|
|
|
}
|
|
|
|
if len(src) >= limit12B {
|
2024-10-14 09:46:02 +00:00
|
|
|
const sz, pool = 65536, 0
|
|
|
|
tmp, ok := encPools[pool].Get().(*[sz]byte)
|
|
|
|
if !ok {
|
|
|
|
tmp = &[sz]byte{}
|
|
|
|
}
|
|
|
|
race.WriteSlice(tmp[:])
|
|
|
|
defer encPools[pool].Put(tmp)
|
|
|
|
return encodeBlockAsm4MB(dst, src, tmp)
|
2022-07-03 10:08:30 +00:00
|
|
|
}
|
|
|
|
if len(src) >= limit10B {
|
2024-10-14 09:46:02 +00:00
|
|
|
const sz, pool = 16384, 1
|
|
|
|
tmp, ok := encPools[pool].Get().(*[sz]byte)
|
|
|
|
if !ok {
|
|
|
|
tmp = &[sz]byte{}
|
|
|
|
}
|
|
|
|
race.WriteSlice(tmp[:])
|
|
|
|
defer encPools[pool].Put(tmp)
|
|
|
|
return encodeBlockAsm12B(dst, src, tmp)
|
2022-07-03 10:08:30 +00:00
|
|
|
}
|
|
|
|
if len(src) >= limit8B {
|
2024-10-14 09:46:02 +00:00
|
|
|
const sz, pool = 4096, 2
|
|
|
|
tmp, ok := encPools[pool].Get().(*[sz]byte)
|
|
|
|
if !ok {
|
|
|
|
tmp = &[sz]byte{}
|
|
|
|
}
|
|
|
|
race.WriteSlice(tmp[:])
|
|
|
|
defer encPools[pool].Put(tmp)
|
|
|
|
return encodeBlockAsm10B(dst, src, tmp)
|
2022-07-03 10:08:30 +00:00
|
|
|
}
|
|
|
|
if len(src) < minNonLiteralBlockSize {
|
|
|
|
return 0
|
|
|
|
}
|
2024-10-14 09:46:02 +00:00
|
|
|
const sz, pool = 1024, 3
|
|
|
|
tmp, ok := encPools[pool].Get().(*[sz]byte)
|
|
|
|
if !ok {
|
|
|
|
tmp = &[sz]byte{}
|
|
|
|
}
|
|
|
|
race.WriteSlice(tmp[:])
|
|
|
|
defer encPools[pool].Put(tmp)
|
|
|
|
return encodeBlockAsm8B(dst, src, tmp)
|
2022-07-03 10:08:30 +00:00
|
|
|
}
|
|
|
|
|
2024-10-14 09:46:02 +00:00
|
|
|
var encBetterPools [5]sync.Pool
|
|
|
|
|
2022-07-03 10:08:30 +00:00
|
|
|
// encodeBlockBetter encodes a non-empty src to a guaranteed-large-enough dst. It
|
|
|
|
// assumes that the varint-encoded length of the decompressed bytes has already
|
|
|
|
// been written.
|
|
|
|
//
|
|
|
|
// It also assumes that:
|
2023-02-27 09:21:58 +00:00
|
|
|
//
|
2022-07-03 10:08:30 +00:00
|
|
|
// len(dst) >= MaxEncodedLen(len(src)) &&
|
2023-02-27 09:21:58 +00:00
|
|
|
// minNonLiteralBlockSize <= len(src) && len(src) <= maxBlockSize
|
2022-07-03 10:08:30 +00:00
|
|
|
func encodeBlockBetter(dst, src []byte) (d int) {
|
2024-03-11 10:51:13 +00:00
|
|
|
race.ReadSlice(src)
|
|
|
|
race.WriteSlice(dst)
|
|
|
|
|
2022-07-03 10:08:30 +00:00
|
|
|
const (
|
|
|
|
// Use 12 bit table when less than...
|
|
|
|
limit12B = 16 << 10
|
|
|
|
// Use 10 bit table when less than...
|
|
|
|
limit10B = 4 << 10
|
|
|
|
// Use 8 bit table when less than...
|
|
|
|
limit8B = 512
|
|
|
|
)
|
|
|
|
|
|
|
|
if len(src) > 4<<20 {
|
2024-10-14 09:46:02 +00:00
|
|
|
const sz, pool = 589824, 0
|
|
|
|
tmp, ok := encBetterPools[pool].Get().(*[sz]byte)
|
|
|
|
if !ok {
|
|
|
|
tmp = &[sz]byte{}
|
|
|
|
}
|
|
|
|
race.WriteSlice(tmp[:])
|
|
|
|
defer encBetterPools[pool].Put(tmp)
|
|
|
|
return encodeBetterBlockAsm(dst, src, tmp)
|
2022-07-03 10:08:30 +00:00
|
|
|
}
|
|
|
|
if len(src) >= limit12B {
|
2024-10-14 09:46:02 +00:00
|
|
|
const sz, pool = 589824, 0
|
|
|
|
tmp, ok := encBetterPools[pool].Get().(*[sz]byte)
|
|
|
|
if !ok {
|
|
|
|
tmp = &[sz]byte{}
|
|
|
|
}
|
|
|
|
race.WriteSlice(tmp[:])
|
|
|
|
defer encBetterPools[pool].Put(tmp)
|
|
|
|
|
|
|
|
return encodeBetterBlockAsm4MB(dst, src, tmp)
|
2022-07-03 10:08:30 +00:00
|
|
|
}
|
|
|
|
if len(src) >= limit10B {
|
2024-10-14 09:46:02 +00:00
|
|
|
const sz, pool = 81920, 0
|
|
|
|
tmp, ok := encBetterPools[pool].Get().(*[sz]byte)
|
|
|
|
if !ok {
|
|
|
|
tmp = &[sz]byte{}
|
|
|
|
}
|
|
|
|
race.WriteSlice(tmp[:])
|
|
|
|
defer encBetterPools[pool].Put(tmp)
|
|
|
|
|
|
|
|
return encodeBetterBlockAsm12B(dst, src, tmp)
|
2022-07-03 10:08:30 +00:00
|
|
|
}
|
|
|
|
if len(src) >= limit8B {
|
2024-10-14 09:46:02 +00:00
|
|
|
const sz, pool = 20480, 1
|
|
|
|
tmp, ok := encBetterPools[pool].Get().(*[sz]byte)
|
|
|
|
if !ok {
|
|
|
|
tmp = &[sz]byte{}
|
|
|
|
}
|
|
|
|
race.WriteSlice(tmp[:])
|
|
|
|
defer encBetterPools[pool].Put(tmp)
|
|
|
|
return encodeBetterBlockAsm10B(dst, src, tmp)
|
2022-07-03 10:08:30 +00:00
|
|
|
}
|
|
|
|
if len(src) < minNonLiteralBlockSize {
|
|
|
|
return 0
|
|
|
|
}
|
2024-10-14 09:46:02 +00:00
|
|
|
|
|
|
|
const sz, pool = 5120, 2
|
|
|
|
tmp, ok := encBetterPools[pool].Get().(*[sz]byte)
|
|
|
|
if !ok {
|
|
|
|
tmp = &[sz]byte{}
|
|
|
|
}
|
|
|
|
race.WriteSlice(tmp[:])
|
|
|
|
defer encBetterPools[pool].Put(tmp)
|
|
|
|
return encodeBetterBlockAsm8B(dst, src, tmp)
|
2022-07-03 10:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// encodeBlockSnappy encodes a non-empty src to a guaranteed-large-enough dst. It
|
|
|
|
// assumes that the varint-encoded length of the decompressed bytes has already
|
|
|
|
// been written.
|
|
|
|
//
|
|
|
|
// It also assumes that:
|
2023-02-27 09:21:58 +00:00
|
|
|
//
|
2022-07-03 10:08:30 +00:00
|
|
|
// len(dst) >= MaxEncodedLen(len(src)) &&
|
2023-02-27 09:21:58 +00:00
|
|
|
// minNonLiteralBlockSize <= len(src) && len(src) <= maxBlockSize
|
2022-07-03 10:08:30 +00:00
|
|
|
func encodeBlockSnappy(dst, src []byte) (d int) {
|
2024-03-11 10:51:13 +00:00
|
|
|
race.ReadSlice(src)
|
|
|
|
race.WriteSlice(dst)
|
|
|
|
|
2022-07-03 10:08:30 +00:00
|
|
|
const (
|
|
|
|
// Use 12 bit table when less than...
|
|
|
|
limit12B = 16 << 10
|
|
|
|
// Use 10 bit table when less than...
|
|
|
|
limit10B = 4 << 10
|
|
|
|
// Use 8 bit table when less than...
|
|
|
|
limit8B = 512
|
|
|
|
)
|
2024-10-14 09:46:02 +00:00
|
|
|
if len(src) > 65536 {
|
|
|
|
const sz, pool = 65536, 0
|
|
|
|
tmp, ok := encPools[pool].Get().(*[sz]byte)
|
|
|
|
if !ok {
|
|
|
|
tmp = &[sz]byte{}
|
|
|
|
}
|
|
|
|
race.WriteSlice(tmp[:])
|
|
|
|
defer encPools[pool].Put(tmp)
|
|
|
|
return encodeSnappyBlockAsm(dst, src, tmp)
|
2022-07-03 10:08:30 +00:00
|
|
|
}
|
|
|
|
if len(src) >= limit12B {
|
2024-10-14 09:46:02 +00:00
|
|
|
const sz, pool = 65536, 0
|
|
|
|
tmp, ok := encPools[pool].Get().(*[sz]byte)
|
|
|
|
if !ok {
|
|
|
|
tmp = &[sz]byte{}
|
|
|
|
}
|
|
|
|
race.WriteSlice(tmp[:])
|
|
|
|
defer encPools[pool].Put(tmp)
|
|
|
|
return encodeSnappyBlockAsm64K(dst, src, tmp)
|
2022-07-03 10:08:30 +00:00
|
|
|
}
|
|
|
|
if len(src) >= limit10B {
|
2024-10-14 09:46:02 +00:00
|
|
|
const sz, pool = 16384, 1
|
|
|
|
tmp, ok := encPools[pool].Get().(*[sz]byte)
|
|
|
|
if !ok {
|
|
|
|
tmp = &[sz]byte{}
|
|
|
|
}
|
|
|
|
race.WriteSlice(tmp[:])
|
|
|
|
defer encPools[pool].Put(tmp)
|
|
|
|
return encodeSnappyBlockAsm12B(dst, src, tmp)
|
2022-07-03 10:08:30 +00:00
|
|
|
}
|
|
|
|
if len(src) >= limit8B {
|
2024-10-14 09:46:02 +00:00
|
|
|
const sz, pool = 4096, 2
|
|
|
|
tmp, ok := encPools[pool].Get().(*[sz]byte)
|
|
|
|
if !ok {
|
|
|
|
tmp = &[sz]byte{}
|
|
|
|
}
|
|
|
|
race.WriteSlice(tmp[:])
|
|
|
|
defer encPools[pool].Put(tmp)
|
|
|
|
return encodeSnappyBlockAsm10B(dst, src, tmp)
|
2022-07-03 10:08:30 +00:00
|
|
|
}
|
|
|
|
if len(src) < minNonLiteralBlockSize {
|
|
|
|
return 0
|
|
|
|
}
|
2024-10-14 09:46:02 +00:00
|
|
|
const sz, pool = 1024, 3
|
|
|
|
tmp, ok := encPools[pool].Get().(*[sz]byte)
|
|
|
|
if !ok {
|
|
|
|
tmp = &[sz]byte{}
|
|
|
|
}
|
|
|
|
race.WriteSlice(tmp[:])
|
|
|
|
defer encPools[pool].Put(tmp)
|
|
|
|
return encodeSnappyBlockAsm8B(dst, src, tmp)
|
2022-07-03 10:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// encodeBlockSnappy encodes a non-empty src to a guaranteed-large-enough dst. It
|
|
|
|
// assumes that the varint-encoded length of the decompressed bytes has already
|
|
|
|
// been written.
|
|
|
|
//
|
|
|
|
// It also assumes that:
|
2023-02-27 09:21:58 +00:00
|
|
|
//
|
2022-07-03 10:08:30 +00:00
|
|
|
// len(dst) >= MaxEncodedLen(len(src)) &&
|
2023-02-27 09:21:58 +00:00
|
|
|
// minNonLiteralBlockSize <= len(src) && len(src) <= maxBlockSize
|
2022-07-03 10:08:30 +00:00
|
|
|
func encodeBlockBetterSnappy(dst, src []byte) (d int) {
|
2024-03-11 10:51:13 +00:00
|
|
|
race.ReadSlice(src)
|
|
|
|
race.WriteSlice(dst)
|
|
|
|
|
2022-07-03 10:08:30 +00:00
|
|
|
const (
|
|
|
|
// Use 12 bit table when less than...
|
|
|
|
limit12B = 16 << 10
|
|
|
|
// Use 10 bit table when less than...
|
|
|
|
limit10B = 4 << 10
|
|
|
|
// Use 8 bit table when less than...
|
|
|
|
limit8B = 512
|
|
|
|
)
|
2024-10-14 09:46:02 +00:00
|
|
|
if len(src) > 65536 {
|
|
|
|
const sz, pool = 589824, 0
|
|
|
|
tmp, ok := encBetterPools[pool].Get().(*[sz]byte)
|
|
|
|
if !ok {
|
|
|
|
tmp = &[sz]byte{}
|
|
|
|
}
|
|
|
|
race.WriteSlice(tmp[:])
|
|
|
|
defer encBetterPools[pool].Put(tmp)
|
|
|
|
return encodeSnappyBetterBlockAsm(dst, src, tmp)
|
2022-07-03 10:08:30 +00:00
|
|
|
}
|
2024-10-14 09:46:02 +00:00
|
|
|
|
2022-07-03 10:08:30 +00:00
|
|
|
if len(src) >= limit12B {
|
2024-10-14 09:46:02 +00:00
|
|
|
const sz, pool = 294912, 4
|
|
|
|
tmp, ok := encBetterPools[pool].Get().(*[sz]byte)
|
|
|
|
if !ok {
|
|
|
|
tmp = &[sz]byte{}
|
|
|
|
}
|
|
|
|
race.WriteSlice(tmp[:])
|
|
|
|
defer encBetterPools[pool].Put(tmp)
|
|
|
|
|
|
|
|
return encodeSnappyBetterBlockAsm64K(dst, src, tmp)
|
2022-07-03 10:08:30 +00:00
|
|
|
}
|
|
|
|
if len(src) >= limit10B {
|
2024-10-14 09:46:02 +00:00
|
|
|
const sz, pool = 81920, 0
|
|
|
|
tmp, ok := encBetterPools[pool].Get().(*[sz]byte)
|
|
|
|
if !ok {
|
|
|
|
tmp = &[sz]byte{}
|
|
|
|
}
|
|
|
|
race.WriteSlice(tmp[:])
|
|
|
|
defer encBetterPools[pool].Put(tmp)
|
|
|
|
|
|
|
|
return encodeSnappyBetterBlockAsm12B(dst, src, tmp)
|
2022-07-03 10:08:30 +00:00
|
|
|
}
|
|
|
|
if len(src) >= limit8B {
|
2024-10-14 09:46:02 +00:00
|
|
|
const sz, pool = 20480, 1
|
|
|
|
tmp, ok := encBetterPools[pool].Get().(*[sz]byte)
|
|
|
|
if !ok {
|
|
|
|
tmp = &[sz]byte{}
|
|
|
|
}
|
|
|
|
race.WriteSlice(tmp[:])
|
|
|
|
defer encBetterPools[pool].Put(tmp)
|
|
|
|
return encodeSnappyBetterBlockAsm10B(dst, src, tmp)
|
2022-07-03 10:08:30 +00:00
|
|
|
}
|
|
|
|
if len(src) < minNonLiteralBlockSize {
|
|
|
|
return 0
|
|
|
|
}
|
2024-10-14 09:46:02 +00:00
|
|
|
|
|
|
|
const sz, pool = 5120, 2
|
|
|
|
tmp, ok := encBetterPools[pool].Get().(*[sz]byte)
|
|
|
|
if !ok {
|
|
|
|
tmp = &[sz]byte{}
|
|
|
|
}
|
|
|
|
race.WriteSlice(tmp[:])
|
|
|
|
defer encBetterPools[pool].Put(tmp)
|
|
|
|
return encodeSnappyBetterBlockAsm8B(dst, src, tmp)
|
2022-07-03 10:08:30 +00:00
|
|
|
}
|