encodeBlockGo64K is a specialized version for compressing blocks <= 64KB
(dst, src []byte)
| 285 | |
| 286 | // encodeBlockGo64K is a specialized version for compressing blocks <= 64KB |
| 287 | func encodeBlockGo64K(dst, src []byte) (d int) { |
| 288 | // Initialize the hash table. |
| 289 | const ( |
| 290 | tableBits = 14 |
| 291 | maxTableSize = 1 << tableBits |
| 292 | |
| 293 | debug = false |
| 294 | ) |
| 295 | |
| 296 | var table [maxTableSize]uint16 |
| 297 | |
| 298 | // sLimit is when to stop looking for offset/length copies. The inputMargin |
| 299 | // lets us use a fast path for emitLiteral in the main loop, while we are |
| 300 | // looking for copies. |
| 301 | sLimit := len(src) - inputMargin |
| 302 | |
| 303 | // Bail if we can't compress to at least this. |
| 304 | dstLimit := len(src) - len(src)>>5 - 5 |
| 305 | |
| 306 | // nextEmit is where in src the next emitLiteral should start from. |
| 307 | nextEmit := 0 |
| 308 | |
| 309 | // The encoded form must start with a literal, as there are no previous |
| 310 | // bytes to copy, so we start looking for hash matches at s == 1. |
| 311 | s := 1 |
| 312 | cv := load64(src, s) |
| 313 | |
| 314 | // We search for a repeat at -1, but don't output repeats when nextEmit == 0 |
| 315 | repeat := 1 |
| 316 | |
| 317 | for { |
| 318 | candidate := 0 |
| 319 | for { |
| 320 | // Next src position to check |
| 321 | nextS := s + (s-nextEmit)>>5 + 4 |
| 322 | if nextS > sLimit { |
| 323 | goto emitRemainder |
| 324 | } |
| 325 | hash0 := hash6(cv, tableBits) |
| 326 | hash1 := hash6(cv>>8, tableBits) |
| 327 | candidate = int(table[hash0]) |
| 328 | candidate2 := int(table[hash1]) |
| 329 | table[hash0] = uint16(s) |
| 330 | table[hash1] = uint16(s + 1) |
| 331 | hash2 := hash6(cv>>16, tableBits) |
| 332 | |
| 333 | // Check repeat at offset checkRep. |
| 334 | const checkRep = 1 |
| 335 | if uint32(cv>>(checkRep*8)) == load32(src, s-repeat+checkRep) { |
| 336 | base := s + checkRep |
| 337 | // Extend back |
| 338 | for i := base - repeat; base > nextEmit && i > 0 && src[i-1] == src[base-1]; { |
| 339 | i-- |
| 340 | base-- |
| 341 | } |
| 342 | |
| 343 | // Bail if we exceed the maximum size. |
| 344 | if d+(base-nextEmit) > dstLimit { |
searching dependent graphs…