]> wimlib.net Git - wimlib/blobdiff - src/lzx-compress.c
read_wim_header(): Check return value of lseek()
[wimlib] / src / lzx-compress.c
index 7510457488b5025be14faf52ce6c8210e1410e49..7e811e4ea53a3504ce016617140acfbab2e40180 100644 (file)
  * that position at previous positions in the window.  With LZX, the minimum
  * match length is 2 and the maximum match length is 257.  The only restriction
  * on offsets is that LZX does not allow the last 2 bytes of the window to match
- * the the beginning of the window.
+ * the beginning of the window.
  *
  * There are a number of algorithms that can be used for this, including hash
  * chains, binary trees, and suffix arrays.  Binary trees generally work well
@@ -439,7 +439,19 @@ struct lzx_mc_pos_data {
        };
 
        /* Adaptive state that exists after an approximate minimum-cost path to
-        * reach this position is taken.  */
+        * reach this position is taken.
+        *
+        * Note: we update this whenever we update the pending minimum-cost
+        * path.  This is in contrast to LZMA, which also has an optimal parser
+        * that maintains a repeat offset queue per position, but will only
+        * compute the queue once that position is actually reached in the
+        * parse, meaning that matches are being considered *starting* at that
+        * position.  However, the two methods seem to have approximately the
+        * same performance if appropriate optimizations are used.  Intuitively
+        * the LZMA method seems faster, but it actually suffers from 1-2 extra
+        * hard-to-predict branches at each position.  Probably it works better
+        * for LZMA than LZX because LZMA has a larger adaptive state than LZX,
+        * and the LZMA encoder considers more possibilities.  */
        struct lzx_lru_queue queue;
 };
 
@@ -1059,46 +1071,27 @@ lzx_literal_cost(u8 c, const struct lzx_costs * costs)
        return costs->main[c];
 }
 
-/* Given a (length, offset) pair that could be turned into a valid LZX match as
- * well as costs for the codewords in the main, length, and aligned Huffman
- * codes, return the approximate number of bits it will take to represent this
- * match in the compressed output.  Take into account the match offset LRU
- * queue and also updates it.  */
+/* Returns the cost, in bits, to output a repeat offset match of the specified
+ * length and position slot (repeat index) using the specified cost model.  */
 static u32
-lzx_match_cost(unsigned length, u32 offset, const struct lzx_costs *costs,
-              struct lzx_lru_queue *queue)
+lzx_repmatch_cost(u32 len, unsigned position_slot, const struct lzx_costs *costs)
 {
-       unsigned position_slot;
        unsigned len_header, main_symbol;
-       unsigned num_extra_bits;
        u32 cost = 0;
 
-       position_slot = lzx_get_position_slot(offset, queue);
-
-       len_header = min(length - LZX_MIN_MATCH_LEN, LZX_NUM_PRIMARY_LENS);
+       len_header = min(len - LZX_MIN_MATCH_LEN, LZX_NUM_PRIMARY_LENS);
        main_symbol = ((position_slot << 3) | len_header) + LZX_NUM_CHARS;
 
        /* Account for main symbol.  */
        cost += costs->main[main_symbol];
 
-       /* Account for extra position information.  */
-       num_extra_bits = lzx_get_num_extra_bits(position_slot);
-       if (num_extra_bits >= 3) {
-               cost += num_extra_bits - 3;
-               cost += costs->aligned[(offset + LZX_OFFSET_OFFSET) & 7];
-       } else {
-               cost += num_extra_bits;
-       }
-
        /* Account for extra length information.  */
        if (len_header == LZX_NUM_PRIMARY_LENS)
-               cost += costs->len[length - LZX_MIN_MATCH_LEN - LZX_NUM_PRIMARY_LENS];
+               cost += costs->len[len - LZX_MIN_MATCH_LEN - LZX_NUM_PRIMARY_LENS];
 
        return cost;
-
 }
 
-
 /* Set the cost model @c->costs from the Huffman codeword lengths specified in
  * @lens.
  *
@@ -1449,19 +1442,20 @@ lzx_choose_near_optimal_item(struct lzx_compressor *c)
        unsigned num_matches;
        const struct lz_match *matches;
        struct lz_match match;
-       unsigned longest_len;
-       unsigned longest_rep_len;
-       u32 longest_rep_offset;
+       u32 longest_len;
+       u32 longest_rep_len;
+       unsigned longest_rep_slot;
        unsigned cur_pos;
        unsigned end_pos;
+       struct lzx_mc_pos_data *optimum = c->optimum;
 
        if (c->optimum_cur_idx != c->optimum_end_idx) {
                /* Case 2: Return the next match/literal already found.  */
