]> wimlib.net Git - wimlib/blobdiff - src/lzx-compress.c
lzx-compress.c: Speed up rep match cost evaluations
[wimlib] / src / lzx-compress.c
index db0e7c10f0714b2272626a77abdd71bd97fb7b2c..070e2bfc005e0211dbe6e8e6b95e8fdb9978ce6a 100644 (file)
@@ -1059,46 +1059,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 +1430,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;
        }
 
@@ -1485,7 +1467,7 @@ 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;
                        }
                }
        }
@@ -1495,7 +1477,7 @@ lzx_choose_near_optimal_item(struct lzx_compressor *c)
                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],
                };
        }
 
@@ -1515,10 +1497,10 @@ lzx_choose_near_optimal_item(struct lzx_compressor *c)
 
        /* 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],
+       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 +1546,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,7 +1577,7 @@ 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
+        * 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
@@ -1620,7 +1603,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.
         *
@@ -1647,7 +1630,7 @@ lzx_choose_near_optimal_item(struct lzx_compressor *c)
                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,7 +1638,7 @@ 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;
                        }
                }
 
@@ -1667,8 +1650,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.  */
@@ -1690,9 +1674,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.  */
@@ -1706,16 +1690,16 @@ lzx_choose_near_optimal_item(struct lzx_compressor *c)
                }
 
                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.
@@ -1734,8 +1718,8 @@ lzx_choose_near_optimal_item(struct lzx_compressor *c)
                        unsigned num_extra_bits;
 
                        offset = matches[i].offset;
-                       queue = c->optimum[cur_pos].queue;
-                       position_cost = c->optimum[cur_pos].cost;
+                       queue = optimum[cur_pos].queue;
+                       position_cost = optimum[cur_pos].cost;
 
                        position_slot = lzx_get_position_slot(offset, &queue);
                        num_extra_bits = lzx_get_num_extra_bits(position_slot);
@@ -1764,34 +1748,33 @@ 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) {
+                                       optimum[cur_pos + len].queue = queue;
+                                       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);
                }
 
                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;
                        }
                }