]> wimlib.net Git - wimlib/blobdiff - src/lzx-compress.c
Variable chunk size support (currently XPRESS only)
[wimlib] / src / lzx-compress.c
index a31fc2ab2271e889f5e9cd2bde4f2657bf5b8a0d..6e1a0d9c6cd873737afc179831411d3e93079c63 100644 (file)
@@ -42,7 +42,8 @@
  * and certain other details are quite similar, such as the method for storing
  * Huffman codes.  However, some of the main differences are:
  *
- * - LZX preprocesses the data before attempting to compress it.
+ * - LZX preprocesses the data to attempt to make x86 machine code slightly more
+ *   compressible before attempting to compress it further.
  * - LZX uses a "main" alphabet which combines literals and matches, with the
  *   match symbols containing a "length header" (giving all or part of the match
  *   length) and a "position slot" (giving, roughly speaking, the order of
@@ -51,7 +52,8 @@
  *   dynamic Huffman blocks ("aligned offset" and "verbatim").
  * - LZX has a minimum match length of 2 rather than 3.
  * - In LZX, match offsets 0 through 2 actually represent entries in an LRU
- *   queue of match offsets.
+ *   queue of match offsets.  This is very useful for certain types of files,
+ *   such as binary files that have repeating records.
  *
  * Algorithms
  * ==========
  * 1. Preprocess the input data to translate the targets of x86 call instructions
  *    to absolute offsets.
  *
- * 2. Build the suffix array and inverse suffix array for the input data.
+ * 2. Build the suffix array and inverse suffix array for the input data.  The
+ *    suffix array contains the indices of all suffixes of the input data,
+ *    sorted lexcographically by the corresponding suffixes.  The "position" of
+ *    a suffix is the index of that suffix in the original string, whereas the
+ *    "rank" of a suffix is the index at which that suffix's position is found
+ *    in the suffix array.
  *
  * 3. Build the longest common prefix array corresponding to the suffix array.
  *
- * 4. For each suffix rank, find the highest lower suffix rank that has a
- *    lower position, the lowest higher suffix rank that has a lower position,
- *    and the length of the common prefix shared between each.  (Position =
- *    index of suffix in original string, rank = index of suffix in suffix
- *    array.)  This information is later used to link suffix ranks into a
- *    doubly-linked list for searching the suffix array.
+ * 4. For each suffix, find the highest lower ranked suffix that has a
+ *    lower position, the lowest higher ranked suffix that has a lower position,
+ *    and the length of the common prefix shared between each.   This
+ *    information is later used to link suffix ranks into a doubly-linked list
+ *    for searching the suffix array.
  *
  * 5. Set a default cost model for matches/literals.
  *
  *    and literal bytes to divide the input into.  Raw match-finding is done by
  *    searching the suffix array using a linked list to avoid considering any
  *    suffixes that start after the current position.  Each run of the
- *    match-finder returns the lowest-cost longest match as well as any shorter
- *    matches that have even lower costs.  Each such run also adds the suffix
- *    rank of the current position into the linked list being used to search the
- *    suffix array.  Parsing, or match-choosing, is solved as a minimum-cost
- *    path problem using a forward "optimal parsing" algorithm based on the
- *    Deflate encoder from 7-Zip.  This algorithm moves forward calculating the
- *    minimum cost to reach each byte until either a very long match is found or
- *    until a position is found at which no matches start or overlap.
+ *    match-finder returns the approximate lowest-cost longest match as well as
+ *    any shorter matches that have even lower approximate costs.  Each such run
+ *    also adds the suffix rank of the current position into the linked list
+ *    being used to search the suffix array.  Parsing, or match-choosing, is
+ *    solved as a minimum-cost path problem using a forward "optimal parsing"
+ *    algorithm based on the Deflate encoder from 7-Zip.  This algorithm moves
+ *    forward calculating the minimum cost to reach each byte until either a
+ *    very long match is found or until a position is found at which no matches
+ *    start or overlap.
  *
  * 7. Build the Huffman codes needed to output the matches/literals.
  *
  * 9. Output the resulting block using the match/literal sequences and the
  *    Huffman codes that were computed for the block.
  *
+ * Note: the algorithm does not yet attempt to split the input into multiple LZX
+ * blocks.
+ *
  * Fast algorithm
  * --------------
  *
  * The fast algorithm (and the only one available in wimlib v1.5.1 and earlier)
  * spends much less time on the main bottlenecks of the compression process ---
- * that is the match finding, match choosing, and block splitting.  Matches are
- * found and chosen with hash chains using a greedy parse with one position of
- * look-ahead.  No block splitting is done; only compressing the full input into
- * an aligned offset block is considered.
+ * that is, the match finding and match choosing.  Matches are found and chosen
+ * with hash chains using a greedy parse with one position of look-ahead.  No
+ * block splitting is done; only compressing the full input into an aligned
+ * offset block is considered.
  *
  * API
  * ===
  *     wimlib_lzx_alloc_context()
  *     wimlib_lzx_compress2()
  *     wimlib_lzx_free_context()
+ *     wimlib_lzx_set_default_params()
  *
  * Both wimlib_lzx_compress() and wimlib_lzx_compress2() are designed to
  * compress an in-memory buffer of up to 32768 bytes.  There is no sliding
  * Again, this is suitable for the WIM format, which stores such data chunks
  * uncompressed.
  *
- * The functions in this API are exported from the library, although this is
- * only in case other programs happen to have uses for it other than WIM
+ * The functions in this LZX compression API are exported from the library,
+ * although with the possible exception of wimlib_lzx_set_default_params(), this
+ * is only in case other programs happen to have uses for it other than WIM
  * reading/writing as already handled through the rest of the library.
  *
  * Acknowledgments
  * Acknowledgments to several open-source projects and research papers that made
  * it possible to implement this code:
  *
- * - divsufsort (author: Yuta Mori), for the suffix array construction code.
+ * - divsufsort (author: Yuta Mori), for the suffix array construction code,
+ *   located in a separate directory (divsufsort/).
  *
  * - "Linear-Time Longest-Common-Prefix Computation in Suffix Arrays and Its
  *   Applications" (Kasai et al. 2001), for the LCP array computation.
  *   (match-choosing).
  *
  * - zlib (author: Jean-loup Gailly and Mark Adler), for the hash table
- *   match-finding algorithm.
+ *   match-finding algorithm (used in lz77.c).
  *
  * - lzx-compress (author: Matthew T. Russotto), on which some parts of this
  *   code were originally based.
 #include <string.h>
 
 #ifdef ENABLE_LZX_DEBUG
-#  include <wimlib/decompress.h>
+#  include "wimlib/decompress.h"
 #endif
 
 #include "divsufsort/divsufsort.h"
 
-typedef freq_t input_idx_t;
-typedef u32 sym_cost_t;
 typedef u32 block_cost_t;
-#define INFINITE_SYM_COST      ((sym_cost_t)~0U)
 #define INFINITE_BLOCK_COST    ((block_cost_t)~0U)
 
 #define LZX_OPTIM_ARRAY_SIZE   4096
@@ -229,9 +239,9 @@ struct lzx_lens {
  * --- generally a high cost, since even if it gets used in the next iteration,
  * it probably will not be used very times.  */
 struct lzx_costs {
-       sym_cost_t main[LZX_MAINCODE_NUM_SYMBOLS];
-       sym_cost_t len[LZX_LENCODE_NUM_SYMBOLS];
-       sym_cost_t aligned[LZX_ALIGNEDCODE_NUM_SYMBOLS];
+       u8 main[LZX_MAINCODE_NUM_SYMBOLS];
+       u8 len[LZX_LENCODE_NUM_SYMBOLS];
+       u8 aligned[LZX_ALIGNEDCODE_NUM_SYMBOLS];
 };
 
 /* The LZX main, length, and aligned offset Huffman codes  */
@@ -406,10 +416,6 @@ struct lzx_compressor {
         * block.  */
        struct lzx_match *chosen_matches;
 
-       struct raw_match *cached_matches;
-       unsigned cached_matches_pos;
-       bool matches_cached;
-
        /* Information about the LZX blocks the preprocessed input was divided
         * into.  */
        struct lzx_block_spec *block_specs;
@@ -424,29 +430,19 @@ struct lzx_compressor {
         * codewords.  */
        struct lzx_codes zero_codes;
 
-       /* Slow algorithm only: The current cost model.  */
+       /* The current cost model.  */
        struct lzx_costs costs;
 
-       /* Slow algorithm only: Suffix array for window.
-        * This is a mapping from suffix rank to suffix position.
-        *
-        * Suffix rank means the index of the suffix in the sorted list of
-        * suffixes, whereas suffix position means the index in the string at
-        * which the suffix starts.
-        */
+       /* Suffix array for window.
+        * This is a mapping from suffix rank to suffix position.  */
        input_idx_t *SA;
 
-       /* Slow algorithm only: Inverse suffix array for window.
+       /* Inverse suffix array for window.
         * This is a mapping from suffix position to suffix rank.
-        * In other words, if 0 <= r < window_size, then ISA[SA[r]] == r.  */
+        * If 0 <= r < window_size, then ISA[SA[r]] == r.  */
        input_idx_t *ISA;
 
-       /* Slow algorithm only: Longest Common Prefix array.  LCP[i] is the
-        * number of initial bytes that the suffixes at positions SA[i - 1] and
-        * SA[i] share.  LCP[0] is undefined.  */
-       input_idx_t *LCP;
-
-       /* Slow algorithm only: Suffix array links.
+       /* Suffix array links.
         *
         * During a linear scan of the input string to find matches, this array
         * used to keep track of which rank suffixes in the suffix array appear
@@ -455,17 +451,26 @@ struct lzx_compressor {
         * list containing only suffixes that appear before that position.  */
        struct salink *salink;
 
-       /* Slow algorithm only: Position in window of next match to return.
-        * This cannot simply be modified, as the match-finder must still be
-        * synchronized on the same position.  To seek forwards or backwards,
+       /* Position in window of next match to return.
+        * Note: This cannot simply be modified, as the match-finder must still
+        * be synchronized on the same position.  To seek forwards or backwards,
         * use lzx_lz_skip_bytes() or lzx_lz_rewind_matchfinder(), respectively.
         */
        input_idx_t match_window_pos;
 
-       /* Slow algorithm only: The match-finder shall ensure the length of
-        * matches does not exceed this position in the input.  */
+       /* The match-finder shall ensure the length of matches does not exceed
+        * this position in the input.  */
        input_idx_t match_window_end;
 
+       /* Matches found by the match-finder are cached in the following array
+        * to achieve a slight speedup when the same matches are needed on
+        * subsequent passes.  This is suboptimal because different matches may
+        * be preferred with different cost models, but seems to be a worthwhile
+        * speedup.  */
+       struct raw_match *cached_matches;
+       unsigned cached_matches_pos;
+       bool matches_cached;
+
        /* Slow algorithm only: Temporary space used for match-choosing
         * algorithm.
         *
@@ -494,7 +499,7 @@ struct lzx_compressor {
  * numbers in the lzx_position_base array to calculate the slot directly from
  * the formatted offset without actually looking at the array.
  */
-static _always_inline_attribute unsigned
+static unsigned
 lzx_get_position_slot_raw(unsigned formatted_offset)
 {
 #if 0
@@ -526,8 +531,9 @@ lzx_get_position_slot_raw(unsigned formatted_offset)
 
 
 /* Returns the LZX position slot that corresponds to a given match offset,
- * taking into account the recent offset queue (and optionally updating it).  */
-static _always_inline_attribute unsigned
+ * taking into account the recent offset queue and updating it if the offset is
+ * found in it.  */
+static unsigned
 lzx_get_position_slot(unsigned offset, struct lzx_lru_queue *queue)
 {
        unsigned position_slot;
@@ -542,9 +548,9 @@ lzx_get_position_slot(unsigned offset, struct lzx_lru_queue *queue)
                         * LRU queue because repeat matches are simply
                         * swapped to the front.  */
                        swap(queue->R[0], queue->R[i]);
-                       /* For recent offsets, the position slot is simply the
-                        * index of the entry in the queue.  */
 
+                       /* The resulting position slot is simply the first index
+                        * at which the offset was found in the queue.  */
                        return i;
                }
        }
@@ -1067,13 +1073,10 @@ lzx_write_all_blocks(struct lzx_compressor *ctx, struct output_bitstream *ostrea
 /* Constructs an LZX match from a literal byte and updates the main code symbol
  * frequencies.  */
 static u32
-lzx_record_literal(u8 literal, void *_freqs)
+lzx_tally_literal(u8 lit, struct lzx_freqs *freqs)
 {
-       struct lzx_freqs *freqs = _freqs;
-
-       freqs->main[literal]++;
-
-       return (u32)literal;
+       freqs->main[lit]++;
+       return (u32)lit;
 }
 
 /* Constructs an LZX match from an offset and a length, and updates the LRU
@@ -1081,11 +1084,9 @@ lzx_record_literal(u8 literal, void *_freqs)
  * alphabets.  The return value is a 32-bit number that provides the match in an
  * intermediate representation documented below.  */
 static u32
-lzx_record_match(unsigned match_offset, unsigned match_len,
-                void *_freqs, void *_queue)
+lzx_tally_match(unsigned match_len, unsigned match_offset,
+               struct lzx_freqs *freqs, struct lzx_lru_queue *queue)
 {
-       struct lzx_freqs *freqs = _freqs;
-       struct lzx_lru_queue *queue = _queue;
        unsigned position_slot;
        unsigned position_footer;
        u32 len_header;
@@ -1159,9 +1160,31 @@ lzx_record_match(unsigned match_offset, unsigned match_len,
                (adjusted_match_len);
 }
 
+struct lzx_record_ctx {
+       struct lzx_freqs freqs;
+       struct lzx_lru_queue queue;
+       struct lzx_match *matches;
+};
+
+static void
+lzx_record_match(unsigned len, unsigned offset, void *_ctx)
+{
+       struct lzx_record_ctx *ctx = _ctx;
+
+       (ctx->matches++)->data = lzx_tally_match(len, offset, &ctx->freqs, &ctx->queue);
+}
+
+static void
+lzx_record_literal(u8 lit, void *_ctx)
+{
+       struct lzx_record_ctx *ctx = _ctx;
+
+       (ctx->matches++)->data = lzx_tally_literal(lit, &ctx->freqs);
+}
+
 /* Returns the cost, in bits, to output a literal byte using the specified cost
  * model.  */
-static sym_cost_t
+static unsigned
 lzx_literal_cost(u8 c, const struct lzx_costs * costs)
 {
        return costs->main[c];
@@ -1172,18 +1195,18 @@ lzx_literal_cost(u8 c, const struct lzx_costs * costs)
  * 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 optionally update it.  */
-static sym_cost_t
+static unsigned
 lzx_match_cost(unsigned length, unsigned offset, const struct lzx_costs *costs,
               struct lzx_lru_queue *queue)
 {
        unsigned position_slot;
        unsigned len_header, main_symbol;
-       sym_cost_t cost = 0;
+       unsigned cost = 0;
 
        position_slot = lzx_get_position_slot(offset, queue);
 
        len_header = min(length - LZX_MIN_MATCH_LEN, LZX_NUM_PRIMARY_LENS);
-       main_symbol = (position_slot << 3) | len_header | LZX_NUM_CHARS;
+       main_symbol = ((position_slot << 3) | len_header) + LZX_NUM_CHARS;
 
        /* Account for main symbol.  */
        cost += costs->main[main_symbol];
@@ -1205,16 +1228,19 @@ lzx_match_cost(unsigned length, unsigned offset, const struct lzx_costs *costs,
 
 }
 
-/* Very fast heuristic cost evaluation to use in the inner loop of the
- * match-finder.  */
-static sym_cost_t
+/* Fast heuristic cost evaluation to use in the inner loop of the match-finder.
+ * Unlike lzx_match_cost() which does a true cost evaluation, this simply
+ * prioritize matches based on their offset.  */
+static block_cost_t
 lzx_match_cost_fast(unsigned offset, const struct lzx_lru_queue *queue)
 {
+       /* It seems well worth it to take the time to give priority to recently
+        * used offsets.  */
        for (unsigned i = 0; i < LZX_NUM_RECENT_OFFSETS; i++)
                if (offset == queue->R[i])
                        return i;
 
-       BUILD_BUG_ON(LZX_MAX_WINDOW_SIZE >= (sym_cost_t)~0U);
+       BUILD_BUG_ON(LZX_MAX_WINDOW_SIZE >= (block_cost_t)~0U);
        return offset;
 }
 
@@ -1303,6 +1329,14 @@ lzx_lz_rewind_matchfinder(struct lzx_compressor *ctx,
        if (ctx->match_window_pos == orig_pos)
                return;
 
+       /* NOTE: this has been optimized for the current algorithm where no
+        * block-splitting is done and matches are cached, so that the suffix
+        * array match-finder only runs through the input one time.  Generalized
+        * rewinds of the suffix array match-finder are possible, but require
+        * incrementally saving fields being overwritten in
+        * lzx_lz_update_salink(), then restoring them here in reverse order.
+        */
+
        LZX_ASSERT(ctx->match_window_pos > orig_pos);
        LZX_ASSERT(orig_pos == 0);
        ctx->matches_cached = true;
@@ -1373,7 +1407,7 @@ lzx_lz_get_matches(const input_idx_t i,
         * We keep track of this so that we can ignore shorter matches that do
         * not have lower costs than a longer matches already found.
         */
-       sym_cost_t best_cost = INFINITE_SYM_COST;
+       block_cost_t best_cost = INFINITE_BLOCK_COST;
 
        /* count_remaining = maximum number of possible matches remaining to be
         * considered.  */
@@ -1381,13 +1415,13 @@ lzx_lz_get_matches(const input_idx_t i,
 
        /* pending = match currently being considered for a specific length.  */
        struct raw_match pending;
+       block_cost_t pending_cost;
 
        while (lenL >= min_match_len || lenR >= min_match_len)
        {
                pending.len = lenL;
-               pending.offset = (input_idx_t)~0U;
-               sym_cost_t pending_cost = INFINITE_SYM_COST;
-               sym_cost_t cost;
+               pending_cost = INFINITE_BLOCK_COST;
+               block_cost_t cost;
 
                /* Extend left.  */
                if (lenL >= min_match_len && lenL >= lenR) {
@@ -1423,8 +1457,7 @@ lzx_lz_get_matches(const input_idx_t i,
                                                                return nmatches;
                                                }
                                                pending.len = lenL;
-                                               pending.offset = (input_idx_t)~0U;
-                                               pending_cost = INFINITE_SYM_COST;
+                                               pending_cost = INFINITE_BLOCK_COST;
                                        }
                                        if (lenL < min_match_len || lenL < lenR)
                                                break;
@@ -1470,8 +1503,7 @@ lzx_lz_get_matches(const input_idx_t i,
                                                break;
 
                                        pending.len = lenR;
-                                       pending.offset = (input_idx_t)~0U;
-                                       pending_cost = INFINITE_SYM_COST;
+                                       pending_cost = INFINITE_BLOCK_COST;
                                }
                                R = link[R].next;
                        }
@@ -1480,7 +1512,7 @@ lzx_lz_get_matches(const input_idx_t i,
        goto out;
 
 out_save_pending:
-       if (pending.offset != (input_idx_t)~0U)
+       if (pending_cost != INFINITE_BLOCK_COST)
                matches[nmatches++] = pending;
 
 out:
@@ -1529,8 +1561,8 @@ lzx_lz_get_matches_caching(struct lzx_compressor *ctx,
                num_matches = matches[-1].len;
        } else {
                unsigned min_match_len = LZX_MIN_MATCH_LEN;
-               if (min_match_len <= 2 && !ctx->params.alg_params.slow.use_len2_matches)
-                       min_match_len = 3;
+               if (!ctx->params.alg_params.slow.use_len2_matches)
+                       min_match_len = max(min_match_len, 3);
                const uint32_t max_search_depth = ctx->params.alg_params.slow.max_search_depth;
                const uint32_t max_matches_per_pos = ctx->params.alg_params.slow.max_matches_per_pos;
 
@@ -1622,101 +1654,6 @@ lzx_lz_reverse_near_optimal_match_list(struct lzx_compressor *ctx,
                };
 }
 
-#if 0
-static struct raw_match
-lzx_lz_get_greedy_match(struct lzx_compressor * ctx)
-{
-       struct raw_match *matches;
-
-       if (!lzx_lz_get_matches_caching(ctx, &ctx->queue, &matches))
-               return (struct raw_match) {.len = 0};
-
-       lzx_lz_skip_bytes(ctx, matches[0].len - 1);
-       return matches[0];
-}
-#endif
-
-#if 0
-static struct raw_match
-lzx_lz_get_lazy_match(struct lzx_compressor * ctx)
-{
-       unsigned num_matches;
-       struct raw_match *matches;
-       struct raw_match prev_match;
-       struct lzx_lru_queue queue;
-
-       if (ctx->optimum_cur_idx != ctx->optimum_end_idx)
-               goto retopt;
-
-       /* Check for matches at first position.  */
-       num_matches = lzx_lz_get_matches_caching(ctx, &ctx->queue, &matches);
-
-       /* Return literal if no matches were found.  */
-       if (num_matches == 0)
-               return (struct raw_match) { .len = 0 };
-
-       /* Immediately choose match if longer than threshold.  */
-       if (matches[0].len > ctx->params.alg_params.slow.num_fast_bytes)
-               goto savecur;
-
-       ctx->optimum_cur_idx = ctx->optimum_end_idx = 0;
-       for (;;) {
-               prev_match = matches[0];
-
-               /* Check for matches at next position.  */
-               num_matches = lzx_lz_get_matches_caching(ctx, &ctx->queue, &matches);
-
-               /* Choose previous match if there is not a match at this
-                * position.  */
-               if (num_matches == 0)
-                       goto saveprev;
-
-               /* Choose previous match the longest match at the next position
-                * is the same place, just one character shifted over.  */
-               if (matches[0].offset == prev_match.offset ||
-                   matches[0].len < prev_match.len)
-                       goto saveprev;
-
-               struct lzx_lru_queue q1 = ctx->queue, q2 = ctx->queue;
-               double lazycost = lzx_literal_cost(ctx->window[ctx->match_window_pos - 2],
-                                                    &ctx->costs) +
-                                   lzx_match_cost(matches[0].len, matches[0].offset,
-                                                  &ctx->costs, &q1);
-               double greedycost = lzx_match_cost(prev_match.len, prev_match.offset,
-                                                    &ctx->costs, &q2);
-               lazycost *= (double)prev_match.len / (1 + matches[0].len);
-
-               /* Choose previous match if greedy cost was lower.  */
-               if (greedycost <= lazycost)
-                       goto saveprev;
-
-               /* Choose literal at the previous position.  */
-               ctx->optimum[ctx->optimum_end_idx++].next.link = 0;
-
-
-               /* Immediately choose match if longer than threshold.  */
-               if (matches[0].len > ctx->params.alg_params.slow.num_fast_bytes)
-                       goto savecur;
-       }
-
-savecur:
-       lzx_lz_skip_bytes(ctx, 1);
-       prev_match = matches[0];
-
-saveprev:
-       lzx_lz_skip_bytes(ctx, prev_match.len - 2);
-       ctx->optimum[ctx->optimum_end_idx].next.link = prev_match.len;
-       ctx->optimum[ctx->optimum_end_idx].next.match_offset = prev_match.offset;
-       ctx->optimum_end_idx++;
-retopt:
-       prev_match.len = ctx->optimum[ctx->optimum_cur_idx].next.link;
-       prev_match.offset = ctx->optimum[ctx->optimum_cur_idx].next.match_offset;
-       ctx->optimum_cur_idx++;
-       return prev_match;
-}
-#endif
-
-
 /*
  * lzx_lz_get_near_optimal_match() -
  *
@@ -1731,8 +1668,9 @@ retopt:
  * model rather than simply assuming that longer is better.
  *
  * Still, this is not truly an optimal parser because very long matches are
- * taken immediately.  This is done to avoid considering many different
- * alternatives that are unlikely to significantly be better.
+ * taken immediately, and the raw match-finder takes some shortcuts.  This is
+ * done to avoid considering many different alternatives that are unlikely to
+ * be significantly better.
  *
  * This algorithm is based on that used in 7-Zip's DEFLATE encoder.
  *
@@ -1754,14 +1692,8 @@ retopt:
  *     ctx->optimum         (internal state; leave uninitialized)
  *     ctx->optimum_cur_idx (must set to 0 before first call)
  *     ctx->optimum_end_idx (must set to 0 before first call)
- *     ctx->SA              (must be built before first call)
- *     ctx->ISA             (must be built before first call)
- *     ctx->salink          (must be built before first call)
- *     ctx->match_window_pos (must initialize to position of next match to
- *                            return; subsequent calls return subsequent
- *                            matches)
- *     ctx->match_window_end (must initialize to limit of match-finding region;
- *                            subsequent calls use the same limit)
+ *
+ *     Plus any state used by the raw match-finder.
  *
  * The return value is a (length, offset) pair specifying the match or literal
  * chosen.  For literals, the length is less than LZX_MIN_MATCH_LEN and the
@@ -1975,13 +1907,13 @@ lzx_choose_verbatim_or_aligned(const struct lzx_freqs * freqs,
 }
 
 /* Find a near-optimal sequence of matches/literals with which to output the
- * specified LZX block, and set its type to that which has the minimum cost to
+ * specified LZX block, then set its type to that which has the minimum cost to
  * output.  */
 static void
 lzx_optimize_block(struct lzx_compressor *ctx, struct lzx_block_spec *spec,
                   unsigned num_passes)
 {
-       struct lzx_lru_queue orig_queue = ctx->queue;
+       const struct lzx_lru_queue orig_queue = ctx->queue;
        struct lzx_freqs freqs;
 
        ctx->match_window_end = spec->window_pos + spec->block_size;
@@ -2005,11 +1937,11 @@ lzx_optimize_block(struct lzx_compressor *ctx, struct lzx_block_spec *spec,
 
                        raw_match = lzx_lz_get_near_optimal_match(ctx);
                        if (raw_match.len >= LZX_MIN_MATCH_LEN) {
-                               lzx_match.data = lzx_record_match(raw_match.offset, raw_match.len,
-                                                                 &freqs, &ctx->queue);
+                               lzx_match.data = lzx_tally_match(raw_match.len, raw_match.offset,
+                                                                &freqs, &ctx->queue);
                                i += raw_match.len;
                        } else {
-                               lzx_match.data = lzx_record_literal(ctx->window[i], &freqs);
+                               lzx_match.data = lzx_tally_literal(ctx->window[i], &freqs);
                                i += 1;
                        }
                        ctx->chosen_matches[spec->chosen_matches_start_pos +
@@ -2036,295 +1968,12 @@ lzx_optimize_blocks(struct lzx_compressor *ctx)
                lzx_optimize_block(ctx, &ctx->block_specs[i], num_passes);
 }
 
-static bool entropy_val_tab_inited = false;
-static double entropy_val_tab[LZX_MAX_WINDOW_SIZE];
-static pthread_mutex_t entropy_val_tab_mutex = PTHREAD_MUTEX_INITIALIZER;
-
-static double entropy_val(unsigned count)
-{
-       /*return count * log(count);*/
-       return entropy_val_tab[count];
-}
-
-/* Split a LZX block into several if it is advantageous to do so.
- *
- * TODO:  This doesn't work very well yet.  Should optimal parsing be done
- * before or after splitting?  */
-static void
-lzx_block_split(const u32 matches[restrict],
-               const input_idx_t n,
-               const double epsilon,
-               const unsigned max_num_blocks,
-               const unsigned min_block_len,
-               struct lzx_block_spec block_specs[restrict],
-               unsigned * const restrict num_blocks_ret)
-{
-       const double block_overhead = 1500;
-
-       if (!entropy_val_tab_inited) {
-               pthread_mutex_lock(&entropy_val_tab_mutex);
-               if (!entropy_val_tab_inited) {
-                       entropy_val_tab[0] = 0;
-                       for (input_idx_t i = 1; i < LZX_MAX_WINDOW_SIZE; i++)
-                               entropy_val_tab[i] = i * log2(i);
-                       entropy_val_tab_inited = true;
-               }
-               pthread_mutex_unlock(&entropy_val_tab_mutex);
-       }
-
-       u16 main_syms[n];
-       u8 len_syms[n];
-       u8 aligned_syms[n];
-       input_idx_t orig_input_indices[n + 1];
-
-       LZX_ASSERT(epsilon >= 0);
-       LZX_ASSERT(max_num_blocks >= 1);
-
-       /* For convenience, extract the main, length, and aligned symbols from
-        * the matches.  Every position will have a main symbol, but not every
-        * position will have a length and aligned symbol.  Special values
-        * larger than the valid symbols are used to indicate the absense of a
-        * symbol. */
-       orig_input_indices[0] = 0;
-       for (input_idx_t i = 0, orig_input_idx = 0; i < n; i++) {
-               u32 match = matches[i];
-               u16 main_sym;
-               u8 len_sym = LZX_LENCODE_NUM_SYMBOLS;
-               u8 aligned_sym = LZX_ALIGNEDCODE_NUM_SYMBOLS;
-               if (match & 0x80000000) {
-                       unsigned match_len_minus_2 = match & 0xff;
-                       unsigned position_footer = (match >> 8) & 0x1ffff;
-                       unsigned position_slot = (match >> 25) & 0x3f;
-                       unsigned len_header;
-
-                       if (match_len_minus_2 < LZX_NUM_PRIMARY_LENS) {
-                               len_header = match_len_minus_2;
-                       } else {
-                               len_header = LZX_NUM_PRIMARY_LENS;
-                               len_sym = match_len_minus_2 - LZX_NUM_PRIMARY_LENS;
-                       }
-                       main_sym = ((position_slot << 3) | len_header) + LZX_NUM_CHARS;
-                       if (position_slot >= 8)
-                               aligned_sym = position_footer & 7;
-                       orig_input_idx += match_len_minus_2 + 2;
-               } else {
-                       main_sym = match;
-                       orig_input_idx++;
-               }
-               main_syms[i] = main_sym;
-               len_syms[i] = len_sym;
-               aligned_syms[i] = aligned_sym;
-               orig_input_indices[i + 1] = orig_input_idx;
-       }
-
-       /* Compute the number of sliding windows that will be used for the
-        * entropy calculations. */
-       int num_windows = 0;
-       unsigned window_len;
-       {
-               double e = min_block_len;
-               do {
-                       window_len = e;
-                       num_windows++;
-                       e *= epsilon + 1;
-               } while (window_len < n);
-       }
-
-       /* Compute the length of each sliding window. */
-       unsigned window_lens[num_windows];
-       {
-               double e = min_block_len;
-               unsigned window_idx = 0;
-               do {
-                       window_len = e;
-                       window_lens[window_idx++] = min(window_len, n);
-                       e *= epsilon + 1;
-               } while (window_len < n);
-       }
-
-       /* Best estimated compression size, in bits, found so far for the input
-        * matches up to each position. */
-       unsigned shortest_paths[n + 1];
-
-       /* Pointers to follow to get the sequence of blocks that represents the
-        * shortest path (in terms of estimated compressed size) up to each
-        * position in the input matches. */
-       input_idx_t back_ptrs[n + 1];
-
-       for (input_idx_t i = 0; i < n + 1; i++) {
-               shortest_paths[i] = ~0U;
-               back_ptrs[i] = 0;
-       }
-       shortest_paths[0] = 0;
-
-       {
-               /* Initialize the per-window symbol and entropy counters */
-               input_idx_t mainsym_ctrs[num_windows][LZX_MAINCODE_NUM_SYMBOLS];
-               input_idx_t lensym_ctrs[num_windows][LZX_LENCODE_NUM_SYMBOLS + 1];
-               input_idx_t alignedsym_ctrs[num_windows][LZX_ALIGNEDCODE_NUM_SYMBOLS + 1];
-               ZERO_ARRAY(mainsym_ctrs);
-               ZERO_ARRAY(lensym_ctrs);
-               ZERO_ARRAY(alignedsym_ctrs);
-
-               {
-                       int start_win_idx = 0;
-                       for (input_idx_t i = 0; i < n; i++) {
-                               while (i >= window_lens[start_win_idx])
-                                       start_win_idx++;
-                               for (int j = start_win_idx; j < num_windows; j++) {
-                                       mainsym_ctrs[j][main_syms[i]]++;
-                                       lensym_ctrs[j][len_syms[i]]++;
-                                       alignedsym_ctrs[j][aligned_syms[i]]++;
-                               }
-                       }
-               }
-
-               double entropy_ctrs[num_windows];
-               for (int i = 0; i < num_windows; i++) {
-                       entropy_ctrs[i] = 0;
-                       for (unsigned j = 0; j < LZX_MAINCODE_NUM_SYMBOLS; j++)
-                               entropy_ctrs[i] += entropy_val(mainsym_ctrs[i][j]);
-                       for (unsigned j = 0; j < LZX_LENCODE_NUM_SYMBOLS; j++)
-                               entropy_ctrs[i] += entropy_val(lensym_ctrs[i][j]);
-                       for (unsigned j = 0; j < LZX_ALIGNEDCODE_NUM_SYMBOLS; j++)
-                               entropy_ctrs[i] += entropy_val(alignedsym_ctrs[i][j]);
-               }
-
-               /* Slide the windows along the input and compute the shortest
-                * path to each position in the matches. */
-               int end_window_idx = (int)num_windows - 1;
-               for (input_idx_t i = 0; i < n; i++) {
-                       for (int j = 0; j <= end_window_idx; j++) {
-                               if (shortest_paths[i] == ~0U)
-                                       continue;
-                               unsigned num_mainsyms = window_lens[j];
-                               unsigned num_lensyms = window_lens[j] -
-                                                      lensym_ctrs[j][LZX_LENCODE_NUM_SYMBOLS];
-                               unsigned num_alignedsyms = window_lens[j] -
-                                                          alignedsym_ctrs[j][LZX_ALIGNEDCODE_NUM_SYMBOLS];
-                               unsigned entropy = entropy_val(num_mainsyms) +
-                                                  entropy_val(num_lensyms) +
-                                                  entropy_val(num_alignedsyms) -
-                                                  entropy_ctrs[j];
-                               unsigned est_csize = entropy + block_overhead;
-
-                               unsigned end_idx = i + window_lens[j];
-                               if (est_csize + shortest_paths[i] < shortest_paths[end_idx]) {
-                                       shortest_paths[end_idx] = est_csize + shortest_paths[i];
-                                       back_ptrs[end_idx] = i;
-                               }
-                       }
-                       /* Remove left symbol from windows */
-                       for (int j = 0; j <= end_window_idx; j++) {
-                               input_idx_t orig_maincnt = mainsym_ctrs[j][main_syms[i]]--;
-                               entropy_ctrs[j] -= entropy_val(orig_maincnt);
-                               entropy_ctrs[j] += entropy_val(orig_maincnt - 1);
-
-                               input_idx_t orig_lencnt =
-                                               lensym_ctrs[j][len_syms[i]]--;
-                               if (len_syms[i] != LZX_LENCODE_NUM_SYMBOLS) {
-                                       entropy_ctrs[j] -= entropy_val(orig_lencnt);
-                                       entropy_ctrs[j] += entropy_val(orig_lencnt - 1);
-                               }
-
-                               input_idx_t orig_alignedcnt =
-                                               alignedsym_ctrs[j][aligned_syms[i]]--;
-                               if (aligned_syms[i] != LZX_ALIGNEDCODE_NUM_SYMBOLS) {
-                                       entropy_ctrs[j] -= entropy_val(orig_alignedcnt);
-                                       entropy_ctrs[j] += entropy_val(orig_alignedcnt - 1);
-                               }
-                       }
-
-                       /* Calculate index of longest window remaining */
-                       while (end_window_idx >= 0 && window_lens[end_window_idx] >= n - i)
-                               end_window_idx--;
-
-                       /* Append right symbol to windows */
-                       for (int j = 0; j <= end_window_idx; j++) {
-                               input_idx_t orig_maincnt = mainsym_ctrs[j][
-                                                               main_syms[i + window_lens[j]]]++;
-                               entropy_ctrs[j] -= entropy_val(orig_maincnt);
-                               entropy_ctrs[j] += entropy_val(orig_maincnt + 1);
-
-                               input_idx_t orig_lencnt =
-                                       lensym_ctrs[j][len_syms[i + window_lens[j]]]++;
-                               if (len_syms[i + window_lens[j]] != LZX_LENCODE_NUM_SYMBOLS) {
-                                       entropy_ctrs[j] -= entropy_val(orig_lencnt);
-                                       entropy_ctrs[j] += entropy_val(orig_lencnt + 1);
-                               }
-
-                               input_idx_t orig_alignedcnt =
-                                       alignedsym_ctrs[j][aligned_syms[i + window_lens[j]]]++;
-                               if (aligned_syms[i + window_lens[j]] != LZX_ALIGNEDCODE_NUM_SYMBOLS) {
-                                       entropy_ctrs[j] -= entropy_val(orig_alignedcnt);
-                                       entropy_ctrs[j] += entropy_val(orig_alignedcnt + 1);
-                               }
-                       }
-               }
-       }
-
-#if 0
-       /* If no cost was computed for the first block (due to it being shorter
-        * than all the windows), merge it with the second block. */
-       for (input_idx_t i = n; i != 0; i = back_ptrs[i])
-               if (back_ptrs[i] != 0 && shortest_paths[back_ptrs[i]] == ~0U)
-                       back_ptrs[i] = 0;
-#endif
-
-       /* Calculate number of blocks */
-       input_idx_t num_blocks = 0;
-       for (input_idx_t i = n; i != 0; i = back_ptrs[i])
-               num_blocks++;
-
-       while (num_blocks > max_num_blocks) {
-               LZX_DEBUG("Joining blocks to bring total under max_num_blucks=%u",
-                         max_num_blocks);
-               back_ptrs[n] = back_ptrs[back_ptrs[n]];
-               num_blocks--;
-       }
-
-       LZX_ASSERT(num_blocks != 0);
-
-       /* fill in the 'struct lzx_block_spec' for each block */
-       for (input_idx_t i = n, j = num_blocks - 1; i != 0; i = back_ptrs[i], j--) {
-
-               block_specs[j].chosen_matches_start_pos = back_ptrs[i];
-               block_specs[j].num_chosen_matches = i - back_ptrs[i];
-               block_specs[j].window_pos = orig_input_indices[back_ptrs[i]];
-               block_specs[j].block_size = orig_input_indices[i] -
-                                           orig_input_indices[back_ptrs[i]];
-               /*block_specs[j].est_csize = (shortest_paths[i] -*/
-                                          /*shortest_paths[back_ptrs[i]]) / 8;*/
-
-               LZX_DEBUG("block match_indices [%u, %u) est_csize %u bits\n",
-                         back_ptrs[i], i,
-                         shortest_paths[i] - shortest_paths[back_ptrs[i]]);
-
-               struct lzx_freqs freqs = {};
-
-               for (input_idx_t k = back_ptrs[i]; k < i; k++) {
-                       freqs.main[main_syms[k]]++;
-                       if (len_syms[k] != LZX_LENCODE_NUM_SYMBOLS)
-                               freqs.len[len_syms[k]]++;
-                       if (aligned_syms[k] != LZX_LENCODE_NUM_SYMBOLS)
-                               freqs.aligned[aligned_syms[k]]++;
-               }
-               lzx_make_huffman_codes(&freqs, &block_specs[j].codes);
-
-               block_specs[j].block_type = lzx_choose_verbatim_or_aligned(&freqs,
-                                                                          &block_specs[j].codes);
-       }
-       *num_blocks_ret = num_blocks;
-}
-
-
 /* Initialize the suffix array match-finder for the specified input.  */
 static void
 lzx_lz_init_matchfinder(const u8 T[const restrict],
                        const input_idx_t n,
                        input_idx_t SA[const restrict],
                        input_idx_t ISA[const restrict],
-                       input_idx_t LCP[const restrict],
                        struct salink link[const restrict],
                        const unsigned max_match_len)
 {
@@ -2373,98 +2022,101 @@ lzx_lz_init_matchfinder(const u8 T[const restrict],
        for (input_idx_t r = 0; r < n; r++)
                ISA[SA[r]] = r;
 
-       /* Compute LCP (longest common prefix) array.
-        *
-        * Algorithm adapted from Kasai et al. 2001: "Linear-Time
-        * Longest-Common-Prefix Computation in Suffix Arrays and Its
-        * Applications".  */
        {
-               input_idx_t h = 0;
-               for (input_idx_t i = 0; i < n; i++) {
-                       input_idx_t r = ISA[i];
-                       if (r > 0) {
-                               input_idx_t j = SA[r - 1];
-
-                               input_idx_t lim = min(n - i, n - j);
-
-                               while (h < lim && T[i + h] == T[j + h])
-                                       h++;
-                               LCP[r] = h;
-                               if (h > 0)
-                                       h--;
+               input_idx_t LCP[n];
+               /* Compute LCP (longest common prefix) array.
+                *
+                * Algorithm adapted from Kasai et al. 2001: "Linear-Time
+                * Longest-Common-Prefix Computation in Suffix Arrays and Its
+                * Applications".  */
+               {
+                       input_idx_t h = 0;
+                       for (input_idx_t i = 0; i < n; i++) {
+                               input_idx_t r = ISA[i];
+                               if (r > 0) {
+                                       input_idx_t j = SA[r - 1];
+
+                                       input_idx_t lim = min(n - i, n - j);
+
+                                       while (h < lim && T[i + h] == T[j + h])
+                                               h++;
+                                       LCP[r] = h;
+                                       if (h > 0)
+                                               h--;
+                               }
                        }
                }
-       }
 
-#ifdef ENABLE_LZX_DEBUG
-       /* Verify LCP array.  */
-       for (input_idx_t r = 0; r < n - 1; r++) {
-               LZX_ASSERT(ISA[SA[r]] == r);
-               LZX_ASSERT(ISA[SA[r + 1]] == r + 1);
+       #ifdef ENABLE_LZX_DEBUG
+               /* Verify LCP array.  */
+               for (input_idx_t r = 0; r < n - 1; r++) {
+                       LZX_ASSERT(ISA[SA[r]] == r);
+                       LZX_ASSERT(ISA[SA[r + 1]] == r + 1);
 
-               input_idx_t i1 = SA[r];
-               input_idx_t i2 = SA[r + 1];
-               input_idx_t lcp = LCP[r + 1];
+                       input_idx_t i1 = SA[r];
+                       input_idx_t i2 = SA[r + 1];
+                       input_idx_t lcp = LCP[r + 1];
 
-               input_idx_t n1 = n - i1;
-               input_idx_t n2 = n - i2;
+                       input_idx_t n1 = n - i1;
+                       input_idx_t n2 = n - i2;
 
-               LZX_ASSERT(lcp <= min(n1, n2));
+                       LZX_ASSERT(lcp <= min(n1, n2));
 
-               LZX_ASSERT(memcmp(&T[i1], &T[i2], lcp) == 0);
-               if (lcp < min(n1, n2))
-                       LZX_ASSERT(T[i1 + lcp] != T[i2 + lcp]);
-       }
-#endif /* ENABLE_LZX_DEBUG */
+                       LZX_ASSERT(memcmp(&T[i1], &T[i2], lcp) == 0);
+                       if (lcp < min(n1, n2))
+                               LZX_ASSERT(T[i1 + lcp] != T[i2 + lcp]);
+               }
+       #endif /* ENABLE_LZX_DEBUG */
 
-       /* Compute salink.next and salink.lcpnext.
-        *
-        * Algorithm adapted from Crochemore et al. 2009:
-        * "LPF computation revisited".
-        *
-        * Note: we cap lcpnext to the maximum match length so that the
-        * match-finder need not worry about it later.  */
-       link[n - 1].next = (input_idx_t)~0U;
-       link[n - 1].prev = (input_idx_t)~0U;
-       link[n - 1].lcpnext = 0;
-       link[n - 1].lcpprev = 0;
-       for (input_idx_t r = n - 2; r != (input_idx_t)~0U; r--) {
-               input_idx_t t = r + 1;
-               input_idx_t l = LCP[t];
-               while (t != (input_idx_t)~0 && SA[t] > SA[r]) {
-                       l = min(l, link[t].lcpnext);
-                       t = link[t].next;
+               /* Compute salink.next and salink.lcpnext.
+                *
+                * Algorithm adapted from Crochemore et al. 2009:
+                * "LPF computation revisited".
+                *
+                * Note: we cap lcpnext to the maximum match length so that the
+                * match-finder need not worry about it later.  */
+               link[n - 1].next = (input_idx_t)~0U;
+               link[n - 1].prev = (input_idx_t)~0U;
+               link[n - 1].lcpnext = 0;
+               link[n - 1].lcpprev = 0;
+               for (input_idx_t r = n - 2; r != (input_idx_t)~0U; r--) {
+                       input_idx_t t = r + 1;
+                       input_idx_t l = LCP[t];
+                       while (t != (input_idx_t)~0 && SA[t] > SA[r]) {
+                               l = min(l, link[t].lcpnext);
+                               t = link[t].next;
+                       }
+                       link[r].next = t;
+                       link[r].lcpnext = min(l, max_match_len);
+                       LZX_ASSERT(t == (input_idx_t)~0U || l <= n - SA[t]);
+                       LZX_ASSERT(l <= n - SA[r]);
+                       LZX_ASSERT(memcmp(&T[SA[r]], &T[SA[t]], l) == 0);
                }
-               link[r].next = t;
-               link[r].lcpnext = min(l, max_match_len);
-               LZX_ASSERT(t == (input_idx_t)~0 || l <= n - SA[t]);
-               LZX_ASSERT(l <= n - SA[r]);
-               LZX_ASSERT(memcmp(&T[SA[r]], &T[SA[t]], l) == 0);
-       }
 
-       /* Compute salink.prev and salink.lcpprev.
-        *
-        * Algorithm adapted from Crochemore et al. 2009:
-        * "LPF computation revisited".
-        *
-        * Note: we cap lcpprev to the maximum match length so that the
-        * match-finder need not worry about it later.  */
-       link[0].prev = (input_idx_t)~0;
-       link[0].next = (input_idx_t)~0;
-       link[0].lcpprev = 0;
-       link[0].lcpnext = 0;
-       for (input_idx_t r = 1; r < n; r++) {
-               input_idx_t t = r - 1;
-               input_idx_t l = LCP[r];
-               while (t != (input_idx_t)~0 && SA[t] > SA[r]) {
-                       l = min(l, link[t].lcpprev);
-                       t = link[t].prev;
+               /* Compute salink.prev and salink.lcpprev.
+                *
+                * Algorithm adapted from Crochemore et al. 2009:
+                * "LPF computation revisited".
+                *
+                * Note: we cap lcpprev to the maximum match length so that the
+                * match-finder need not worry about it later.  */
+               link[0].prev = (input_idx_t)~0;
+               link[0].next = (input_idx_t)~0;
+               link[0].lcpprev = 0;
+               link[0].lcpnext = 0;
+               for (input_idx_t r = 1; r < n; r++) {
+                       input_idx_t t = r - 1;
+                       input_idx_t l = LCP[r];
+                       while (t != (input_idx_t)~0 && SA[t] > SA[r]) {
+                               l = min(l, link[t].lcpprev);
+                               t = link[t].prev;
+                       }
+                       link[r].prev = t;
+                       link[r].lcpprev = min(l, max_match_len);
+                       LZX_ASSERT(t == (input_idx_t)~0 || l <= n - SA[t]);
+                       LZX_ASSERT(l <= n - SA[r]);
+                       LZX_ASSERT(memcmp(&T[SA[r]], &T[SA[t]], l) == 0);
                }
-               link[r].prev = t;
-               link[r].lcpprev = min(l, max_match_len);
-               LZX_ASSERT(t == (input_idx_t)~0 || l <= n - SA[t]);
-               LZX_ASSERT(l <= n - SA[r]);
-               LZX_ASSERT(memcmp(&T[SA[r]], &T[SA[t]], l) == 0);
        }
 }
 
@@ -2474,7 +2126,7 @@ lzx_prepare_blocks(struct lzx_compressor * ctx)
 {
        /* Initialize the match-finder.  */
        lzx_lz_init_matchfinder(ctx->window, ctx->window_size,
-                               ctx->SA, ctx->ISA, ctx->LCP, ctx->salink,
+                               ctx->SA, ctx->ISA, ctx->salink,
                                LZX_MAX_MATCH_LEN);
        ctx->cached_matches_pos = 0;
        ctx->matches_cached = false;
@@ -2483,26 +2135,13 @@ lzx_prepare_blocks(struct lzx_compressor * ctx)
        /* Set up a default cost model.  */
        lzx_set_default_costs(&ctx->costs);
 
-       /* Initially assume that the entire input will be one LZX block.  */
-       ctx->block_specs[0].block_type = LZX_BLOCKTYPE_ALIGNED;
+       /* Assume that the entire input will be one LZX block.  */
        ctx->block_specs[0].window_pos = 0;
        ctx->block_specs[0].block_size = ctx->window_size;
        ctx->num_blocks = 1;
 
-       /* Perform near-optimal LZ parsing.  */
+       /* Determine sequence of matches/literals to output for each block.  */
        lzx_optimize_blocks(ctx);
-
-       /* Possibly divide up the LZX block.  */
-       const unsigned max_num_blocks = 1U << ctx->params.alg_params.slow.num_split_passes;
-       if (max_num_blocks > 1) {
-               const double epsilon = 0.2;
-               const unsigned min_block_len = 500;
-
-               lzx_block_split((const u32*)ctx->chosen_matches,
-                               ctx->block_specs[0].num_chosen_matches,
-                               epsilon, max_num_blocks, min_block_len,
-                               ctx->block_specs, &ctx->num_blocks);
-       }
 }
 
 /*
@@ -2516,9 +2155,6 @@ lzx_prepare_blocks(struct lzx_compressor * ctx)
  *     ctx->window[]
  *     ctx->window_size
  *
- * Working space:
- *     ctx->queue
- *
  * Output --- the block specification and the corresponding match/literal data:
  *
  *     ctx->block_specs[]
@@ -2528,8 +2164,7 @@ lzx_prepare_blocks(struct lzx_compressor * ctx)
 static void
 lzx_prepare_block_fast(struct lzx_compressor * ctx)
 {
-       unsigned num_matches;
-       struct lzx_freqs freqs;
+       struct lzx_record_ctx record_ctx;
        struct lzx_block_spec *spec;
 
        /* Parameters to hash chain LZ match finder
@@ -2539,6 +2174,7 @@ lzx_prepare_block_fast(struct lzx_compressor * ctx)
                 * aren't worth choosing when using greedy or lazy parsing.  */
                .min_match      = 3,
                .max_match      = LZX_MAX_MATCH_LEN,
+               .max_offset     = 32768,
                .good_match     = LZX_MAX_MATCH_LEN,
                .nice_match     = LZX_MAX_MATCH_LEN,
                .max_chain_len  = LZX_MAX_MATCH_LEN,
@@ -2547,19 +2183,21 @@ lzx_prepare_block_fast(struct lzx_compressor * ctx)
        };
 
        /* Initialize symbol frequencies and match offset LRU queue.  */
-       memset(&freqs, 0, sizeof(struct lzx_freqs));
-       lzx_lru_queue_init(&ctx->queue);
+       memset(&record_ctx.freqs, 0, sizeof(struct lzx_freqs));
+       lzx_lru_queue_init(&record_ctx.queue);
+       record_ctx.matches = ctx->chosen_matches;
 
        /* Determine series of matches/literals to output.  */
-       num_matches = lz_analyze_block(ctx->window,
-                                      ctx->window_size,
-                                      (u32*)ctx->chosen_matches,
-                                      lzx_record_match,
-                                      lzx_record_literal,
-                                      &freqs,
-                                      &ctx->queue,
-                                      &freqs,
-                                      &lzx_lz_params);
+       {
+               input_idx_t prev_tab[ctx->window_size];
+               lz_analyze_block(ctx->window,
+                                ctx->window_size,
+                                lzx_record_match,
+                                lzx_record_literal,
+                                &record_ctx,
+                                &lzx_lz_params,
+                                prev_tab);
+       }
 
 
        /* Set up block specification.  */
@@ -2567,9 +2205,9 @@ lzx_prepare_block_fast(struct lzx_compressor * ctx)
        spec->block_type = LZX_BLOCKTYPE_ALIGNED;
        spec->window_pos = 0;
        spec->block_size = ctx->window_size;
-       spec->num_chosen_matches = num_matches;
+       spec->num_chosen_matches = (record_ctx.matches - ctx->chosen_matches);
        spec->chosen_matches_start_pos = 0;
-       lzx_make_huffman_codes(&freqs, &spec->codes);
+       lzx_make_huffman_codes(&record_ctx.freqs, &spec->codes);
        ctx->num_blocks = 1;
 }
 
@@ -2616,7 +2254,7 @@ wimlib_lzx_compress2(const void                   * const restrict uncompressed_data,
 {
        struct lzx_compressor *ctx = (struct lzx_compressor*)lzx_ctx;
        struct output_bitstream ostream;
-       unsigned compressed_len;
+       input_idx_t compressed_len;
 
        if (uncompressed_len < 100) {
                LZX_DEBUG("Too small to bother compressing.");
@@ -2663,21 +2301,19 @@ wimlib_lzx_compress2(const void                 * const restrict uncompressed_data,
        lzx_write_all_blocks(ctx, &ostream);
 
        LZX_DEBUG("Flushing bitstream...");
-       if (flush_output_bitstream(&ostream)) {
-               /* If the bitstream cannot be flushed, then the output space was
-                * exhausted.  */
+       compressed_len = flush_output_bitstream(&ostream);
+       if (compressed_len == ~(input_idx_t)0) {
                LZX_DEBUG("Data did not compress to less than original length!");
                return 0;
        }
 
-       /* Compute the length of the compressed data.  */
-       compressed_len = ostream.bit_output - (u8*)compressed_data;
-
        LZX_DEBUG("Done: compressed %u => %u bytes.",
                  uncompressed_len, compressed_len);
 
        /* Verify that we really get the same thing back when decompressing.
-        * TODO: Disable this check by default on the slow algorithm.  */
+        * Although this could be disabled by default in all cases, it only
+        * takes around 2-3% of the running time of the slow algorithm to do the
+        * verification.  */
        if (ctx->params.algorithm == WIMLIB_LZX_ALGORITHM_SLOW
        #if defined(ENABLE_LZX_DEBUG) || defined(ENABLE_VERIFY_COMPRESSION)
            || 1
@@ -2685,11 +2321,10 @@ wimlib_lzx_compress2(const void                 * const restrict uncompressed_data,
            )
        {
                u8 buf[uncompressed_len];
-               int ret;
 
-               ret = wimlib_lzx_decompress(compressed_data, compressed_len,
-                                           buf, uncompressed_len);
-               if (ret) {
+               if (wimlib_lzx_decompress(compressed_data, compressed_len,
+                                         buf, uncompressed_len))
+               {
                        ERROR("Failed to decompress data we "
                              "compressed using LZX algorithm");
                        wimlib_assert(0);
@@ -2759,15 +2394,11 @@ lzx_params_valid(const struct wimlib_lzx_params *params)
                        LZX_DEBUG("Invalid aligned_nostat_cost!");
                        return false;
                }
-
-               if (params->alg_params.slow.num_split_passes > 31) {
-                       LZX_DEBUG("Invalid num_split_passes!");
-                       return false;
-               }
        }
        return true;
 }
 
+/* API function documented in wimlib.h  */
 WIMLIBAPI int
 wimlib_lzx_set_default_params(const struct wimlib_lzx_params * params)
 {
@@ -2810,7 +2441,6 @@ wimlib_lzx_alloc_context(const struct wimlib_lzx_params *params,
                                .use_len2_matches = 1,
                                .num_fast_bytes = 32,
                                .num_optim_passes = 2,
-                               .num_split_passes = 0,
                                .max_search_depth = 50,
                                .max_matches_per_pos = 3,
                                .main_nostat_cost = 15,
@@ -2856,9 +2486,11 @@ wimlib_lzx_alloc_context(const struct wimlib_lzx_params *params,
 
        size_t block_specs_length;
 
+#if 0
        if (params->algorithm == WIMLIB_LZX_ALGORITHM_SLOW)
                block_specs_length = 1U << params->alg_params.slow.num_split_passes;
        else
+#endif
                block_specs_length = 1U;
        ctx->block_specs = MALLOC(block_specs_length * sizeof(ctx->block_specs[0]));
        if (ctx->block_specs == NULL)
@@ -2869,14 +2501,12 @@ wimlib_lzx_alloc_context(const struct wimlib_lzx_params *params,
                if (ctx->SA == NULL)
                        goto err_free_block_specs;
                ctx->ISA = ctx->SA + LZX_MAX_WINDOW_SIZE;
-               ctx->LCP = ctx->ISA + LZX_MAX_WINDOW_SIZE;
                ctx->salink = MALLOC(LZX_MAX_WINDOW_SIZE * sizeof(ctx->salink[0]));
                if (ctx->salink == NULL)
                        goto err_free_SA;
        } else {
                ctx->SA = NULL;
                ctx->ISA = NULL;
-               ctx->LCP = NULL;
                ctx->salink = NULL;
        }
 
@@ -2942,11 +2572,11 @@ wimlib_lzx_free_context(struct wimlib_lzx_context *_ctx)
        struct lzx_compressor *ctx = (struct lzx_compressor*)_ctx;
 
        if (ctx) {
-               FREE(ctx->cached_matches);
                FREE(ctx->chosen_matches);
+               FREE(ctx->cached_matches);
                FREE(ctx->optimum);
-               FREE(ctx->SA);
                FREE(ctx->salink);
+               FREE(ctx->SA);
                FREE(ctx->block_specs);
                FREE(ctx);
        }