43ff24c
From 00464ad8a698fe7735737fab57420f8a44013890 Mon Sep 17 00:00:00 2001
43ff24c
From: Jon Severinsson <jon@severinsson.net>
43ff24c
Date: Tue, 8 Jul 2014 18:29:46 +0200
43ff24c
Subject: [PATCH] journal/compress: improve xz compression performance
43ff24c
43ff24c
The new lzma2 compression options at the top of compress_blob_xz are
43ff24c
equivalent to using preset "0", exept for using a 1 MiB dictionary
43ff24c
(the same as preset "1"). This makes the memory usage at most 7.5 MiB
43ff24c
in the compressor, and 1 MiB in the decompressor, instead of the
43ff24c
previous 92 MiB in the compressor and 8 MiB in the decompressor.
43ff24c
43ff24c
According to test-compress-benchmark this commit makes XZ compression
43ff24c
20 times faster, with no increase in compressed data size.
43ff24c
Using more realistic test data (an ELF binary rather than repeating
43ff24c
ASCII letters 'a' through 'z' in order) it only provides a factor 10
43ff24c
speedup, and at a cost if a 10% increase in compressed data size.
43ff24c
But that is still a worthwhile trade-off.
43ff24c
43ff24c
According to test-compress-benchmark XZ compression is still 25 times
43ff24c
slower than LZ4, but the compressed data is one eighth the size.
43ff24c
Using more realistic test data XZ compression is only 18 times slower
43ff24c
than LZ4, and the compressed data is only one quarter the size.
43ff24c
43ff24c
$ ./test-compress-benchmark
43ff24c
XZ: compressed & decompressed 2535300963 bytes in 42.30s (57.15MiB/s), mean compresion 99.95%, skipped 3570 bytes
43ff24c
LZ4: compressed & decompressed 2535303543 bytes in 1.60s (1510.60MiB/s), mean compresion 99.60%, skipped 990 bytes
43ff24c
43ff24c
(cherry picked from commit 1930eed2a7855d2df06ccf51f9e394428bf547e2)
43ff24c
43ff24c
Conflicts:
43ff24c
	src/journal/compress.c
43ff24c
---
43ff24c
 src/journal/compress.c | 14 ++++++++++++--
43ff24c
 1 file changed, 12 insertions(+), 2 deletions(-)
43ff24c
43ff24c
diff --git a/src/journal/compress.c b/src/journal/compress.c
43ff24c
index 1fc62ead2a..9c0b74c455 100644
43ff24c
--- a/src/journal/compress.c
43ff24c
+++ b/src/journal/compress.c
43ff24c
@@ -30,6 +30,13 @@
43ff24c
 #include "util.h"
43ff24c
 
43ff24c
 bool compress_blob(const void *src, uint64_t src_size, void *dst, uint64_t *dst_size) {
43ff24c
+        static const lzma_options_lzma opt = {
43ff24c
+                1u << 20u, NULL, 0, LZMA_LC_DEFAULT, LZMA_LP_DEFAULT,
43ff24c
+                LZMA_PB_DEFAULT, LZMA_MODE_FAST, 128, LZMA_MF_HC3, 4};
43ff24c
+        static const lzma_filter filters[2] = {
43ff24c
+                {LZMA_FILTER_LZMA2, (lzma_options_lzma*) &opt},
43ff24c
+                {LZMA_VLI_UNKNOWN, NULL}
43ff24c
+        };
43ff24c
         lzma_ret ret;
43ff24c
         size_t out_pos = 0;
43ff24c
 
43ff24c
@@ -41,8 +48,11 @@ bool compress_blob(const void *src, uint64_t src_size, void *dst, uint64_t *dst_
43ff24c
         /* Returns false if we couldn't compress the data or the
43ff24c
          * compressed result is longer than the original */
43ff24c
 
43ff24c
-        ret = lzma_easy_buffer_encode(LZMA_PRESET_DEFAULT, LZMA_CHECK_NONE, NULL,
43ff24c
-                                      src, src_size, dst, &out_pos, src_size);
43ff24c
+        if (src_size < 80)
43ff24c
+                return -ENOBUFS;
43ff24c
+
43ff24c
+        ret = lzma_stream_buffer_encode((lzma_filter*) filters, LZMA_CHECK_NONE, NULL,
43ff24c
+                                        src, src_size, dst, &out_pos, src_size - 1);
43ff24c
         if (ret != LZMA_OK)
43ff24c
                 return false;
43ff24c