-               match.len = c->optimum[c->optimum_cur_idx].next.link -
+               match.len = optimum[c->optimum_cur_idx].next.link -
                                    c->optimum_cur_idx;
-               match.offset = c->optimum[c->optimum_cur_idx].next.match_offset;
+               match.offset = optimum[c->optimum_cur_idx].next.match_offset;
 
-               c->optimum_cur_idx = c->optimum[c->optimum_cur_idx].next.link;
+               c->optimum_cur_idx = optimum[c->optimum_cur_idx].next.link;
                return match;
        }
 
@@ -1470,8 +1464,8 @@ lzx_choose_near_optimal_item(struct lzx_compressor *c)
        c->optimum_cur_idx = 0;
        c->optimum_end_idx = 0;
 
-       /* Search for matches at recent offsets.  Only keep the one with the
-        * longest match length.  */
+       /* Search for matches at repeat offsets.  As a heuristic, we only keep
+        * the one with the longest match length.  */
        longest_rep_len = LZX_MIN_MATCH_LEN - 1;
        if (c->match_window_pos >= 1) {
                unsigned limit = min(LZX_MAX_MATCH_LEN,
@@ -1485,24 +1479,24 @@ lzx_choose_near_optimal_item(struct lzx_compressor *c)
                                len++;
                        if (len > longest_rep_len) {
                                longest_rep_len = len;
-                               longest_rep_offset = offset;
+                               longest_rep_slot = i;
                        }
                }
        }
 
-       /* If there's a long match with a recent offset, take it.  */
+       /* If there's a long match with a repeat offset, choose it immediately.  */
        if (longest_rep_len >= c->params.nice_match_length) {
                lzx_skip_bytes(c, longest_rep_len);
                return (struct lz_match) {
                        .len = longest_rep_len,
-                       .offset = longest_rep_offset,
+                       .offset = c->queue.R[longest_rep_slot],
                };
        }
 
-       /* Search other matches.  */
+       /* Find other matches.  */
        num_matches = lzx_get_matches(c, &matches);
 
