| 1 |
//go:build (amd64 || arm64) && !appengine && !noasm && gc |
| 2 |
|
| 3 |
package s2 |
| 4 |
|
| 5 |
import ( |
| 6 |
"sync" |
| 7 |
|
| 8 |
"github.com/klauspost/compress/internal/race" |
| 9 |
) |
| 10 |
|
| 11 |
const hasAsm = true |
| 12 |
|
| 13 |
var encPools [4]sync.Pool |
| 14 |
|
| 15 |
// encodeBlock encodes a non-empty src to a guaranteed-large-enough dst. It |
| 16 |
// assumes that the varint-encoded length of the decompressed bytes has already |
| 17 |
// been written. |
| 18 |
// |
| 19 |
// It also assumes that: |
| 20 |
// |
| 21 |
// len(dst) >= MaxEncodedLen(len(src)) && |
| 22 |
// minNonLiteralBlockSize <= len(src) && len(src) <= maxBlockSize |
| 23 |
func encodeBlock(dst, src []byte) (d int) { |
| 24 |
race.ReadSlice(src) |
| 25 |
race.WriteSlice(dst) |
| 26 |
|
| 27 |
const ( |
| 28 |
// Use 12 bit table when less than... |
| 29 |
limit12B = 16 << 10 |
| 30 |
// Use 10 bit table when less than... |
| 31 |
limit10B = 4 << 10 |
| 32 |
// Use 8 bit table when less than... |
| 33 |
limit8B = 512 |
| 34 |
) |
| 35 |
|
| 36 |
if len(src) >= 4<<20 { |
| 37 |
const sz, pool = 65536, 0 |
| 38 |
tmp, ok := encPools[pool].Get().(*[sz]byte) |
| 39 |
if !ok { |
| 40 |
tmp = &[sz]byte{} |
| 41 |
} |
| 42 |
race.WriteSlice(tmp[:]) |
| 43 |
defer encPools[pool].Put(tmp) |
| 44 |
return encodeBlockAsm(dst, src, tmp) |
| 45 |
} |
| 46 |
if len(src) >= limit12B { |
| 47 |
const sz, pool = 65536, 0 |
| 48 |
tmp, ok := encPools[pool].Get().(*[sz]byte) |
| 49 |
if !ok { |
| 50 |
tmp = &[sz]byte{} |
| 51 |
} |
| 52 |
race.WriteSlice(tmp[:]) |
| 53 |
defer encPools[pool].Put(tmp) |
| 54 |
return encodeBlockAsm4MB(dst, src, tmp) |
| 55 |
} |
| 56 |
if len(src) >= limit10B { |
| 57 |
const sz, pool = 16384, 1 |
| 58 |
tmp, ok := encPools[pool].Get().(*[sz]byte) |
| 59 |
if !ok { |
| 60 |
tmp = &[sz]byte{} |
| 61 |
} |
| 62 |
race.WriteSlice(tmp[:]) |
| 63 |
defer encPools[pool].Put(tmp) |
| 64 |
return encodeBlockAsm12B(dst, src, tmp) |
| 65 |
} |
| 66 |
if len(src) >= limit8B { |
| 67 |
const sz, pool = 4096, 2 |
| 68 |
tmp, ok := encPools[pool].Get().(*[sz]byte) |
| 69 |
if !ok { |
| 70 |
tmp = &[sz]byte{} |
| 71 |
} |
| 72 |
race.WriteSlice(tmp[:]) |
| 73 |
defer encPools[pool].Put(tmp) |
| 74 |
return encodeBlockAsm10B(dst, src, tmp) |
| 75 |
} |
| 76 |
if len(src) < minNonLiteralBlockSize { |
| 77 |
return 0 |
| 78 |
} |
| 79 |
const sz, pool = 1024, 3 |
| 80 |
tmp, ok := encPools[pool].Get().(*[sz]byte) |
| 81 |
if !ok { |
| 82 |
tmp = &[sz]byte{} |
| 83 |
} |
| 84 |
race.WriteSlice(tmp[:]) |
| 85 |
defer encPools[pool].Put(tmp) |
| 86 |
return encodeBlockAsm8B(dst, src, tmp) |
| 87 |
} |
| 88 |
|
| 89 |
var encBetterPools [5]sync.Pool |
| 90 |
|
| 91 |
// encodeBlockBetter encodes a non-empty src to a guaranteed-large-enough dst. It |
| 92 |
// assumes that the varint-encoded length of the decompressed bytes has already |
| 93 |
// been written. |
| 94 |
// |
| 95 |
// It also assumes that: |
| 96 |
// |
| 97 |
// len(dst) >= MaxEncodedLen(len(src)) && |
| 98 |
// minNonLiteralBlockSize <= len(src) && len(src) <= maxBlockSize |
| 99 |
func encodeBlockBetter(dst, src []byte) (d int) { |
| 100 |
race.ReadSlice(src) |
| 101 |
race.WriteSlice(dst) |
| 102 |
|
| 103 |
const ( |
| 104 |
// Use 12 bit table when less than... |
| 105 |
limit12B = 16 << 10 |
| 106 |
// Use 10 bit table when less than... |
| 107 |
limit10B = 4 << 10 |
| 108 |
// Use 8 bit table when less than... |
| 109 |
limit8B = 512 |
| 110 |
) |
| 111 |
|
| 112 |
if len(src) > 4<<20 { |
| 113 |
const sz, pool = 589824, 0 |
| 114 |
tmp, ok := encBetterPools[pool].Get().(*[sz]byte) |
| 115 |
if !ok { |
| 116 |
tmp = &[sz]byte{} |
| 117 |
} |
| 118 |
race.WriteSlice(tmp[:]) |
| 119 |
defer encBetterPools[pool].Put(tmp) |
| 120 |
return encodeBetterBlockAsm(dst, src, tmp) |
| 121 |
} |
| 122 |
if len(src) >= limit12B { |
| 123 |
const sz, pool = 589824, 0 |
| 124 |
tmp, ok := encBetterPools[pool].Get().(*[sz]byte) |
| 125 |
if !ok { |
| 126 |
tmp = &[sz]byte{} |
| 127 |
} |
| 128 |
race.WriteSlice(tmp[:]) |
| 129 |
defer encBetterPools[pool].Put(tmp) |
| 130 |
|
| 131 |
return encodeBetterBlockAsm4MB(dst, src, tmp) |
| 132 |
} |
| 133 |
if len(src) >= limit10B { |
| 134 |
const sz, pool = 81920, 0 |
| 135 |
tmp, ok := encBetterPools[pool].Get().(*[sz]byte) |
| 136 |
if !ok { |
| 137 |
tmp = &[sz]byte{} |
| 138 |
} |
| 139 |
race.WriteSlice(tmp[:]) |
| 140 |
defer encBetterPools[pool].Put(tmp) |
| 141 |
|
| 142 |
return encodeBetterBlockAsm12B(dst, src, tmp) |
| 143 |
} |
| 144 |
if len(src) >= limit8B { |
| 145 |
const sz, pool = 20480, 1 |
| 146 |
tmp, ok := encBetterPools[pool].Get().(*[sz]byte) |
| 147 |
if !ok { |
| 148 |
tmp = &[sz]byte{} |
| 149 |
} |
| 150 |
race.WriteSlice(tmp[:]) |
| 151 |
defer encBetterPools[pool].Put(tmp) |
| 152 |
return encodeBetterBlockAsm10B(dst, src, tmp) |
| 153 |
} |
| 154 |
if len(src) < minNonLiteralBlockSize { |
| 155 |
return 0 |
| 156 |
} |
| 157 |
|
| 158 |
const sz, pool = 5120, 2 |
| 159 |
tmp, ok := encBetterPools[pool].Get().(*[sz]byte) |
| 160 |
if !ok { |
| 161 |
tmp = &[sz]byte{} |
| 162 |
} |
| 163 |
race.WriteSlice(tmp[:]) |
| 164 |
defer encBetterPools[pool].Put(tmp) |
| 165 |
return encodeBetterBlockAsm8B(dst, src, tmp) |
| 166 |
} |
| 167 |
|
| 168 |
// encodeBlockSnappy encodes a non-empty src to a guaranteed-large-enough dst. It |
| 169 |
// assumes that the varint-encoded length of the decompressed bytes has already |
| 170 |
// been written. |
| 171 |
// |
| 172 |
// It also assumes that: |
| 173 |
// |
| 174 |
// len(dst) >= MaxEncodedLen(len(src)) && |
| 175 |
// minNonLiteralBlockSize <= len(src) && len(src) <= maxBlockSize |
| 176 |
func encodeBlockSnappy(dst, src []byte) (d int) { |
| 177 |
race.ReadSlice(src) |
| 178 |
race.WriteSlice(dst) |
| 179 |
|
| 180 |
const ( |
| 181 |
// Use 12 bit table when less than... |
| 182 |
limit12B = 16 << 10 |
| 183 |
// Use 10 bit table when less than... |
| 184 |
limit10B = 4 << 10 |
| 185 |
// Use 8 bit table when less than... |
| 186 |
limit8B = 512 |
| 187 |
) |
| 188 |
if len(src) > 65536 { |
| 189 |
const sz, pool = 65536, 0 |
| 190 |
tmp, ok := encPools[pool].Get().(*[sz]byte) |
| 191 |
if !ok { |
| 192 |
tmp = &[sz]byte{} |
| 193 |
} |
| 194 |
race.WriteSlice(tmp[:]) |
| 195 |
defer encPools[pool].Put(tmp) |
| 196 |
return encodeSnappyBlockAsm(dst, src, tmp) |
| 197 |
} |
| 198 |
if len(src) >= limit12B { |
| 199 |
const sz, pool = 65536, 0 |
| 200 |
tmp, ok := encPools[pool].Get().(*[sz]byte) |
| 201 |
if !ok { |
| 202 |
tmp = &[sz]byte{} |
| 203 |
} |
| 204 |
race.WriteSlice(tmp[:]) |
| 205 |
defer encPools[pool].Put(tmp) |
| 206 |
return encodeSnappyBlockAsm64K(dst, src, tmp) |
| 207 |
} |
| 208 |
if len(src) >= limit10B { |
| 209 |
const sz, pool = 16384, 1 |
| 210 |
tmp, ok := encPools[pool].Get().(*[sz]byte) |
| 211 |
if !ok { |
| 212 |
tmp = &[sz]byte{} |
| 213 |
} |
| 214 |
race.WriteSlice(tmp[:]) |
| 215 |
defer encPools[pool].Put(tmp) |
| 216 |
return encodeSnappyBlockAsm12B(dst, src, tmp) |
| 217 |
} |
| 218 |
if len(src) >= limit8B { |
| 219 |
const sz, pool = 4096, 2 |
| 220 |
tmp, ok := encPools[pool].Get().(*[sz]byte) |
| 221 |
if !ok { |
| 222 |
tmp = &[sz]byte{} |
| 223 |
} |
| 224 |
race.WriteSlice(tmp[:]) |
| 225 |
defer encPools[pool].Put(tmp) |
| 226 |
return encodeSnappyBlockAsm10B(dst, src, tmp) |
| 227 |
} |
| 228 |
if len(src) < minNonLiteralBlockSize { |
| 229 |
return 0 |
| 230 |
} |
| 231 |
const sz, pool = 1024, 3 |
| 232 |
tmp, ok := encPools[pool].Get().(*[sz]byte) |
| 233 |
if !ok { |
| 234 |
tmp = &[sz]byte{} |
| 235 |
} |
| 236 |
race.WriteSlice(tmp[:]) |
| 237 |
defer encPools[pool].Put(tmp) |
| 238 |
return encodeSnappyBlockAsm8B(dst, src, tmp) |
| 239 |
} |
| 240 |
|
| 241 |
// encodeBlockSnappy encodes a non-empty src to a guaranteed-large-enough dst. It |
| 242 |
// assumes that the varint-encoded length of the decompressed bytes has already |
| 243 |
// been written. |
| 244 |
// |
| 245 |
// It also assumes that: |
| 246 |
// |
| 247 |
// len(dst) >= MaxEncodedLen(len(src)) && |
| 248 |
// minNonLiteralBlockSize <= len(src) && len(src) <= maxBlockSize |
| 249 |
func encodeBlockBetterSnappy(dst, src []byte) (d int) { |
| 250 |
race.ReadSlice(src) |
| 251 |
race.WriteSlice(dst) |
| 252 |
|
| 253 |
const ( |
| 254 |
// Use 12 bit table when less than... |
| 255 |
limit12B = 16 << 10 |
| 256 |
// Use 10 bit table when less than... |
| 257 |
limit10B = 4 << 10 |
| 258 |
// Use 8 bit table when less than... |
| 259 |
limit8B = 512 |
| 260 |
) |
| 261 |
if len(src) > 65536 { |
| 262 |
const sz, pool = 589824, 0 |
| 263 |
tmp, ok := encBetterPools[pool].Get().(*[sz]byte) |
| 264 |
if !ok { |
| 265 |
tmp = &[sz]byte{} |
| 266 |
} |
| 267 |
race.WriteSlice(tmp[:]) |
| 268 |
defer encBetterPools[pool].Put(tmp) |
| 269 |
return encodeSnappyBetterBlockAsm(dst, src, tmp) |
| 270 |
} |
| 271 |
|
| 272 |
if len(src) >= limit12B { |
| 273 |
const sz, pool = 294912, 4 |
| 274 |
tmp, ok := encBetterPools[pool].Get().(*[sz]byte) |
| 275 |
if !ok { |
| 276 |
tmp = &[sz]byte{} |
| 277 |
} |
| 278 |
race.WriteSlice(tmp[:]) |
| 279 |
defer encBetterPools[pool].Put(tmp) |
| 280 |
|
| 281 |
return encodeSnappyBetterBlockAsm64K(dst, src, tmp) |
| 282 |
} |
| 283 |
if len(src) >= limit10B { |
| 284 |
const sz, pool = 81920, 0 |
| 285 |
tmp, ok := encBetterPools[pool].Get().(*[sz]byte) |
| 286 |
if !ok { |
| 287 |
tmp = &[sz]byte{} |
| 288 |
} |
| 289 |
race.WriteSlice(tmp[:]) |
| 290 |
defer encBetterPools[pool].Put(tmp) |
| 291 |
|
| 292 |
return encodeSnappyBetterBlockAsm12B(dst, src, tmp) |
| 293 |
} |
| 294 |
if len(src) >= limit8B { |
| 295 |
const sz, pool = 20480, 1 |
| 296 |
tmp, ok := encBetterPools[pool].Get().(*[sz]byte) |
| 297 |
if !ok { |
| 298 |
tmp = &[sz]byte{} |
| 299 |
} |
| 300 |
race.WriteSlice(tmp[:]) |
| 301 |
defer encBetterPools[pool].Put(tmp) |
| 302 |
return encodeSnappyBetterBlockAsm10B(dst, src, tmp) |
| 303 |
} |
| 304 |
if len(src) < minNonLiteralBlockSize { |
| 305 |
return 0 |
| 306 |
} |
| 307 |
|
| 308 |
const sz, pool = 5120, 2 |
| 309 |
tmp, ok := encBetterPools[pool].Get().(*[sz]byte) |
| 310 |
if !ok { |
| 311 |
tmp = &[sz]byte{} |
| 312 |
} |
| 313 |
race.WriteSlice(tmp[:]) |
| 314 |
defer encBetterPools[pool].Put(tmp) |
| 315 |
return encodeSnappyBetterBlockAsm8B(dst, src, tmp) |
| 316 |
} |