backward_references.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package brotli
  2. import (
  3. "sync"
  4. )
  5. /* Copyright 2013 Google Inc. All Rights Reserved.
  6. Distributed under MIT license.
  7. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  8. */
  9. /* Function to find backward reference copies. */
  10. func computeDistanceCode(distance uint, max_distance uint, dist_cache []int) uint {
  11. if distance <= max_distance {
  12. var distance_plus_3 uint = distance + 3
  13. var offset0 uint = distance_plus_3 - uint(dist_cache[0])
  14. var offset1 uint = distance_plus_3 - uint(dist_cache[1])
  15. if distance == uint(dist_cache[0]) {
  16. return 0
  17. } else if distance == uint(dist_cache[1]) {
  18. return 1
  19. } else if offset0 < 7 {
  20. return (0x9750468 >> (4 * offset0)) & 0xF
  21. } else if offset1 < 7 {
  22. return (0xFDB1ACE >> (4 * offset1)) & 0xF
  23. } else if distance == uint(dist_cache[2]) {
  24. return 2
  25. } else if distance == uint(dist_cache[3]) {
  26. return 3
  27. }
  28. }
  29. return distance + numDistanceShortCodes - 1
  30. }
  31. var hasherSearchResultPool sync.Pool
  32. func createBackwardReferences(num_bytes uint, position uint, ringbuffer []byte, ringbuffer_mask uint, params *encoderParams, hasher hasherHandle, dist_cache []int, last_insert_len *uint, commands *[]command, num_literals *uint) {
  33. var max_backward_limit uint = maxBackwardLimit(params.lgwin)
  34. var insert_length uint = *last_insert_len
  35. var pos_end uint = position + num_bytes
  36. var store_end uint
  37. if num_bytes >= hasher.StoreLookahead() {
  38. store_end = position + num_bytes - hasher.StoreLookahead() + 1
  39. } else {
  40. store_end = position
  41. }
  42. var random_heuristics_window_size uint = literalSpreeLengthForSparseSearch(params)
  43. var apply_random_heuristics uint = position + random_heuristics_window_size
  44. var gap uint = 0
  45. /* Set maximum distance, see section 9.1. of the spec. */
  46. const kMinScore uint = scoreBase + 100
  47. /* For speed up heuristics for random data. */
  48. /* Minimum score to accept a backward reference. */
  49. hasher.PrepareDistanceCache(dist_cache)
  50. sr2, _ := hasherSearchResultPool.Get().(*hasherSearchResult)
  51. if sr2 == nil {
  52. sr2 = &hasherSearchResult{}
  53. }
  54. sr, _ := hasherSearchResultPool.Get().(*hasherSearchResult)
  55. if sr == nil {
  56. sr = &hasherSearchResult{}
  57. }
  58. for position+hasher.HashTypeLength() < pos_end {
  59. var max_length uint = pos_end - position
  60. var max_distance uint = brotli_min_size_t(position, max_backward_limit)
  61. sr.len = 0
  62. sr.len_code_delta = 0
  63. sr.distance = 0
  64. sr.score = kMinScore
  65. hasher.FindLongestMatch(&params.dictionary, ringbuffer, ringbuffer_mask, dist_cache, position, max_length, max_distance, gap, params.dist.max_distance, sr)
  66. if sr.score > kMinScore {
  67. /* Found a match. Let's look for something even better ahead. */
  68. var delayed_backward_references_in_row int = 0
  69. max_length--
  70. for ; ; max_length-- {
  71. var cost_diff_lazy uint = 175
  72. if params.quality < minQualityForExtensiveReferenceSearch {
  73. sr2.len = brotli_min_size_t(sr.len-1, max_length)
  74. } else {
  75. sr2.len = 0
  76. }
  77. sr2.len_code_delta = 0
  78. sr2.distance = 0
  79. sr2.score = kMinScore
  80. max_distance = brotli_min_size_t(position+1, max_backward_limit)
  81. hasher.FindLongestMatch(&params.dictionary, ringbuffer, ringbuffer_mask, dist_cache, position+1, max_length, max_distance, gap, params.dist.max_distance, sr2)
  82. if sr2.score >= sr.score+cost_diff_lazy {
  83. /* Ok, let's just write one byte for now and start a match from the
  84. next byte. */
  85. position++
  86. insert_length++
  87. *sr = *sr2
  88. delayed_backward_references_in_row++
  89. if delayed_backward_references_in_row < 4 && position+hasher.HashTypeLength() < pos_end {
  90. continue
  91. }
  92. }
  93. break
  94. }
  95. apply_random_heuristics = position + 2*sr.len + random_heuristics_window_size
  96. max_distance = brotli_min_size_t(position, max_backward_limit)
  97. {
  98. /* The first 16 codes are special short-codes,
  99. and the minimum offset is 1. */
  100. var distance_code uint = computeDistanceCode(sr.distance, max_distance+gap, dist_cache)
  101. if (sr.distance <= (max_distance + gap)) && distance_code > 0 {
  102. dist_cache[3] = dist_cache[2]
  103. dist_cache[2] = dist_cache[1]
  104. dist_cache[1] = dist_cache[0]
  105. dist_cache[0] = int(sr.distance)
  106. hasher.PrepareDistanceCache(dist_cache)
  107. }
  108. *commands = append(*commands, makeCommand(&params.dist, insert_length, sr.len, sr.len_code_delta, distance_code))
  109. }
  110. *num_literals += insert_length
  111. insert_length = 0
  112. /* Put the hash keys into the table, if there are enough bytes left.
  113. Depending on the hasher implementation, it can push all positions
  114. in the given range or only a subset of them.
  115. Avoid hash poisoning with RLE data. */
  116. {
  117. var range_start uint = position + 2
  118. var range_end uint = brotli_min_size_t(position+sr.len, store_end)
  119. if sr.distance < sr.len>>2 {
  120. range_start = brotli_min_size_t(range_end, brotli_max_size_t(range_start, position+sr.len-(sr.distance<<2)))
  121. }
  122. hasher.StoreRange(ringbuffer, ringbuffer_mask, range_start, range_end)
  123. }
  124. position += sr.len
  125. } else {
  126. insert_length++
  127. position++
  128. /* If we have not seen matches for a long time, we can skip some
  129. match lookups. Unsuccessful match lookups are very very expensive
  130. and this kind of a heuristic speeds up compression quite
  131. a lot. */
  132. if position > apply_random_heuristics {
  133. /* Going through uncompressible data, jump. */
  134. if position > apply_random_heuristics+4*random_heuristics_window_size {
  135. var kMargin uint = brotli_max_size_t(hasher.StoreLookahead()-1, 4)
  136. /* It is quite a long time since we saw a copy, so we assume
  137. that this data is not compressible, and store hashes less
  138. often. Hashes of non compressible data are less likely to
  139. turn out to be useful in the future, too, so we store less of
  140. them to not to flood out the hash table of good compressible
  141. data. */
  142. var pos_jump uint = brotli_min_size_t(position+16, pos_end-kMargin)
  143. for ; position < pos_jump; position += 4 {
  144. hasher.Store(ringbuffer, ringbuffer_mask, position)
  145. insert_length += 4
  146. }
  147. } else {
  148. var kMargin uint = brotli_max_size_t(hasher.StoreLookahead()-1, 2)
  149. var pos_jump uint = brotli_min_size_t(position+8, pos_end-kMargin)
  150. for ; position < pos_jump; position += 2 {
  151. hasher.Store(ringbuffer, ringbuffer_mask, position)
  152. insert_length += 2
  153. }
  154. }
  155. }
  156. }
  157. }
  158. insert_length += pos_end - position
  159. *last_insert_len = insert_length
  160. hasherSearchResultPool.Put(sr)
  161. hasherSearchResultPool.Put(sr2)
  162. }