]> wimlib.net Git - wimlib/blobdiff - src/lzms-compress.c
Optimize Huffman code generation
[wimlib] / src / lzms-compress.c
index a696e261a3d331b88084f037af6124f6485f6c0a..3b0caab4a963e75cd60161d2fb5ddf5a05b8ffe2 100644 (file)
@@ -32,6 +32,7 @@
 #endif
 
 #include "wimlib.h"
+#include "wimlib/assert.h"
 #include "wimlib/compiler.h"
 #include "wimlib/compressor_ops.h"
 #include "wimlib/compress_common.h"
@@ -45,8 +46,6 @@
 #include <limits.h>
 #include <pthread.h>
 
-#define LZMS_OPTIM_ARRAY_SIZE  1024
-
 struct lzms_compressor;
 struct lzms_adaptive_state {
        struct lzms_lz_lru_queues lru;
@@ -164,7 +163,7 @@ struct lzms_huffman_encoder {
        u8 lens[LZMS_MAX_NUM_SYMS];
 
        /* The codeword of each symbol in the Huffman code.  */
-       u16 codewords[LZMS_MAX_NUM_SYMS];
+       u32 codewords[LZMS_MAX_NUM_SYMS];
 };
 
 /* State of the LZMS compressor.  */
@@ -535,7 +534,7 @@ lzms_encode_lz_match(struct lzms_compressor *ctx, u32 length, u32 offset)
        /* Main bit: 1 = a match, not a literal.  */
        lzms_range_encode_bit(&ctx->main_range_encoder, 1);
 
-       /* Match bit: 0 = a LZ match, not a delta match.  */
+       /* Match bit: 0 = an LZ match, not a delta match.  */
        lzms_range_encode_bit(&ctx->match_range_encoder, 0);
 
        /* Determine if the offset can be represented as a recent offset.  */
@@ -877,9 +876,10 @@ lzms_get_near_optimal_match(struct lzms_compressor *ctx)
  * - The costs of literals and matches are estimated using the range encoder
  *   states and the semi-adaptive Huffman codes.  Except for range encoding
  *   states, costs are assumed to be constant throughout a single run of the
- *   parsing algorithm, which can parse up to LZMS_OPTIM_ARRAY_SIZE bytes of
- *   data.  This introduces a source of inaccuracy because the probabilities and
- *   Huffman codes can change over this part of the data.
+ *   parsing algorithm, which can parse up to @optim_array_length (from the
+ *   `struct wimlib_lzms_compressor_params') bytes of data.  This introduces a
+ *   source of inaccuracy because the probabilities and Huffman codes can change
+ *   over this part of the data.
  */
 static void
 lzms_encode(struct lzms_compressor *ctx)
@@ -1169,7 +1169,9 @@ lzms_free_compressor(void *_ctx)
 }
 
 static const struct wimlib_lzms_compressor_params lzms_default = {
-       .hdr = sizeof(struct wimlib_lzms_compressor_params),
+       .hdr = {
+               .size = sizeof(struct wimlib_lzms_compressor_params),
+       },
        .min_match_length = 2,
        .max_match_length = UINT32_MAX,
        .nice_match_length = 32,
@@ -1178,6 +1180,9 @@ static const struct wimlib_lzms_compressor_params lzms_default = {
        .optim_array_length = 1024,
 };
 
+static bool
+lzms_params_valid(const struct wimlib_compressor_params_header *);
+
 static const struct wimlib_lzms_compressor_params *
 lzms_get_params(const struct wimlib_compressor_params_header *_params)
 {
@@ -1187,6 +1192,8 @@ lzms_get_params(const struct wimlib_compressor_params_header *_params)
        if (params == NULL)
                params = &lzms_default;
 
+       LZMS_ASSERT(lzms_params_valid(&params->hdr));
+
        return params;
 }
 
@@ -1220,7 +1227,7 @@ lzms_create_compressor(size_t max_block_size,
 
        if (!lz_sarray_init(&ctx->lz_sarray, max_block_size,
                            params->min_match_length,
-                           params->max_match_length,
+                           min(params->max_match_length, LZ_SARRAY_LEN_MAX),
                            params->max_search_depth,
                            params->max_matches_per_pos))
                goto oom;