Encode improves compression...
(blk *blockEnc, src []byte)
| 54 | |
| 55 | // Encode improves compression... |
| 56 | func (e *betterFastEncoder) Encode(blk *blockEnc, src []byte) { |
| 57 | const ( |
| 58 | // Input margin is the number of bytes we read (8) |
| 59 | // and the maximum we will read ahead (2) |
| 60 | inputMargin = 8 + 2 |
| 61 | minNonLiteralBlockSize = 16 |
| 62 | ) |
| 63 | |
| 64 | // Protect against e.cur wraparound. |
| 65 | for e.cur >= e.bufferReset-int32(len(e.hist)) { |
| 66 | if len(e.hist) == 0 { |
| 67 | e.table = [betterShortTableSize]tableEntry{} |
| 68 | e.longTable = [betterLongTableSize]prevEntry{} |
| 69 | e.cur = e.maxMatchOff |
| 70 | break |
| 71 | } |
| 72 | // Shift down everything in the table that isn't already too far away. |
| 73 | minOff := e.cur + int32(len(e.hist)) - e.maxMatchOff |
| 74 | for i := range e.table[:] { |
| 75 | v := e.table[i].offset |
| 76 | if v < minOff { |
| 77 | v = 0 |
| 78 | } else { |
| 79 | v = v - e.cur + e.maxMatchOff |
| 80 | } |
| 81 | e.table[i].offset = v |
| 82 | } |
| 83 | for i := range e.longTable[:] { |
| 84 | v := e.longTable[i].offset |
| 85 | v2 := e.longTable[i].prev |
| 86 | if v < minOff { |
| 87 | v = 0 |
| 88 | v2 = 0 |
| 89 | } else { |
| 90 | v = v - e.cur + e.maxMatchOff |
| 91 | if v2 < minOff { |
| 92 | v2 = 0 |
| 93 | } else { |
| 94 | v2 = v2 - e.cur + e.maxMatchOff |
| 95 | } |
| 96 | } |
| 97 | e.longTable[i] = prevEntry{ |
| 98 | offset: v, |
| 99 | prev: v2, |
| 100 | } |
| 101 | } |
| 102 | e.cur = e.maxMatchOff |
| 103 | break |
| 104 | } |
| 105 | // Add block to history |
| 106 | s := e.addBlock(src) |
| 107 | blk.size = len(src) |
| 108 | |
| 109 | // Check RLE first |
| 110 | if len(src) > zstdMinMatch { |
| 111 | ml := matchLen(src[1:], src) |
| 112 | if ml == len(src)-1 { |
| 113 | blk.literals = append(blk.literals, src[0]) |