-       /* If there's a long match, take it.  */
+       /* If there's a long match, choose it immediately.  */
        if (num_matches) {
                longest_len = matches[num_matches - 1].len;
                if (longest_len >= c->params.nice_match_length) {
@@ -1513,12 +1507,11 @@ lzx_choose_near_optimal_item(struct lzx_compressor *c)
                longest_len = 1;
        }
 
-       /* Calculate the cost to reach the next position by coding a literal.
-        */
-       c->optimum[1].queue = c->queue;
-       c->optimum[1].cost = lzx_literal_cost(c->cur_window[c->match_window_pos - 1],
+       /* Calculate the cost to reach the next position by coding a literal.  */
+       optimum[1].queue = c->queue;
+       optimum[1].cost = lzx_literal_cost(c->cur_window[c->match_window_pos - 1],
                                              &c->costs);
-       c->optimum[1].prev.link = 0;
+       optimum[1].prev.link = 0;
 
        /* Calculate the cost to reach any position up to and including that
         * reached by the longest match.
@@ -1564,29 +1557,30 @@ lzx_choose_near_optimal_item(struct lzx_compressor *c)
                        if (len_header == LZX_NUM_PRIMARY_LENS)
                                cost += c->costs.len[len - LZX_MIN_MATCH_LEN - LZX_NUM_PRIMARY_LENS];
 
-                       c->optimum[len].queue = queue;
-                       c->optimum[len].prev.link = 0;
-                       c->optimum[len].prev.match_offset = offset;
-                       c->optimum[len].cost = cost;
+                       optimum[len].queue = queue;
+                       optimum[len].prev.link = 0;
+                       optimum[len].prev.match_offset = offset;
+                       optimum[len].cost = cost;
                } while (++len <= matches[i].len);
        }
        end_pos = longest_len;
 
        if (longest_rep_len >= LZX_MIN_MATCH_LEN) {
-               struct lzx_lru_queue queue;
                u32 cost;
 
                while (end_pos < longest_rep_len)
-                       c->optimum[++end_pos].cost = MC_INFINITE_COST;
+                       optimum[++end_pos].cost = MC_INFINITE_COST;
 
-               queue = c->queue;
-               cost = lzx_match_cost(longest_rep_len, longest_rep_offset,
-                                     &c->costs, &queue);
-               if (cost <= c->optimum[longest_rep_len].cost) {
-                       c->optimum[longest_rep_len].queue = queue;
-                       c->optimum[longest_rep_len].prev.link = 0;
-                       c->optimum[longest_rep_len].prev.match_offset = longest_rep_offset;
-                       c->optimum[longest_rep_len].cost = cost;
+               cost = lzx_repmatch_cost(longest_rep_len, longest_rep_slot,
+                                        &c->costs);
+               if (cost <= optimum[longest_rep_len].cost) {
+                       optimum[longest_rep_len].queue = c->queue;
+                       swap(optimum[longest_rep_len].queue.R[0],
+                            optimum[longest_rep_len].queue.R[longest_rep_slot]);
+                       optimum[longest_rep_len].prev.link = 0;
+                       optimum[longest_rep_len].prev.match_offset =
+                               optimum[longest_rep_len].queue.R[0];
+                       optimum[longest_rep_len].cost = cost;
                }
        }
 
@@ -1594,17 +1588,16 @@ lzx_choose_near_optimal_item(struct lzx_compressor *c)
         * position.  The algorithm may find multiple paths to reach each
         * position; only the lowest-cost path is saved.
         *
-        * The progress of the parse is tracked in the @c->optimum array, which
-        * for each position contains the minimum cost to reach that position,
-        * the index of the start of the match/literal taken to reach that
-        * position through the minimum-cost path, the offset of the match taken
-        * (not relevant for literals), and the adaptive state that will exist
-        * at that position after the minimum-cost path is taken.  The @cur_pos
+        * The progress of the parse is tracked in the @optimum array, which for
+        * each position contains the minimum cost to reach that position, the
+        * index of the start of the match/literal taken to reach that position
+        * through the minimum-cost path, the offset of the match taken (not
+        * relevant for literals), and the adaptive state that will exist at
+        * that position after the minimum-cost path is taken.  The @cur_pos
         * variable stores the position at which the algorithm is currently
         * considering coding choices, and the @end_pos variable stores the
         * greatest position at which the costs of coding choices have been
-        * saved.  (Actually, the algorithm guarantees that all positions up to
-        * and including @end_pos are reachable by at least one path.)
+        * saved.
         *
         * The loop terminates when any one of the following conditions occurs:
         *
@@ -1620,7 +1613,7 @@ lzx_choose_near_optimal_item(struct lzx_compressor *c)
         *    match/literal list.
         *
         * 3. Failing either of the above in a degenerate case, the loop
-        *    terminates when space in the @c->optimum array is exhausted.
+        *    terminates when space in the @optimum array is exhausted.
         *    This terminates the algorithm and forces it to start returning
         *    matches/literals even though they may not be globally optimal.
         *
@@ -1642,12 +1635,13 @@ lzx_choose_near_optimal_item(struct lzx_compressor *c)
                if (cur_pos == end_pos || cur_pos == LZX_OPTIM_ARRAY_LENGTH)
                        return lzx_match_chooser_reverse_list(c, cur_pos);
 
-               /* Search for matches at recent offsets.  */
+               /* Search for matches at repeat offsets.  Again, as a heuristic
+                * we only keep the longest one.  */
                longest_rep_len = LZX_MIN_MATCH_LEN - 1;
                unsigned limit = min(LZX_MAX_MATCH_LEN,
                                     c->match_window_end - c->match_window_pos);
                for (int i = 0; i < LZX_NUM_RECENT_OFFSETS; i++) {
-                       u32 offset = c->optimum[cur_pos].queue.R[i];
+                       u32 offset = optimum[cur_pos].queue.R[i];
                        const u8 *strptr = &c->cur_window[c->match_window_pos];
                        const u8 *matchptr = strptr - offset;
                        unsigned len = 0;
@@ -1655,11 +1649,11 @@ lzx_choose_near_optimal_item(struct lzx_compressor *c)
                                len++;
                        if (len > longest_rep_len) {
                                longest_rep_len = len;
-                               longest_rep_offset = offset;
+                               longest_rep_slot = i;
                        }
                }
 
-               /* If we found a long match at a recent offset, choose it
+               /* If we found a long match at a repeat offset, choose it
                 * immediately.  */
                if (longest_rep_len >= c->params.nice_match_length) {
                        /* Build the list of matches to return and get
@@ -1667,8 +1661,9 @@ lzx_choose_near_optimal_item(struct lzx_compressor *c)
                        match = lzx_match_chooser_reverse_list(c, cur_pos);
 
                        /* Append the long match to the end of the list.  */
-                       c->optimum[cur_pos].next.match_offset = longest_rep_offset;
-                       c->optimum[cur_pos].next.link = cur_pos + longest_rep_len;
+                       optimum[cur_pos].next.match_offset =
+                               optimum[cur_pos].queue.R[longest_rep_slot];
+                       optimum[cur_pos].next.link = cur_pos + longest_rep_len;
                        c->optimum_end_idx = cur_pos + longest_rep_len;
 
                        /* Skip over the remaining bytes of the long match.  */
@@ -1678,10 +1673,10 @@ lzx_choose_near_optimal_item(struct lzx_compressor *c)
                        return match;
                }
 
-               /* Search other matches.  */
+               /* Find other matches.  */
                num_matches = lzx_get_matches(c, &matches);
 
-               /* If there's a long match, take it.  */
+               /* If there's a long match, choose it immediately.  */
                if (num_matches) {
                        longest_len = matches[num_matches - 1].len;
                        if (longest_len >= c->params.nice_match_length) {
@@ -1690,9 +1685,9 @@ lzx_choose_near_optimal_item(struct lzx_compressor *c)
                                match = lzx_match_chooser_reverse_list(c, cur_pos);
 
                                /* Append the long match to the end of the list.  */
-                               c->optimum[cur_pos].next.match_offset =
+                               optimum[cur_pos].next.match_offset =
                                        matches[num_matches - 1].offset;
-                               c->optimum[cur_pos].next.link = cur_pos + longest_len;
+                               optimum[cur_pos].next.link = cur_pos + longest_len;
                                c->optimum_end_idx = cur_pos + longest_len;
 
                                /* Skip over the remaining bytes of the long match.  */
@@ -1705,17 +1700,19 @@ lzx_choose_near_optimal_item(struct lzx_compressor *c)
                        longest_len = 1;
                }
 
+               /* If we are reaching any positions for the first time, we need
+                * to initialize their costs to infinity.  */
                while (end_pos < cur_pos + longest_len)
-                       c->optimum[++end_pos].cost = MC_INFINITE_COST;
+                       optimum[++end_pos].cost = MC_INFINITE_COST;
 
                /* Consider coding a literal.  */
-               cost = c->optimum[cur_pos].cost +
+               cost = optimum[cur_pos].cost +
                        lzx_literal_cost(c->cur_window[c->match_window_pos - 1],
                                         &c->costs);
-               if (cost < c->optimum[cur_pos + 1].cost) {
-                       c->optimum[cur_pos + 1].queue = c->optimum[cur_pos].queue;
-                       c->optimum[cur_pos + 1].cost = cost;
-                       c->optimum[cur_pos + 1].prev.link = cur_pos;
+               if (cost < optimum[cur_pos + 1].cost) {
+                       optimum[cur_pos + 1].queue = optimum[cur_pos].queue;
+                       optimum[cur_pos + 1].cost = cost;
+                       optimum[cur_pos + 1].prev.link = cur_pos;
                }
 
                /* Consider coding a match.
@@ -1728,16 +1725,25 @@ lzx_choose_near_optimal_item(struct lzx_compressor *c)
                 * length.  */
                for (unsigned i = 0, len = 2; i < num_matches; i++) {
                        u32 offset;
-                       struct lzx_lru_queue queue;
                        u32 position_cost;
                        unsigned position_slot;
                        unsigned num_extra_bits;
 
                        offset = matches[i].offset;
-                       queue = c->optimum[cur_pos].queue;
-                       position_cost = c->optimum[cur_pos].cost;
+                       position_cost = optimum[cur_pos].cost;
+
+                       /* Yet another optimization: instead of calling
+                        * lzx_get_position_slot(), hand-inline the search of
+                        * the repeat offset queue.  Then we can omit the
+                        * extra_bits calculation for repeat offset matches, and
+                        * also only compute the updated queue if we actually do
+                        * find a new lowest cost path.  */
+                       for (position_slot = 0; position_slot < LZX_NUM_RECENT_OFFSETS; position_slot++)
+                               if (offset == optimum[cur_pos].queue.R[position_slot])
+                                       goto have_position_cost;
+
+                       position_slot = lzx_get_position_slot_raw(offset + LZX_OFFSET_OFFSET);
 
-                       position_slot = lzx_get_position_slot(offset, &queue);
                        num_extra_bits = lzx_get_num_extra_bits(position_slot);
                        if (num_extra_bits >= 3) {
                                position_cost += num_extra_bits - 3;
@@ -1747,6 +1753,8 @@ lzx_choose_near_optimal_item(struct lzx_compressor *c)
                                position_cost += num_extra_bits;
                        }
 
+               have_position_cost:
+
                        do {
                                unsigned len_header;
                                unsigned main_symbol;
@@ -1764,34 +1772,65 @@ lzx_choose_near_optimal_item(struct lzx_compressor *c)
                                                        LZX_MIN_MATCH_LEN -
                                                        LZX_NUM_PRIMARY_LENS];
                                }
-                               if (cost < c->optimum[cur_pos + len].cost) {
-                                       c->optimum[cur_pos + len].queue = queue;
-                                       c->optimum[cur_pos + len].prev.link = cur_pos;
-                                       c->optimum[cur_pos + len].prev.match_offset = offset;
-                                       c->optimum[cur_pos + len].cost = cost;
+                               if (cost < optimum[cur_pos + len].cost) {
+                                       if (position_slot < LZX_NUM_RECENT_OFFSETS) {
+                                               optimum[cur_pos + len].queue = optimum[cur_pos].queue;
+                                               swap(optimum[cur_pos + len].queue.R[0],
+                                                    optimum[cur_pos + len].queue.R[position_slot]);
+                                       } else {
+                                               optimum[cur_pos + len].queue.R[0] = offset;
+                                               optimum[cur_pos + len].queue.R[1] = optimum[cur_pos].queue.R[0];
+                                               optimum[cur_pos + len].queue.R[2] = optimum[cur_pos].queue.R[1];
+                                       }
+                                       optimum[cur_pos + len].prev.link = cur_pos;
+                                       optimum[cur_pos + len].prev.match_offset = offset;
+                                       optimum[cur_pos + len].cost = cost;
                                }
                        } while (++len <= matches[i].len);
                }
 
+               /* Consider coding a repeat offset match.
+                *
+                * As a heuristic, we only consider the longest length of the
+                * longest repeat offset match.  This does not, however,
+                * necessarily mean that we will never consider any other repeat
+                * offsets, because above we detect repeat offset matches that
+                * were found by the regular match-finder.  Therefore, this
+                * special handling of the longest repeat-offset match is only
+                * helpful for coding a repeat offset match that was *not* found
+                * by the match-finder, e.g. due to being obscured by a less
+                * distant match that is at least as long.
+                *
+                * Note: an alternative, used in LZMA, is to consider every
+                * length of every repeat offset match.  This is a more thorough
+                * search, and it makes it unnecessary to detect repeat offset
+                * matches that were found by the regular match-finder.  But by
+                * my tests, for LZX the LZMA method slows down the compressor
+                * by ~10% and doesn't actually help the compression ratio too
+                * much.
+                *
+                * Also tested a compromise approach: consider every 3rd length
+                * of the longest repeat offset match.  Still didn't seem quite
+                * worth it, though.
+                */
                if (longest_rep_len >= LZX_MIN_MATCH_LEN) {
-                       struct lzx_lru_queue queue;
 
                        while (end_pos < cur_pos + longest_rep_len)
-                               c->optimum[++end_pos].cost = MC_INFINITE_COST;
-
-                       queue = c->optimum[cur_pos].queue;
-
-                       cost = c->optimum[cur_pos].cost +
-                               lzx_match_cost(longest_rep_len, longest_rep_offset,
-                                              &c->costs, &queue);
-                       if (cost <= c->optimum[cur_pos + longest_rep_len].cost) {
-                               c->optimum[cur_pos + longest_rep_len].queue =
-                                       queue;
-                               c->optimum[cur_pos + longest_rep_len].prev.link =
+                               optimum[++end_pos].cost = MC_INFINITE_COST;
+
+                       cost = optimum[cur_pos].cost +
+                               lzx_repmatch_cost(longest_rep_len, longest_rep_slot,
+                                                 &c->costs);
+                       if (cost <= optimum[cur_pos + longest_rep_len].cost) {
+                               optimum[cur_pos + longest_rep_len].queue =
+                                       optimum[cur_pos].queue;
+                               swap(optimum[cur_pos + longest_rep_len].queue.R[0],
+                                    optimum[cur_pos + longest_rep_len].queue.R[longest_rep_slot]);
+                               optimum[cur_pos + longest_rep_len].prev.link =
                                        cur_pos;
-                               c->optimum[cur_pos + longest_rep_len].prev.match_offset =
-                                       longest_rep_offset;
-                               c->optimum[cur_pos + longest_rep_len].cost =
+                               optimum[cur_pos + longest_rep_len].prev.match_offset =
+                                       optimum[cur_pos + longest_rep_len].queue.R[0];
+                               optimum[cur_pos + longest_rep_len].cost =
                                        cost;
                        }
                }