{"thread":{"id":"1501","subject":"[RFC] Improving compression in git network protocols","startedAt":"2005-08-13T16:22:42Z","lastAt":"2005-08-13T16:23:52Z","messageCount":2,"participants":["Sergey Vlasov"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"7195","messageId":"20050813162242.GA11609@procyon.home","threadId":"1501","inReplyTo":null,"subject":"[RFC] Improving compression in git network protocols","fromName":"Sergey Vlasov","fromEmail":"vsu@altlinux.ru","sentAt":"2005-08-13T16:22:42Z","receivedAt":"2005-08-13T16:22:42Z","isPatch":false,"sender":{"key":"vsu@altlinux.ru","avatar":"https://avatars.githubusercontent.com/u/616082?v=4"},"body":"Hello!\n\nThe git pack format has two uses:\n\n1) A space-optimized format for local repository storage.\n\n2) A compact format for transferring repository data over network.\n\nHowever, these uses have some conflicting requirements, and currently\nthe pack format is not as optimal for the task of network transfer as\nit could be.  In particular, because using pack files in a local\nrepository requires random access to the contained objects, all\nobjects in the pack are compressed separately, which negatively\nimpacts the compression rate.\n\nI have made a patch which adds the \"--compression-level=N\" option to\ngit-pack-objects and tried to look what kind of improvement in the\ncompression rate we can get if we compress the whole pack file instead\nof individual objects.  The patch is in a separate message, however,\nI'm not sure if it should be applied immediately - currently there is\nno infrastructure for using this option, and maybe we will choose to\nimplement the same idea in some different way.\n\nHere are the results of my tests:\n\n===========================================================================\n\n1. Packing the whole linux-2.4 repository (13390 objects):\n\n-rw-r--r--  1 vsu 159632105 Aug 13 17:12 pack-0-ef502d3d97088a8b1da4594fda438c268dc5c692.pack\n-rw-r--r--  1 vsu  30878501 Aug 13 17:16 pack-0-ef502d3d97088a8b1da4594fda438c268dc5c692.pack.bz2\n-rw-r--r--  1 vsu  38035157 Aug 13 17:14 pack-0-ef502d3d97088a8b1da4594fda438c268dc5c692.pack.gz\n-rw-r--r--  1 vsu  37739041 Aug 13 17:17 pack-0-ef502d3d97088a8b1da4594fda438c268dc5c692.pack.gz-9\n-rw-r--r--  1 vsu  43035924 Aug 13 17:12 pack-9-ef502d3d97088a8b1da4594fda438c268dc5c692.pack\n-rw-r--r--  1 vsu  43288931 Aug 13 17:11 pack-default-ef502d3d97088a8b1da4594fda438c268dc5c692.pack\n\nThe \"pack-default-*\" file is made without the --compression-level\noption; the \"pack-0-*\" and \"pack-9-*\" files are made with level 0 (no\ncompression) and 9 (max compression) respectively.  From this we can\nsee:\n\n- Using maximum compression for objects in the pack provides little\n  benefit - about 0.6%.\n  \n- Creating a pack with uncompressed objects and compressing it with\n  gzip gives a 12% improvement over the pack with compressed objects.\n  Using \"gzip -9\" at this stage gives about 0.7% more compression.\n\n- For an offline compression, bzip2 could be used instead of gzip -\n  the pack compressed with bzip2 is 28% smaller than the pack with\n  zlib-compressed objects.\n\n2. Packing the whole linux-2.6 repository (67111 objects):\n\n-rw-r--r--  1 vsu 232977930 Aug 13 17:47 pack-0-af8d554c2a184c1ebbaab13a8f844329bcbfe763.pack\n-rw-r--r--  1 vsu  49245784 Aug 13 17:52 pack-0-af8d554c2a184c1ebbaab13a8f844329bcbfe763.pack.bz2\n-rw-r--r--  1 vsu  59767656 Aug 13 17:49 pack-0-af8d554c2a184c1ebbaab13a8f844329bcbfe763.pack.gz\n-rw-r--r--  1 vsu  59323808 Aug 13 17:50 pack-0-af8d554c2a184c1ebbaab13a8f844329bcbfe763.pack.gz-9\n-rw-r--r--  1 vsu  70067732 Aug 13 17:45 pack-9-af8d554c2a184c1ebbaab13a8f844329bcbfe763.pack\n-rw-r--r--  1 vsu  70415173 Aug 13 17:43 pack-default-af8d554c2a184c1ebbaab13a8f844329bcbfe763.pack\n\n- Again, --compression-level=9 does not help much - only 0.5%\n  reduction.\n\n- Using gzip on the pack with uncompressed objects gives 15%\n  improvement over the pack with compressed objects; \"gzip -9\" does\n  not help much.\n\n- The pack with uncompressed objects compressed with bzip2 is 30%\n  smaller than the pack with zlib-compressed objects.\n\n3. Creating an incremental pack for the linux-2.6 repository (743\nobjects):\n\n-rw-r--r--  1 vsu 4270645 Aug 13 17:54 pack-0-8d2c7fe3c00288d4a46fe25a61db35ec965db8a1.pack\n-rw-r--r--  1 vsu 1068277 Aug 13 17:56 pack-0-8d2c7fe3c00288d4a46fe25a61db35ec965db8a1.pack.bz2\n-rw-r--r--  1 vsu 1221308 Aug 13 17:56 pack-0-8d2c7fe3c00288d4a46fe25a61db35ec965db8a1.pack.gz\n-rw-r--r--  1 vsu 1214597 Aug 13 17:56 pack-0-8d2c7fe3c00288d4a46fe25a61db35ec965db8a1.pack.gz-9\n-rw-r--r--  1 vsu 1314817 Aug 13 17:55 pack-9-8d2c7fe3c00288d4a46fe25a61db35ec965db8a1.pack\n-rw-r--r--  1 vsu 1319322 Aug 13 17:54 pack-default-8d2c7fe3c00288d4a46fe25a61db35ec965db8a1.pack\n\n- Once again, --compression-level=9 is next to useless - 0.3%\n  improvement.\n\n- The pack with uncompressed objects compressed with gzip is 7%\n  smaller than the pack with zlib-compressed objects.\n\n- The pack with uncompressed objects compressed with bzip2 is 19%\n  smaller than the pack with zlib-compressed objects.\n\n===========================================================================\n\nAs you see, compressing the pack as a whole can give noticeable\nimprovements (less on smaller files, more on bigger files).  Now we\nneed to find a way to use this:\n\n- For methods which use git tools on both ends (git-clone-pack,\n  git-ssh-pull, git-daemon) we could just create pipes to gzip/gunzip\n  in the appropriate places.\n\n- For non-git-aware methods (rsync, http) we still can use these\n  improvements, but there are additional complications because of the\n  pack index file.  We could have globally-compressed pack files in a\n  separate directory together with their index files, and write an\n  utility which will take a pack file with its index, recompress all\n  objects and produce a pack file with compressed objects and new\n  index.  In theory, we could reconstruct the index from just the pack\n  file alone, but this procedure may be expensive (it will need to\n  reconstruct all objects represented by deltas to find their hash\n  values).\n\nBTW, it could be possible to improve the global compression even more\nby optimizing the order of objects in the pack file (currently trees\nand blobs seems to be intermixed).  I did not try this yet, however.\n\n-- \nSergey Vlasov\n"},{"id":"7196","messageId":"20050813162352.GB11609@procyon.home","threadId":"1501","inReplyTo":"20050813162242.GA11609@procyon.home","subject":"[RFC] [PATCH] Add \"--compression-level=N\" option to git-pack-objects","fromName":"Sergey Vlasov","fromEmail":"vsu@altlinux.ru","sentAt":"2005-08-13T16:23:52Z","receivedAt":"2005-08-13T16:23:52Z","isPatch":true,"sender":{"key":"vsu@altlinux.ru","avatar":"https://avatars.githubusercontent.com/u/616082?v=4"},"body":"[PATCH] Add \"--compression-level=N\" option to git-pack-objects\n\nSetting the compression level for objects in the pack is useful in some\ncases; in particular, disabling compression of the individual objects and\nthen compressing the whole pack can improve the overall compression ratio.\n\nSigned-off-by: Sergey Vlasov <vsu@altlinux.ru>\n---\n\n Documentation/git-pack-objects.txt |   14 +++++++++++++-\n csum-file.c                        |    5 +++--\n csum-file.h                        |    2 +-\n pack-objects.c                     |   15 +++++++++++++--\n 4 files changed, 30 insertions(+), 6 deletions(-)\n\ne54e13c2f057e71998ed39299545717311c7678d\ndiff --git a/Documentation/git-pack-objects.txt b/Documentation/git-pack-objects.txt\n--- a/Documentation/git-pack-objects.txt\n+++ b/Documentation/git-pack-objects.txt\n@@ -9,7 +9,7 @@ git-pack-objects - Create a packed archi\n \n SYNOPSIS\n --------\n-'git-pack-objects' [--incremental] [--window=N] [--depth=N] {--stdout | base-name} < object-list\n+'git-pack-objects' [--incremental] [--window=N] [--depth=N] [--compression-level=N] {--stdout | base-name} < object-list\n \n \n DESCRIPTION\n@@ -61,6 +61,18 @@ base-name::\n \tside, because delta data needs to be applied that many\n \ttimes to get to the necessary object.\n \n+--compression-level::\n+\tSet zlib compression level for the object data.  The\n+\tcompression level is a number from 0 to 9, where 0 means\n+\tno compression, 1 indicates the fastest compression\n+\tmethod (less compression), and 9 indicates the slowest\n+\tcompression method (best compression).  The default\n+\tcompression level is 6.  Setting the compression level\n+\tto 0 may be useful when the pack will be compressed as a\n+\twhole at a later stage (in the pack format every object\n+\tis compressed separately to allow random access, which\n+\tis less efficient than compressing the whole file).\n+\n --incremental::\n \tThis flag causes an object already in a pack ignored\n \teven if it appears in the standard input.\ndiff --git a/csum-file.c b/csum-file.c\n--- a/csum-file.c\n+++ b/csum-file.c\n@@ -117,14 +117,15 @@ struct sha1file *sha1fd(int fd, const ch\n \treturn f;\n }\n \n-int sha1write_compressed(struct sha1file *f, void *in, unsigned int size)\n+int sha1write_compressed(struct sha1file *f, void *in, unsigned int size,\n+\t\t\t int compression_level)\n {\n \tz_stream stream;\n \tunsigned long maxsize;\n \tvoid *out;\n \n \tmemset(&stream, 0, sizeof(stream));\n-\tdeflateInit(&stream, Z_DEFAULT_COMPRESSION);\n+\tdeflateInit(&stream, compression_level);\n \tmaxsize = deflateBound(&stream, size);\n \tout = xmalloc(maxsize);\n \ndiff --git a/csum-file.h b/csum-file.h\n--- a/csum-file.h\n+++ b/csum-file.h\n@@ -14,6 +14,6 @@ extern struct sha1file *sha1fd(int fd, c\n extern struct sha1file *sha1create(const char *fmt, ...) __attribute__((format (printf, 1, 2)));\n extern int sha1close(struct sha1file *, unsigned char *, int);\n extern int sha1write(struct sha1file *, void *, unsigned int);\n-extern int sha1write_compressed(struct sha1file *, void *, unsigned int);\n+extern int sha1write_compressed(struct sha1file *, void *, unsigned int, int);\n \n #endif\ndiff --git a/pack-objects.c b/pack-objects.c\n--- a/pack-objects.c\n+++ b/pack-objects.c\n@@ -5,7 +5,7 @@\n #include \"pack.h\"\n #include \"csum-file.h\"\n \n-static const char pack_usage[] = \"git-pack-objects [--incremental] [--window=N] [--depth=N] {--stdout | base-name} < object-list\";\n+static const char pack_usage[] = \"git-pack-objects [--incremental] [--window=N] [--depth=N] [--compression-level=N] {--stdout | base-name} < object-list\";\n \n struct object_entry {\n \tunsigned char sha1[20];\n@@ -21,6 +21,7 @@ struct object_entry {\n static unsigned char object_list_sha1[20];\n static int non_empty = 0;\n static int incremental = 0;\n+static int compression_level = Z_DEFAULT_COMPRESSION;\n static struct object_entry **sorted_by_sha, **sorted_by_type;\n static struct object_entry *objects = NULL;\n static int nr_objects = 0, nr_alloc = 0;\n@@ -103,7 +104,7 @@ static unsigned long write_object(struct\n \t\tsha1write(f, entry->delta, 20);\n \t\thdrlen += 20;\n \t}\n-\tdatalen = sha1write_compressed(f, buf, size);\n+\tdatalen = sha1write_compressed(f, buf, size, compression_level);\n \tfree(buf);\n \treturn hdrlen + datalen;\n }\n@@ -421,6 +422,16 @@ int main(int argc, char **argv)\n \t\t\t\t\tusage(pack_usage);\n \t\t\t\tcontinue;\n \t\t\t}\n+\t\t\tif (!strncmp(\"--compression-level=\", arg, 20)) {\n+\t\t\t\tchar *end;\n+\t\t\t\tcompression_level = strtoul(arg+20, &end, 0);\n+\t\t\t\tif (!arg[20] || *end)\n+\t\t\t\t\tusage(pack_usage);\n+\t\t\t\tif (compression_level < Z_NO_COMPRESSION\n+\t\t\t\t    || compression_level > Z_BEST_COMPRESSION)\n+\t\t\t\t\tdie(\"invalid compression level\");\n+\t\t\t\tcontinue;\n+\t\t\t}\n \t\t\tif (!strcmp(\"--stdout\", arg)) {\n \t\t\t\tpack_to_stdout = 1;\n \t\t\t\tcontinue;\n"}]}