本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
8.0KB

  1. // Copyright 2016 The Snappy-Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build !amd64 appengine !gc noasm
  5. package snappy
  6. func load32(b []byte, i int) uint32 {
  7. b = b[i : i+4 : len(b)] // Help the compiler eliminate bounds checks on the next line.
  8. return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
  9. }
  10. func load64(b []byte, i int) uint64 {
  11. b = b[i : i+8 : len(b)] // Help the compiler eliminate bounds checks on the next line.
  12. return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
  13. uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
  14. }
  15. // emitLiteral writes a literal chunk and returns the number of bytes written.
  16. //
  17. // It assumes that:
  18. // dst is long enough to hold the encoded bytes
  19. // 1 <= len(lit) && len(lit) <= 65536
  20. func emitLiteral(dst, lit []byte) int {
  21. i, n := 0, uint(len(lit)-1)
  22. switch {
  23. case n < 60:
  24. dst[0] = uint8(n)<<2 | tagLiteral
  25. i = 1
  26. case n < 1<<8:
  27. dst[0] = 60<<2 | tagLiteral
  28. dst[1] = uint8(n)
  29. i = 2
  30. default:
  31. dst[0] = 61<<2 | tagLiteral
  32. dst[1] = uint8(n)
  33. dst[2] = uint8(n >> 8)
  34. i = 3
  35. }
  36. return i + copy(dst[i:], lit)
  37. }
  38. // emitCopy writes a copy chunk and returns the number of bytes written.
  39. //
  40. // It assumes that:
  41. // dst is long enough to hold the encoded bytes
  42. // 1 <= offset && offset <= 65535
  43. // 4 <= length && length <= 65535
  44. func emitCopy(dst []byte, offset, length int) int {
  45. i := 0
  46. // The maximum length for a single tagCopy1 or tagCopy2 op is 64 bytes. The
  47. // threshold for this loop is a little higher (at 68 = 64 + 4), and the
  48. // length emitted down below is is a little lower (at 60 = 64 - 4), because
  49. // it's shorter to encode a length 67 copy as a length 60 tagCopy2 followed
  50. // by a length 7 tagCopy1 (which encodes as 3+2 bytes) than to encode it as
  51. // a length 64 tagCopy2 followed by a length 3 tagCopy2 (which encodes as
  52. // 3+3 bytes). The magic 4 in the 64±4 is because the minimum length for a
  53. // tagCopy1 op is 4 bytes, which is why a length 3 copy has to be an
  54. // encodes-as-3-bytes tagCopy2 instead of an encodes-as-2-bytes tagCopy1.
  55. for length >= 68 {
  56. // Emit a length 64 copy, encoded as 3 bytes.
  57. dst[i+0] = 63<<2 | tagCopy2
  58. dst[i+1] = uint8(offset)
  59. dst[i+2] = uint8(offset >> 8)
  60. i += 3
  61. length -= 64
  62. }
  63. if length > 64 {
  64. // Emit a length 60 copy, encoded as 3 bytes.
  65. dst[i+0] = 59<<2 | tagCopy2
  66. dst[i+1] = uint8(offset)
  67. dst[i+2] = uint8(offset >> 8)
  68. i += 3
  69. length -= 60
  70. }
  71. if length >= 12 || offset >= 2048 {
  72. // Emit the remaining copy, encoded as 3 bytes.
  73. dst[i+0] = uint8(length-1)<<2 | tagCopy2
  74. dst[i+1] = uint8(offset)
  75. dst[i+2] = uint8(offset >> 8)
  76. return i + 3
  77. }
  78. // Emit the remaining copy, encoded as 2 bytes.
  79. dst[i+0] = uint8(offset>>8)<<5 | uint8(length-4)<<2 | tagCopy1
  80. dst[i+1] = uint8(offset)
  81. return i + 2
  82. }
  83. // extendMatch returns the largest k such that k <= len(src) and that
  84. // src[i:i+k-j] and src[j:k] have the same contents.
  85. //
  86. // It assumes that:
  87. // 0 <= i && i < j && j <= len(src)
  88. func extendMatch(src []byte, i, j int) int {
  89. for ; j < len(src) && src[i] == src[j]; i, j = i+1, j+1 {
  90. }
  91. return j
  92. }
  93. func hash(u, shift uint32) uint32 {
  94. return (u * 0x1e35a7bd) >> shift
  95. }
  96. // encodeBlock encodes a non-empty src to a guaranteed-large-enough dst. It
  97. // assumes that the varint-encoded length of the decompressed bytes has already
  98. // been written.
  99. //
  100. // It also assumes that:
  101. // len(dst) >= MaxEncodedLen(len(src)) &&
  102. // minNonLiteralBlockSize <= len(src) && len(src) <= maxBlockSize
  103. func encodeBlock(dst, src []byte) (d int) {
  104. // Initialize the hash table. Its size ranges from 1<<8 to 1<<14 inclusive.
  105. // The table element type is uint16, as s < sLimit and sLimit < len(src)
  106. // and len(src) <= maxBlockSize and maxBlockSize == 65536.
  107. const (
  108. maxTableSize = 1 << 14
  109. // tableMask is redundant, but helps the compiler eliminate bounds
  110. // checks.
  111. tableMask = maxTableSize - 1
  112. )
  113. shift := uint32(32 - 8)
  114. for tableSize := 1 << 8; tableSize < maxTableSize && tableSize < len(src); tableSize *= 2 {
  115. shift--
  116. }
  117. // In Go, all array elements are zero-initialized, so there is no advantage
  118. // to a smaller tableSize per se. However, it matches the C++ algorithm,
  119. // and in the asm versions of this code, we can get away with zeroing only
  120. // the first tableSize elements.
  121. var table [maxTableSize]uint16
  122. // sLimit is when to stop looking for offset/length copies. The inputMargin
  123. // lets us use a fast path for emitLiteral in the main loop, while we are
  124. // looking for copies.
  125. sLimit := len(src) - inputMargin
  126. // nextEmit is where in src the next emitLiteral should start from.
  127. nextEmit := 0
  128. // The encoded form must start with a literal, as there are no previous
  129. // bytes to copy, so we start looking for hash matches at s == 1.
  130. s := 1
  131. nextHash := hash(load32(src, s), shift)
  132. for {
  133. // Copied from the C++ snappy implementation:
  134. //
  135. // Heuristic match skipping: If 32 bytes are scanned with no matches
  136. // found, start looking only at every other byte. If 32 more bytes are
  137. // scanned (or skipped), look at every third byte, etc.. When a match
  138. // is found, immediately go back to looking at every byte. This is a
  139. // small loss (~5% performance, ~0.1% density) for compressible data
  140. // due to more bookkeeping, but for non-compressible data (such as
  141. // JPEG) it's a huge win since the compressor quickly "realizes" the
  142. // data is incompressible and doesn't bother looking for matches
  143. // everywhere.
  144. //
  145. // The "skip" variable keeps track of how many bytes there are since
  146. // the last match; dividing it by 32 (ie. right-shifting by five) gives
  147. // the number of bytes to move ahead for each iteration.
  148. skip := 32
  149. nextS := s
  150. candidate := 0
  151. for {
  152. s = nextS
  153. bytesBetweenHashLookups := skip >> 5
  154. nextS = s + bytesBetweenHashLookups
  155. skip += bytesBetweenHashLookups
  156. if nextS > sLimit {
  157. goto emitRemainder
  158. }
  159. candidate = int(table[nextHash&tableMask])
  160. table[nextHash&tableMask] = uint16(s)
  161. nextHash = hash(load32(src, nextS), shift)
  162. if load32(src, s) == load32(src, candidate) {
  163. break
  164. }
  165. }
  166. // A 4-byte match has been found. We'll later see if more than 4 bytes
  167. // match. But, prior to the match, src[nextEmit:s] are unmatched. Emit
  168. // them as literal bytes.
  169. d += emitLiteral(dst[d:], src[nextEmit:s])
  170. // Call emitCopy, and then see if another emitCopy could be our next
  171. // move. Repeat until we find no match for the input immediately after
  172. // what was consumed by the last emitCopy call.
  173. //
  174. // If we exit this loop normally then we need to call emitLiteral next,
  175. // though we don't yet know how big the literal will be. We handle that
  176. // by proceeding to the next iteration of the main loop. We also can
  177. // exit this loop via goto if we get close to exhausting the input.
  178. for {
  179. // Invariant: we have a 4-byte match at s, and no need to emit any
  180. // literal bytes prior to s.
  181. base := s
  182. // Extend the 4-byte match as long as possible.
  183. //
  184. // This is an inlined version of:
  185. // s = extendMatch(src, candidate+4, s+4)
  186. s += 4
  187. for i := candidate + 4; s < len(src) && src[i] == src[s]; i, s = i+1, s+1 {
  188. }
  189. d += emitCopy(dst[d:], base-candidate, s-base)
  190. nextEmit = s
  191. if s >= sLimit {
  192. goto emitRemainder
  193. }
  194. // We could immediately start working at s now, but to improve
  195. // compression we first update the hash table at s-1 and at s. If
  196. // another emitCopy is not our next move, also calculate nextHash
  197. // at s+1. At least on GOARCH=amd64, these three hash calculations
  198. // are faster as one load64 call (with some shifts) instead of
  199. // three load32 calls.
  200. x := load64(src, s-1)
  201. prevHash := hash(uint32(x>>0), shift)
  202. table[prevHash&tableMask] = uint16(s - 1)
  203. currHash := hash(uint32(x>>8), shift)
  204. candidate = int(table[currHash&tableMask])
  205. table[currHash&tableMask] = uint16(s)
  206. if uint32(x>>8) != load32(src, candidate) {
  207. nextHash = hash(uint32(x>>16), shift)
  208. s++
  209. break
  210. }
  211. }
  212. }
  213. emitRemainder:
  214. if nextEmit < len(src) {
  215. d += emitLiteral(dst[d:], src[nextEmit:])
  216. }
  217. return d
  218. }
上海开阖软件有限公司 沪ICP备12045867号-1