h6.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package brotli
  2. import "encoding/binary"
  3. /* Copyright 2010 Google Inc. All Rights Reserved.
  4. Distributed under MIT license.
  5. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  6. */
  7. /* A (forgetful) hash table to the data seen by the compressor, to
  8. help create backward references to previous data.
  9. This is a hash map of fixed size (bucket_size_) to a ring buffer of
  10. fixed size (block_size_). The ring buffer contains the last block_size_
  11. index positions of the given hash key in the compressed data. */
  12. func (*h6) HashTypeLength() uint {
  13. return 8
  14. }
  15. func (*h6) StoreLookahead() uint {
  16. return 8
  17. }
  18. /* HashBytes is the function that chooses the bucket to place the address in. */
  19. func hashBytesH6(data []byte, mask uint64, shift int) uint32 {
  20. var h uint64 = (binary.LittleEndian.Uint64(data) & mask) * kHashMul64Long
  21. /* The higher bits contain more mixture from the multiplication,
  22. so we take our results from there. */
  23. return uint32(h >> uint(shift))
  24. }
  25. type h6 struct {
  26. hasherCommon
  27. bucket_size_ uint
  28. block_size_ uint
  29. hash_shift_ int
  30. hash_mask_ uint64
  31. block_mask_ uint32
  32. num []uint16
  33. buckets []uint32
  34. }
  35. func (h *h6) Initialize(params *encoderParams) {
  36. h.hash_shift_ = 64 - h.params.bucket_bits
  37. h.hash_mask_ = (^(uint64(0))) >> uint(64-8*h.params.hash_len)
  38. h.bucket_size_ = uint(1) << uint(h.params.bucket_bits)
  39. h.block_size_ = uint(1) << uint(h.params.block_bits)
  40. h.block_mask_ = uint32(h.block_size_ - 1)
  41. h.num = make([]uint16, h.bucket_size_)
  42. h.buckets = make([]uint32, h.block_size_*h.bucket_size_)
  43. }
  44. func (h *h6) Prepare(one_shot bool, input_size uint, data []byte) {
  45. var num []uint16 = h.num
  46. var partial_prepare_threshold uint = h.bucket_size_ >> 6
  47. /* Partial preparation is 100 times slower (per socket). */
  48. if one_shot && input_size <= partial_prepare_threshold {
  49. var i uint
  50. for i = 0; i < input_size; i++ {
  51. var key uint32 = hashBytesH6(data[i:], h.hash_mask_, h.hash_shift_)
  52. num[key] = 0
  53. }
  54. } else {
  55. for i := 0; i < int(h.bucket_size_); i++ {
  56. num[i] = 0
  57. }
  58. }
  59. }
  60. /* Look at 4 bytes at &data[ix & mask].
  61. Compute a hash from these, and store the value of ix at that position. */
  62. func (h *h6) Store(data []byte, mask uint, ix uint) {
  63. var num []uint16 = h.num
  64. var key uint32 = hashBytesH6(data[ix&mask:], h.hash_mask_, h.hash_shift_)
  65. var minor_ix uint = uint(num[key]) & uint(h.block_mask_)
  66. var offset uint = minor_ix + uint(key<<uint(h.params.block_bits))
  67. h.buckets[offset] = uint32(ix)
  68. num[key]++
  69. }
  70. func (h *h6) StoreRange(data []byte, mask uint, ix_start uint, ix_end uint) {
  71. var i uint
  72. for i = ix_start; i < ix_end; i++ {
  73. h.Store(data, mask, i)
  74. }
  75. }
  76. func (h *h6) StitchToPreviousBlock(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint) {
  77. if num_bytes >= h.HashTypeLength()-1 && position >= 3 {
  78. /* Prepare the hashes for three last bytes of the last write.
  79. These could not be calculated before, since they require knowledge
  80. of both the previous and the current block. */
  81. h.Store(ringbuffer, ringbuffer_mask, position-3)
  82. h.Store(ringbuffer, ringbuffer_mask, position-2)
  83. h.Store(ringbuffer, ringbuffer_mask, position-1)
  84. }
  85. }
  86. func (h *h6) PrepareDistanceCache(distance_cache []int) {
  87. prepareDistanceCache(distance_cache, h.params.num_last_distances_to_check)
  88. }
  89. /* Find a longest backward match of &data[cur_ix] up to the length of
  90. max_length and stores the position cur_ix in the hash table.
  91. REQUIRES: PrepareDistanceCacheH6 must be invoked for current distance cache
  92. values; if this method is invoked repeatedly with the same distance
  93. cache values, it is enough to invoke PrepareDistanceCacheH6 once.
  94. Does not look for matches longer than max_length.
  95. Does not look for matches further away than max_backward.
  96. Writes the best match into |out|.
  97. |out|->score is updated only if a better match is found. */
  98. func (h *h6) FindLongestMatch(dictionary *encoderDictionary, data []byte, ring_buffer_mask uint, distance_cache []int, cur_ix uint, max_length uint, max_backward uint, gap uint, max_distance uint, out *hasherSearchResult) {
  99. var num []uint16 = h.num
  100. var buckets []uint32 = h.buckets
  101. var cur_ix_masked uint = cur_ix & ring_buffer_mask
  102. var min_score uint = out.score
  103. var best_score uint = out.score
  104. var best_len uint = out.len
  105. var i uint
  106. var bucket []uint32
  107. /* Don't accept a short copy from far away. */
  108. out.len = 0
  109. out.len_code_delta = 0
  110. /* Try last distance first. */
  111. for i = 0; i < uint(h.params.num_last_distances_to_check); i++ {
  112. var backward uint = uint(distance_cache[i])
  113. var prev_ix uint = uint(cur_ix - backward)
  114. if prev_ix >= cur_ix {
  115. continue
  116. }
  117. if backward > max_backward {
  118. continue
  119. }
  120. prev_ix &= ring_buffer_mask
  121. if cur_ix_masked+best_len > ring_buffer_mask || prev_ix+best_len > ring_buffer_mask || data[cur_ix_masked+best_len] != data[prev_ix+best_len] {
  122. continue
  123. }
  124. {
  125. var len uint = findMatchLengthWithLimit(data[prev_ix:], data[cur_ix_masked:], max_length)
  126. if len >= 3 || (len == 2 && i < 2) {
  127. /* Comparing for >= 2 does not change the semantics, but just saves for
  128. a few unnecessary binary logarithms in backward reference score,
  129. since we are not interested in such short matches. */
  130. var score uint = backwardReferenceScoreUsingLastDistance(uint(len))
  131. if best_score < score {
  132. if i != 0 {
  133. score -= backwardReferencePenaltyUsingLastDistance(i)
  134. }
  135. if best_score < score {
  136. best_score = score
  137. best_len = uint(len)
  138. out.len = best_len
  139. out.distance = backward
  140. out.score = best_score
  141. }
  142. }
  143. }
  144. }
  145. }
  146. {
  147. var key uint32 = hashBytesH6(data[cur_ix_masked:], h.hash_mask_, h.hash_shift_)
  148. bucket = buckets[key<<uint(h.params.block_bits):]
  149. var down uint
  150. if uint(num[key]) > h.block_size_ {
  151. down = uint(num[key]) - h.block_size_
  152. } else {
  153. down = 0
  154. }
  155. for i = uint(num[key]); i > down; {
  156. var prev_ix uint
  157. i--
  158. prev_ix = uint(bucket[uint32(i)&h.block_mask_])
  159. var backward uint = cur_ix - prev_ix
  160. if backward > max_backward {
  161. break
  162. }
  163. prev_ix &= ring_buffer_mask
  164. if cur_ix_masked+best_len > ring_buffer_mask || prev_ix+best_len > ring_buffer_mask || data[cur_ix_masked+best_len] != data[prev_ix+best_len] {
  165. continue
  166. }
  167. {
  168. var len uint = findMatchLengthWithLimit(data[prev_ix:], data[cur_ix_masked:], max_length)
  169. if len >= 4 {
  170. /* Comparing for >= 3 does not change the semantics, but just saves
  171. for a few unnecessary binary logarithms in backward reference
  172. score, since we are not interested in such short matches. */
  173. var score uint = backwardReferenceScore(uint(len), backward)
  174. if best_score < score {
  175. best_score = score
  176. best_len = uint(len)
  177. out.len = best_len
  178. out.distance = backward
  179. out.score = best_score
  180. }
  181. }
  182. }
  183. }
  184. bucket[uint32(num[key])&h.block_mask_] = uint32(cur_ix)
  185. num[key]++
  186. }
  187. if min_score == out.score {
  188. searchInStaticDictionary(dictionary, h, data[cur_ix_masked:], max_length, max_backward+gap, max_distance, out, false)
  189. }
  190. }