Skip to content

Reduce LZMA dictionary size for small baskets #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions core/lzma/src/ZipLZMA.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ void R__zipLZMA(int cxlevel, int *srcsize, char *src, int *tgtsize, char *tgt, i
{
uint64_t out_size; /* compressed size */
unsigned in_size = (unsigned) (*srcsize);
uint32_t dict_size_est = in_size/4;
lzma_stream stream = LZMA_STREAM_INIT;
lzma_options_lzma opt_lzma2;
lzma_filter filters[] = {
{ .id = LZMA_FILTER_LZMA2, .options = &opt_lzma2 },
{ .id = LZMA_VLI_UNKNOWN, .options = NULL },
};
lzma_ret returnStatus;

*irep = 0;
Expand All @@ -33,9 +39,24 @@ void R__zipLZMA(int cxlevel, int *srcsize, char *src, int *tgtsize, char *tgt, i
}

if (cxlevel > 9) cxlevel = 9;
returnStatus = lzma_easy_encoder(&stream,
(uint32_t)(cxlevel),
LZMA_CHECK_CRC32);

if (lzma_lzma_preset(&opt_lzma2, cxlevel)) {
return;
}

if (LZMA_DICT_SIZE_MIN > dict_size_est) {
dict_size_est = LZMA_DICT_SIZE_MIN;
}
if (opt_lzma2.dict_size > dict_size_est) {
/* reduce the dictionary size if larger than 1/4 the input size, preset
dictionaries size can be expensively large
*/
opt_lzma2.dict_size = dict_size_est;
}

returnStatus = lzma_stream_encoder(&stream,
filters,
LZMA_CHECK_CRC32);
if (returnStatus != LZMA_OK) {
return;
}
Expand Down