[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[PATCH v2 1/2] libdpkg: let backends decide default compression level



When compressing packages with gzip or bzip2, the tradeoff is
clear: a better compression ratio for a distributed package is
generally worth spending some extra time at build time.  Since
better compressed packages are not much more inconvenient to
decompress at all, dpkg defaults to the maximum compression level
and developers rarely need to override that default.

On the other hand, LZ77-based decompressors use more memory at
decompression time for more tightly compressed packages, so the
maximum compression level of "-9" (which uses more than 32 MiB
of memory to decompress) can be too high.

With this patch, instead of defaulting to -9, the compress_cat()
function passes a special preset of "-\0" for the compression
backend to sort out.  All backends handle this as -9 for now; no
change in behavior is intended.

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
 lib/dpkg/compression-backend.c |   21 +++++++++++++++++++++
 lib/dpkg/compression.c         |    7 +++++--
 2 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/lib/dpkg/compression-backend.c b/lib/dpkg/compression-backend.c
index 9bfbcee..8f0c055 100644
--- a/lib/dpkg/compression-backend.c
+++ b/lib/dpkg/compression-backend.c
@@ -23,6 +23,10 @@
 
 #include "compression-backend.h"
 
+static const char default_gz_compression = '9';
+static const char default_bz2_compression = '9';
+static const char default_lzma_compression = '9';
+
 static void fd_fd_filter(int fd_in, int fd_out, const char *desc,
 	const char *file, const char *cmd, const char *argfmt, ...)
 	DPKG_ATTR_NORET DPKG_ATTR_PRINTF(6);
@@ -89,6 +93,7 @@ fd_fd_filter(int fd_in, int fd_out, const char *desc,
 #define COMPRESS(format, zFile, zdopen, zwrite, zclose, zerror, ERR_ERRNO, \
 		fd_in, fd_out, compression, desc) do \
 { \
+	/* If compression == '\0', use library default. */ \
 	char combuf[] = {'w', compression, '\0'}; \
 	int actualread, actualwrite; \
 	char buffer[4096]; \
@@ -119,6 +124,7 @@ static void
 compress_cmd(int fd_in, int fd_out, const char *path,
 	const char *cmd, char compression, const char *desc)
 {
+	/* If compression == '\0', use tool’s default compression level. */
 	fd_fd_filter(fd_in, fd_out, desc, path, cmd, "-c%c", compression);
 }
 
@@ -133,6 +139,9 @@ decompress_gzip(int fd_in, int fd_out, const char *desc)
 void
 compress_gzip(int fd_in, int fd_out, char compression, const char *desc)
 {
+	if (compression == '\0')
+		compression = default_gz_compression;
+
 	COMPRESS("gzip", gzFile, gzdopen, gzwrite, gzclose, gzerror, Z_ERRNO,
 		fd_in, fd_out, compression, desc);
 }
@@ -146,6 +155,9 @@ decompress_gzip(int fd_in, int fd_out, const char *desc)
 void
 compress_gzip(int fd_in, int fd_out, char compression, const char *desc)
 {
+	if (compression == '\0')
+		compression = default_gz_compression;
+
 	compress_cmd(fd_in, fd_out, GZIP, "gzip", compression, desc);
 }
 #endif
@@ -162,6 +174,9 @@ decompress_bzip2(int fd_in, int fd_out, const char *desc)
 void
 compress_bzip2(int fd_in, int fd_out, char compression, const char *desc)
 {
+	if (compression == '\0')
+		compression = default_bz2_compression;
+
 	COMPRESS("bzip2", BZFILE *, BZ2_bzdopen, BZ2_bzwrite, BZ2_bzclose,
 		BZ2_bzerror, BZ_IO_ERROR,
 		fd_in, fd_out, compression, desc);
@@ -176,6 +191,9 @@ decompress_bzip2(int fd_in, int fd_out, const char *desc)
 void
 compress_bzip2(int fd_in, int fd_out, char compression, const char *desc)
 {
+	if (compression == '\0')
+		compression = default_bz2_compression;
+
 	compress_cmd(fd_in, fd_out, BZIP2, "bzip2", compression, desc);
 }
 #endif
@@ -189,6 +207,9 @@ decompress_lzma(int fd_in, int fd_out, const char *desc)
 void
 compress_lzma(int fd_in, int fd_out, char compression, const char *desc)
 {
+	if (compression == '\0')
+		compression = default_lzma_compression;
+
 	compress_cmd(fd_in, fd_out, LZMA, "lzma", compression, desc);
 }
 
diff --git a/lib/dpkg/compression.c b/lib/dpkg/compression.c
index 813526f..c8a6b75 100644
--- a/lib/dpkg/compression.c
+++ b/lib/dpkg/compression.c
@@ -38,8 +38,11 @@ void compress_cat(enum compress_type type, int fd_in, int fd_out, const char *co
   varbufvprintf(&v, desc, al);
   va_end(al);
 
-  if(compression == NULL) compression= "9";
-  else if (*compression == '0')
+  if (compression == NULL)
+    /* Let the backend choose the default compression level. */
+    compression = "\0";
+
+  if (*compression == '0')
     type = compress_type_cat;
 
   switch(type) {
-- 
1.6.5.rc1.199.g596ec


Reply to: