From c052a188f28d6ded5eae0d98cba6f72f2020fd0e Mon Sep 17 00:00:00 2001 From: Emmanuel Marty Date: Thu, 19 Sep 2019 11:46:03 +0200 Subject: [PATCH] Reduce LZSA2 forward arrivals memory use --- src/shrink_block_v2.c | 13 ------------- src/shrink_context.c | 23 ++++++++++++++--------- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/src/shrink_block_v2.c b/src/shrink_block_v2.c index 5b1dd0e..49bfe3e 100644 --- a/src/shrink_block_v2.c +++ b/src/shrink_block_v2.c @@ -302,22 +302,9 @@ static void lzsa_optimize_forward_v2(lzsa_compressor *pCompressor, const int nSt pCompressor->best_match[i].length = 0; pCompressor->best_match[i].offset = 0; - unsigned int nRepMatchOffset = 0; - int nEndCost = end_arrival->cost; - - int *backward_cost = (int*)pCompressor->pos_data; /* Reuse */ - for (i = nStartOffset; i != nEndOffset; i++) { - backward_cost[i] = nEndCost - arrival[(i << MATCHES_PER_OFFSET_SHIFT) + 0].cost; - } - while (end_arrival->from_slot > 0 && end_arrival->from_pos >= 0) { pCompressor->best_match[end_arrival->from_pos].length = end_arrival->match_len; pCompressor->best_match[end_arrival->from_pos].offset = end_arrival->match_offset; - pCompressor->repmatch_opt[end_arrival->from_pos].expected_repmatch = (end_arrival->match_len >= MIN_MATCH_SIZE_V2 && nRepMatchOffset == end_arrival->match_offset) ? 1 : 0; - - if (end_arrival->match_len >= MIN_MATCH_SIZE_V2) - nRepMatchOffset = end_arrival->match_offset; - end_arrival = &arrival[(end_arrival->from_pos << MATCHES_PER_OFFSET_SHIFT) + (end_arrival->from_slot - 1)]; } } diff --git a/src/shrink_context.c b/src/shrink_context.c index 3eba0f4..1f9b6d7 100644 --- a/src/shrink_context.c +++ b/src/shrink_context.c @@ -92,21 +92,26 @@ int lzsa_compressor_init(lzsa_compressor *pCompressor, const int nMaxWindowSize, if (pCompressor->arrival || (pCompressor->flags & LZSA_FLAG_FAVOR_RATIO) == 0) { if (pCompressor->format_version == 2) { - pCompressor->selected_match = (lzsa_match *)malloc(nMaxWindowSize * NMATCHES_PER_OFFSET * sizeof(lzsa_match)); + pCompressor->best_match = (lzsa_match *)malloc(nMaxWindowSize * sizeof(lzsa_match)); - if (pCompressor->selected_match) { - pCompressor->best_match = (lzsa_match *)malloc(nMaxWindowSize * sizeof(lzsa_match)); + if (pCompressor->best_match) { + if ((pCompressor->flags & LZSA_FLAG_FAVOR_RATIO) == 0) { + pCompressor->selected_match = (lzsa_match *)malloc(nMaxWindowSize * NMATCHES_PER_OFFSET * sizeof(lzsa_match)); - if (pCompressor->best_match) { - pCompressor->slot_cost = (int *)malloc(nMaxWindowSize * NMATCHES_PER_OFFSET * sizeof(int)); + if (pCompressor->selected_match) { + pCompressor->slot_cost = (int *)malloc(nMaxWindowSize * NMATCHES_PER_OFFSET * sizeof(int)); - if (pCompressor->slot_cost) { - pCompressor->repmatch_opt = (lzsa_repmatch_opt *)malloc(nMaxWindowSize * sizeof(lzsa_repmatch_opt)); + if (pCompressor->slot_cost) { + pCompressor->repmatch_opt = (lzsa_repmatch_opt *)malloc(nMaxWindowSize * sizeof(lzsa_repmatch_opt)); - if (pCompressor->repmatch_opt) - return 0; + if (pCompressor->repmatch_opt) + return 0; + } } } + else { + return 0; + } } } else {