h5.go 6.8 KB

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