gendx / lzma-rs

An LZMA decoder written in pure Rust
MIT License
129 stars 27 forks source link

lzbuffer: add memlimit option #50

Closed cccs-sadugas closed 4 years ago

cccs-sadugas commented 4 years ago

Pull Request Overview

Adds a memlimit configuration option for decompression. If the dict buffer's memory limit is exceeded, decompression will fail with an LZMAError. Additional functions were added to reduce the amount of breaking changes in the library.

Implements #49.

Testing Strategy

This pull request was tested by...

Supporting Documentation and References

cccs-sadugas commented 4 years ago

This idea sounds good. My main concern is how it affects performance, especially when the limit is never reached (or never set). Could you provide some numbers from running cargo bench?

If performance is affected too much, one possibility would be to make this opt-in via a Cargo feature, so that users that don't set a limit are not affected at all in terms of performance. A similar pattern could be used to address #27 by the way.

Here are some benches after moving the memlimit check to the inner block. I'm not sure how accurate they are.

CPU: Intel(R) Core(TM) i7-1065G7 @ 1.30 GHz RAM: 16Gb @ 4267 MHz OS: Docker Windows Ubuntu 20.04 LTS

master

test compress_65536                  ... bench:   2,689,440 ns/iter (+/- 667,102)
test compress_empty                  ... bench:       1,629 ns/iter (+/- 346)
test compress_hello                  ... bench:       2,278 ns/iter (+/- 430)
test decompress_after_compress_65536 ... bench:   3,655,315 ns/iter (+/- 6,067,710)
test decompress_after_compress_empty ... bench:       3,977 ns/iter (+/- 1,797)
test decompress_after_compress_hello ... bench:       4,633 ns/iter (+/- 1,811)
test decompress_big_file             ... bench:   7,079,015 ns/iter (+/- 2,228,350)
test decompress_huge_dict            ... bench:       5,359 ns/iter (+/- 4,065)

memlimit

test compress_65536                  ... bench:   2,665,187 ns/iter (+/- 226,483)
test compress_empty                  ... bench:       1,585 ns/iter (+/- 479)
test compress_hello                  ... bench:       2,206 ns/iter (+/- 689)
test decompress_after_compress_65536 ... bench:   2,845,747 ns/iter (+/- 2,467,223)
test decompress_after_compress_empty ... bench:       3,406 ns/iter (+/- 548)
test decompress_after_compress_hello ... bench:       4,174 ns/iter (+/- 472)
test decompress_big_file             ... bench:   6,802,520 ns/iter (+/- 1,939,560)
test decompress_huge_dict            ... bench:       4,799 ns/iter (+/- 1,594)
gendx commented 4 years ago

My own benchmarks don't show any statistical difference either.