- if (num_matches) {
- /*
- * If there was a very long match found, then don't
- * cache any matches for the bytes covered by that
- * match. This avoids degenerate behavior when
- * compressing highly redundant data, where the number
- * of matches can be very large.
- *
- * This heuristic doesn't actually hurt the compression
- * ratio very much. If there's a long match, then the
- * data must be highly compressible, so it doesn't
- * matter as much what we do.
- */
- unsigned best_len = cache_ptr[-2].length;
- if (best_len >= c->nice_match_length) {
- --best_len;
- do {
- bt_matchfinder_skip_position(&c->bt_mf,
- in_base,
- in_next,
- in_end,
- min(in_end - in_next,
- c->nice_match_length),
- c->max_search_depth,
- &prev_hash);
-
- cache_ptr->length = 0;
- cache_ptr->offset = *in_next++;
- cache_ptr++;
- } while (--best_len);
- }
+ /*
+ * If there was a very long match found, then don't cache any
+ * matches for the bytes covered by that match. This avoids
+ * degenerate behavior when compressing highly redundant data,
+ * where the number of matches can be very large.
+ *
+ * This heuristic doesn't actually hurt the compression ratio
+ * very much. If there's a long match, then the data must be
+ * highly compressible, so it doesn't matter as much what we do.
+ */
+ if (best_len >= nice_len) {
+ if (unlikely(best_len +
+ BT_MATCHFINDER_REQUIRED_NBYTES >= max_len))
+ break;
+ --best_len;
+ do {
+ bt_matchfinder_skip_position(&c->bt_mf,
+ in_begin,
+ in_next - in_begin,
+ nice_len,
+ c->max_search_depth,
+ next_hashes);
+ cache_ptr->length = 0;
+ cache_ptr->offset = *in_next++;
+ cache_ptr++;
+ max_len--;
+ nice_len = min(nice_len, max_len);
+ } while (--best_len);