EncodeBetter 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. EncodeBetter compresses better than Encode but typically with a 10-40% speed decrease on both co
(dst, src []byte)
| 115 | // If you need to encode larger amounts of data, consider using |
| 116 | // the streaming interface which gives all of these features. |
| 117 | func EncodeBetter(dst, src []byte) []byte { |
| 118 | if n := MaxEncodedLen(len(src)); n < 0 { |
| 119 | panic(ErrTooLarge) |
| 120 | } else if cap(dst) < n { |
| 121 | dst = make([]byte, n) |
| 122 | } else { |
| 123 | dst = dst[:n] |
| 124 | } |
| 125 | |
| 126 | // The block starts with the varint-encoded length of the decompressed bytes. |
| 127 | d := binary.PutUvarint(dst, uint64(len(src))) |
| 128 | |
| 129 | if len(src) == 0 { |
| 130 | return dst[:d] |
| 131 | } |
| 132 | if len(src) < minNonLiteralBlockSize { |
| 133 | d += emitLiteral(dst[d:], src) |
| 134 | return dst[:d] |
| 135 | } |
| 136 | n := encodeBlockBetter(dst[d:], src) |
| 137 | if n > 0 { |
| 138 | d += n |
| 139 | return dst[:d] |
| 140 | } |
| 141 | // Not compressible |
| 142 | d += emitLiteral(dst[d:], src) |
| 143 | return dst[:d] |
| 144 | } |
| 145 | |
| 146 | // EncodeBest returns the encoded form of src. The returned slice may be a sub- |
| 147 | // slice of dst if dst was large enough to hold the entire encoded block. |
searching dependent graphs…