summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYann Collet <cyan@fb.com>2017-10-20 18:32:15 (GMT)
committerYann Collet <cyan@fb.com>2017-10-20 18:32:15 (GMT)
commitfc879fe170d1644f8fd93017d53e44bc2b3cde04 (patch)
tree60d2306e4f657f4cef217af0b2164ff546985c73
parentc058753393063b4ec72de996b103327710f2c36b (diff)
downloadlz4-fc879fe170d1644f8fd93017d53e44bc2b3cde04.zip
lz4-fc879fe170d1644f8fd93017d53e44bc2b3cde04.tar.gz
lz4-fc879fe170d1644f8fd93017d53e44bc2b3cde04.tar.bz2
lz4opt: refactor sequence reverse traversal
-rw-r--r--lib/lz4opt.h30
1 files changed, 20 insertions, 10 deletions
diff --git a/lib/lz4opt.h b/lib/lz4opt.h
index c7c95a8..7d487a7 100644
--- a/lib/lz4opt.h
+++ b/lib/lz4opt.h
@@ -334,17 +334,25 @@ static int LZ4HC_compress_optimal (
cur = last_match_pos - best_mlen;
encode: /* cur, last_match_pos, best_mlen, best_off must be set */
+ assert(cur < LZ4_OPT_NUM);
+ assert(last_match_pos >= 1); /* == 1 when only one candidate */
opt[0].mlen = 1;
- while (1) { /* from end to beginning */
- size_t const ml = opt[cur].mlen;
- int const offset = opt[cur].off;
- opt[cur].mlen = (int)best_mlen;
- opt[cur].off = (int)best_off;
- best_mlen = ml;
- best_off = offset;
- if (ml > cur) break; /* can this happen ? */
- cur -= ml;
- }
+ DEBUGLOG(6, "sequence reverse traversal");
+ { int candidate_pos = (int)cur;
+ int selected_matchLength = (int)best_mlen;
+ int selected_offset = (int)best_off;
+ while (1) { /* from end to beginning */
+ int const next_matchLength = opt[candidate_pos].mlen;
+ int const next_offset = opt[candidate_pos].off;
+ assert(next_matchLength > 0); /* note : can be 1, means literal */
+ opt[candidate_pos].mlen = selected_matchLength;
+ opt[candidate_pos].off = selected_offset;
+ DEBUGLOG(6, "rPos:%3i, matchLength:%3i", candidate_pos, selected_matchLength);
+ selected_matchLength = next_matchLength;
+ selected_offset = next_offset;
+ if (next_matchLength > candidate_pos) break; /* last match elected, first match to encode */
+ candidate_pos -= next_matchLength;
+ } }
/* encode all recorded sequences */
cur = 0;
@@ -352,7 +360,9 @@ encode: /* cur, last_match_pos, best_mlen, best_off must be set */
int const ml = opt[cur].mlen;
int const offset = opt[cur].off;
if (ml == 1) { ip++; cur++; continue; }
+ assert(ml >= MINMATCH);
cur += ml;
+ assert((offset >= 1) && (offset <=65535));
if ( LZ4HC_encodeSequence(&ip, &op, &anchor, ml, ip - offset, limit, oend) ) return 0;
}
} /* while (ip < mflimit) */