{"thread":{"id":"35562","subject":"[PATCH v4 0/22] pack bitmaps","startedAt":"2013-12-21T13:56:51Z","lastAt":"2014-01-24T02:22:28Z","messageCount":68,"participants":["Jeff King","Thomas Rast","Christian Couder","Erik Faye-Lund","Vicent Martí","Ramsay Jones","Jonathan Nieder","Shawn Pearce","brian m. carlson"],"isPatch":true,"patchVersion":4,"patchTotal":22},"messages":[{"id":"232305","messageId":"20131221135651.GA20818@sigill.intra.peff.net","threadId":"35562","inReplyTo":null,"subject":"[PATCH v4 0/22] pack bitmaps","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-12-21T13:56:51Z","receivedAt":"2013-12-21T13:56:51Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"Here's the v4 re-roll of the pack bitmap series.\n\nThe changes from v3 are:\n\n - reworked add_object_entry refactoring (see patch 11, which is new,\n   and patch 12 which builds on it in a more natural way)\n\n - better error/die reporting from write_reused_pack\n\n - added Ramsay's PRIx64 compat fix\n\n - fixed a user-after-free in the warning message of open_pack_bitmap_1\n\n - minor typo/thinko fixes from Thomas in docs and tests\n\nInterdiff is below.\n\n  [01/23]: sha1write: make buffer const-correct\n  [02/23]: revindex: Export new APIs\n  [03/23]: pack-objects: Refactor the packing list\n  [04/23]: pack-objects: factor out name_hash\n  [05/23]: revision: allow setting custom limiter function\n  [06/23]: sha1_file: export `git_open_noatime`\n  [07/23]: compat: add endianness helpers\n  [08/23]: ewah: compressed bitmap implementation\n  [09/23]: documentation: add documentation for the bitmap format\n  [10/23]: pack-bitmap: add support for bitmap indexes\n  [11/23]: pack-objects: split add_object_entry\n  [12/23]: pack-objects: use bitmaps when packing objects\n  [13/23]: rev-list: add bitmap mode to speed up object lists\n  [14/23]: pack-objects: implement bitmap writing\n  [15/23]: repack: stop using magic number for ARRAY_SIZE(exts)\n  [16/23]: repack: turn exts array into array-of-struct\n  [17/23]: repack: handle optional files created by pack-objects\n  [18/23]: repack: consider bitmaps when performing repacks\n  [19/23]: count-objects: recognize .bitmap in garbage-checking\n  [20/23]: t: add basic bitmap functionality tests\n  [21/23]: t/perf: add tests for pack bitmaps\n  [22/23]: pack-bitmap: implement optional name_hash cache\n  [23/23]: compat/mingw.h: Fix the MinGW and msvc builds\n\n---\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex e6d3922..499a3c4 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -1866,7 +1866,7 @@ pack.useBitmaps::\n \n pack.writebitmaps::\n \tWhen true, git will write a bitmap index when packing all\n-\tobjects to disk (e.g., as when `git repack -a` is run).  This\n+\tobjects to disk (e.g., when `git repack -a` is run).  This\n \tindex can speed up the \"counting objects\" phase of subsequent\n \tpacks created for clones and fetches, at the cost of some disk\n \tspace and extra time spent on the initial repack.  Defaults to\ndiff --git a/builtin/pack-objects.c b/builtin/pack-objects.c\nindex 4504789..fd74197 100644\n--- a/builtin/pack-objects.c\n+++ b/builtin/pack-objects.c\n@@ -72,11 +72,6 @@ static unsigned long cache_max_small_delta_size = 1000;\n \n static unsigned long window_memory_limit = 0;\n \n-enum {\n-\tOBJECT_ENTRY_EXCLUDE = (1 << 0),\n-\tOBJECT_ENTRY_NO_TRY_DELTA = (1 << 1)\n-};\n-\n /*\n  * stats\n  */\n@@ -712,21 +707,20 @@ static struct object_entry **compute_write_order(void)\n \n static off_t write_reused_pack(struct sha1file *f)\n {\n-\tuint8_t buffer[8192];\n+\tunsigned char buffer[8192];\n \toff_t to_write;\n \tint fd;\n \n \tif (!is_pack_valid(reuse_packfile))\n-\t\treturn 0;\n+\t\tdie(\"packfile is invalid: %s\", reuse_packfile->pack_name);\n \n \tfd = git_open_noatime(reuse_packfile->pack_name);\n \tif (fd < 0)\n-\t\treturn 0;\n+\t\tdie_errno(\"unable to open packfile for reuse: %s\",\n+\t\t\t  reuse_packfile->pack_name);\n \n-\tif (lseek(fd, sizeof(struct pack_header), SEEK_SET) == -1) {\n-\t\tclose(fd);\n-\t\treturn 0;\n-\t}\n+\tif (lseek(fd, sizeof(struct pack_header), SEEK_SET) == -1)\n+\t\tdie_errno(\"unable to seek in reused packfile\");\n \n \tif (reuse_packfile_offset < 0)\n \t\treuse_packfile_offset = reuse_packfile->pack_size - 20;\n@@ -736,10 +730,8 @@ static off_t write_reused_pack(struct sha1file *f)\n \twhile (to_write) {\n \t\tint read_pack = xread(fd, buffer, sizeof(buffer));\n \n-\t\tif (read_pack <= 0) {\n-\t\t\tclose(fd);\n-\t\t\treturn 0;\n-\t\t}\n+\t\tif (read_pack <= 0)\n+\t\t\tdie_errno(\"unable to read from reused packfile\");\n \n \t\tif (read_pack > to_write)\n \t\t\tread_pack = to_write;\n@@ -785,9 +777,6 @@ static void write_pack_file(void)\n \t\t\tassert(pack_to_stdout);\n \n \t\t\tpackfile_size = write_reused_pack(f);\n-\t\t\tif (!packfile_size)\n-\t\t\t\tdie_errno(\"failed to re-use existing pack\");\n-\n \t\t\toffset += packfile_size;\n \t\t}\n \n@@ -909,86 +898,143 @@ static int no_try_delta(const char *path)\n \treturn 0;\n }\n \n-static int add_object_entry_1(const unsigned char *sha1, enum object_type type,\n-\t\t\t      int flags, uint32_t name_hash,\n-\t\t\t      struct packed_git *found_pack, off_t found_offset)\n+/*\n+ * When adding an object, check whether we have already added it\n+ * to our packing list. If so, we can skip. However, if we are\n+ * being asked to excludei t, but the previous mention was to include\n+ * it, make sure to adjust its flags and tweak our numbers accordingly.\n+ *\n+ * As an optimization, we pass out the index position where we would have\n+ * found the item, since that saves us from having to look it up again a\n+ * few lines later when we want to add the new entry.\n+ */\n+static int have_duplicate_entry(const unsigned char *sha1,\n+\t\t\t\tint exclude,\n+\t\t\t\tuint32_t *index_pos)\n {\n \tstruct object_entry *entry;\n-\tstruct packed_git *p;\n-\tuint32_t index_pos;\n-\tint exclude = (flags & OBJECT_ENTRY_EXCLUDE);\n-\n-\tentry = packlist_find(&to_pack, sha1, &index_pos);\n-\tif (entry) {\n-\t\tif (exclude) {\n-\t\t\tif (!entry->preferred_base)\n-\t\t\t\tnr_result--;\n-\t\t\tentry->preferred_base = 1;\n-\t\t}\n+\n+\tentry = packlist_find(&to_pack, sha1, index_pos);\n+\tif (!entry)\n \t\treturn 0;\n+\n+\tif (exclude) {\n+\t\tif (!entry->preferred_base)\n+\t\t\tnr_result--;\n+\t\tentry->preferred_base = 1;\n \t}\n \n+\treturn 1;\n+}\n+\n+/*\n+ * Check whether we want the object in the pack (e.g., we do not want\n+ * objects found in non-local stores if the \"--local\" option was used).\n+ *\n+ * As a side effect of this check, we will find the packed version of this\n+ * object, if any. We therefore pass out the pack information to avoid having\n+ * to look it up again later.\n+ */\n+static int want_object_in_pack(const unsigned char *sha1,\n+\t\t\t       int exclude,\n+\t\t\t       struct packed_git **found_pack,\n+\t\t\t       off_t *found_offset)\n+{\n+\tstruct packed_git *p;\n+\n \tif (!exclude && local && has_loose_object_nonlocal(sha1))\n \t\treturn 0;\n \n-\tif (!found_pack) {\n-\t\tfor (p = packed_git; p; p = p->next) {\n-\t\t\toff_t offset = find_pack_entry_one(sha1, p);\n-\t\t\tif (offset) {\n-\t\t\t\tif (!found_pack) {\n-\t\t\t\t\tif (!is_pack_valid(p)) {\n-\t\t\t\t\t\twarning(\"packfile %s cannot be accessed\", p->pack_name);\n-\t\t\t\t\t\tcontinue;\n-\t\t\t\t\t}\n-\t\t\t\t\tfound_offset = offset;\n-\t\t\t\t\tfound_pack = p;\n+\t*found_pack = NULL;\n+\t*found_offset = 0;\n+\n+\tfor (p = packed_git; p; p = p->next) {\n+\t\toff_t offset = find_pack_entry_one(sha1, p);\n+\t\tif (offset) {\n+\t\t\tif (!*found_pack) {\n+\t\t\t\tif (!is_pack_valid(p)) {\n+\t\t\t\t\twarning(\"packfile %s cannot be accessed\", p->pack_name);\n+\t\t\t\t\tcontinue;\n \t\t\t\t}\n-\t\t\t\tif (exclude)\n-\t\t\t\t\tbreak;\n-\t\t\t\tif (incremental)\n-\t\t\t\t\treturn 0;\n-\t\t\t\tif (local && !p->pack_local)\n-\t\t\t\t\treturn 0;\n-\t\t\t\tif (ignore_packed_keep && p->pack_local && p->pack_keep)\n-\t\t\t\t\treturn 0;\n+\t\t\t\t*found_offset = offset;\n+\t\t\t\t*found_pack = p;\n \t\t\t}\n+\t\t\tif (exclude)\n+\t\t\t\treturn 1;\n+\t\t\tif (incremental)\n+\t\t\t\treturn 0;\n+\t\t\tif (local && !p->pack_local)\n+\t\t\t\treturn 0;\n+\t\t\tif (ignore_packed_keep && p->pack_local && p->pack_keep)\n+\t\t\t\treturn 0;\n \t\t}\n \t}\n \n+\treturn 1;\n+}\n+\n+static void create_object_entry(const unsigned char *sha1,\n+\t\t\t\tenum object_type type,\n+\t\t\t\tuint32_t hash,\n+\t\t\t\tint exclude,\n+\t\t\t\tint no_try_delta,\n+\t\t\t\tuint32_t index_pos,\n+\t\t\t\tstruct packed_git *found_pack,\n+\t\t\t\toff_t found_offset)\n+{\n+\tstruct object_entry *entry;\n+\n \tentry = packlist_alloc(&to_pack, sha1, index_pos);\n-\tentry->hash = name_hash;\n+\tentry->hash = hash;\n \tif (type)\n \t\tentry->type = type;\n \tif (exclude)\n \t\tentry->preferred_base = 1;\n \telse\n \t\tnr_result++;\n-\n-\tif (flags & OBJECT_ENTRY_NO_TRY_DELTA)\n-\t\tentry->no_try_delta = 1;\n-\n \tif (found_pack) {\n \t\tentry->in_pack = found_pack;\n \t\tentry->in_pack_offset = found_offset;\n \t}\n \n-\tdisplay_progress(progress_state, to_pack.nr_objects);\n-\n-\treturn 1;\n+\tentry->no_try_delta = no_try_delta;\n }\n \n static int add_object_entry(const unsigned char *sha1, enum object_type type,\n \t\t\t    const char *name, int exclude)\n {\n-\tint flags = 0;\n+\tstruct packed_git *found_pack;\n+\toff_t found_offset;\n+\tuint32_t index_pos;\n \n-\tif (exclude)\n-\t\tflags |= OBJECT_ENTRY_EXCLUDE;\n+\tif (have_duplicate_entry(sha1, exclude, &index_pos))\n+\t\treturn 0;\n+\n+\tif (!want_object_in_pack(sha1, exclude, &found_pack, &found_offset))\n+\t\treturn 0;\n+\n+\tcreate_object_entry(sha1, type, pack_name_hash(name),\n+\t\t\t    exclude, name && no_try_delta(name),\n+\t\t\t    index_pos, found_pack, found_offset);\n \n-\tif (name && no_try_delta(name))\n-\t\tflags |= OBJECT_ENTRY_NO_TRY_DELTA;\n+\tdisplay_progress(progress_state, to_pack.nr_objects);\n+\treturn 1;\n+}\n \n-\treturn add_object_entry_1(sha1, type, flags, pack_name_hash(name), NULL, 0);\n+static int add_object_entry_from_bitmap(const unsigned char *sha1,\n+\t\t\t\t\tenum object_type type,\n+\t\t\t\t\tint flags, uint32_t name_hash,\n+\t\t\t\t\tstruct packed_git *pack, off_t offset)\n+{\n+\tuint32_t index_pos;\n+\n+\tif (have_duplicate_entry(sha1, 0, &index_pos))\n+\t\treturn 0;\n+\n+\tcreate_object_entry(sha1, type, name_hash, 0, 0, index_pos, pack, offset);\n+\n+\tdisplay_progress(progress_state, to_pack.nr_objects);\n+\treturn 1;\n }\n \n struct pbase_tree_cache {\n@@ -2397,7 +2443,7 @@ static int get_object_list_from_bitmap(struct rev_info *revs)\n \t\t}\n \t}\n \n-\ttraverse_bitmap_commit_list(&add_object_entry_1);\n+\ttraverse_bitmap_commit_list(&add_object_entry_from_bitmap);\n \treturn 0;\n }\n \ndiff --git a/pack-bitmap.c b/pack-bitmap.c\nindex 078f7c6..ae0b57b 100644\n--- a/pack-bitmap.c\n+++ b/pack-bitmap.c\n@@ -266,7 +266,7 @@ static int open_pack_bitmap_1(struct packed_git *packfile)\n \t}\n \n \tif (bitmap_git.pack) {\n-\t\twarning(\"ignoring extra bitmap file: %s\", idx_name);\n+\t\twarning(\"ignoring extra bitmap file: %s\", packfile->pack_name);\n \t\tclose(fd);\n \t\treturn -1;\n \t}\ndiff --git a/pack-bitmap.h b/pack-bitmap.h\nindex e4e1a57..8b7f4e9 100644\n--- a/pack-bitmap.h\n+++ b/pack-bitmap.h\n@@ -19,7 +19,7 @@ struct bitmap_disk_header {\n \tunsigned char checksum[20];\n };\n \n-static const char BITMAP_IDX_SIGNATURE[] = {'B', 'I', 'T', 'M'};;\n+static const char BITMAP_IDX_SIGNATURE[] = {'B', 'I', 'T', 'M'};\n \n #define NEEDS_BITMAP (1u<<22)\n \ndiff --git a/t/perf/p5310-pack-bitmaps.sh b/t/perf/p5310-pack-bitmaps.sh\nindex 8811fc4..685d46f 100755\n--- a/t/perf/p5310-pack-bitmaps.sh\n+++ b/t/perf/p5310-pack-bitmaps.sh\n@@ -22,7 +22,7 @@ test_perf 'simulated clone' '\n '\n \n test_perf 'simulated fetch' '\n-\thave=$(git rev-list HEAD --until=1.week.ago -1) &&\n+\thave=$(git rev-list HEAD~100 -1) &&\n \t{\n \t\techo HEAD &&\n \t\techo ^$have\n@@ -31,7 +31,7 @@ test_perf 'simulated fetch' '\n \n test_expect_success 'create partial bitmap state' '\n \t# pick a commit to represent the repo tip in the past\n-\tcutoff=$(git rev-list HEAD --until=1.week.ago -1) &&\n+\tcutoff=$(git rev-list HEAD~100 -1) &&\n \torig_tip=$(git rev-parse HEAD) &&\n \n \t# now kill off all of the refs and pretend we had\ndiff --git a/t/t5310-pack-bitmaps.sh b/t/t5310-pack-bitmaps.sh\nindex 2c2632f..d3a3afa 100755\n--- a/t/t5310-pack-bitmaps.sh\n+++ b/t/t5310-pack-bitmaps.sh\n@@ -127,9 +127,9 @@ test_expect_success JGIT 'we can read jgit bitmaps' '\n '\n \n test_expect_success JGIT 'jgit can read our bitmaps' '\n-\tgit clone . compat-us.git &&\n+\tgit clone . compat-us &&\n \t(\n-\t\tcd compat-us.git &&\n+\t\tcd compat-us &&\n \t\tgit repack -adb &&\n \t\t# jgit gc will barf if it does not like our bitmaps\n \t\tjgit gc\n"},{"id":"232306","messageId":"20131221135926.GA21145@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20131221135651.GA20818@sigill.intra.peff.net","subject":"[PATCH v4 01/23] sha1write: make buffer const-correct","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-12-21T13:59:27Z","receivedAt":"2013-12-21T13:59:27Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"We are passed a \"void *\" and write it out without ever\ntouching it; let's indicate that by using \"const\".\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\n csum-file.c | 6 +++---\n csum-file.h | 2 +-\n 2 files changed, 4 insertions(+), 4 deletions(-)\n\ndiff --git a/csum-file.c b/csum-file.c\nindex 53f5375..465971c 100644\n--- a/csum-file.c\n+++ b/csum-file.c\n@@ -11,7 +11,7 @@\n #include \"progress.h\"\n #include \"csum-file.h\"\n \n-static void flush(struct sha1file *f, void *buf, unsigned int count)\n+static void flush(struct sha1file *f, const void *buf, unsigned int count)\n {\n \tif (0 <= f->check_fd && count)  {\n \t\tunsigned char check_buffer[8192];\n@@ -86,13 +86,13 @@ int sha1close(struct sha1file *f, unsigned char *result, unsigned int flags)\n \treturn fd;\n }\n \n-int sha1write(struct sha1file *f, void *buf, unsigned int count)\n+int sha1write(struct sha1file *f, const void *buf, unsigned int count)\n {\n \twhile (count) {\n \t\tunsigned offset = f->offset;\n \t\tunsigned left = sizeof(f->buffer) - offset;\n \t\tunsigned nr = count > left ? left : count;\n-\t\tvoid *data;\n+\t\tconst void *data;\n \n \t\tif (f->do_crc)\n \t\t\tf->crc32 = crc32(f->crc32, buf, nr);\ndiff --git a/csum-file.h b/csum-file.h\nindex 3b540bd..9dedb03 100644\n--- a/csum-file.h\n+++ b/csum-file.h\n@@ -34,7 +34,7 @@ extern struct sha1file *sha1fd(int fd, const char *name);\n extern struct sha1file *sha1fd_check(const char *name);\n extern struct sha1file *sha1fd_throughput(int fd, const char *name, struct progress *tp);\n extern int sha1close(struct sha1file *, unsigned char *, unsigned int);\n-extern int sha1write(struct sha1file *, void *, unsigned int);\n+extern int sha1write(struct sha1file *, const void *, unsigned int);\n extern void sha1flush(struct sha1file *f);\n extern void crc32_begin(struct sha1file *);\n extern uint32_t crc32_end(struct sha1file *);\n-- \n1.8.5.1.399.g900e7cd\n"},{"id":"232307","messageId":"20131221135932.GB21145@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20131221135651.GA20818@sigill.intra.peff.net","subject":"[PATCH v4 02/23] revindex: Export new APIs","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-12-21T13:59:32Z","receivedAt":"2013-12-21T13:59:32Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"From: Vicent Marti <tanoku@gmail.com>\n\nAllow users to efficiently lookup consecutive entries that are expected\nto be found on the same revindex by exporting `find_revindex_position`:\nthis function takes a pointer to revindex itself, instead of looking up\nthe proper revindex for a given packfile on each call.\n\nSigned-off-by: Vicent Marti <tanoku@gmail.com>\nSigned-off-by: Jeff King <peff@peff.net>\n---\n pack-revindex.c | 38 +++++++++++++++++++++++++-------------\n pack-revindex.h |  8 ++++++++\n 2 files changed, 33 insertions(+), 13 deletions(-)\n\ndiff --git a/pack-revindex.c b/pack-revindex.c\nindex b4d2b35..0bb13b1 100644\n--- a/pack-revindex.c\n+++ b/pack-revindex.c\n@@ -16,11 +16,6 @@\n  * get the object sha1 from the main index.\n  */\n \n-struct pack_revindex {\n-\tstruct packed_git *p;\n-\tstruct revindex_entry *revindex;\n-};\n-\n static struct pack_revindex *pack_revindex;\n static int pack_revindex_hashsz;\n \n@@ -201,15 +196,14 @@ static void create_pack_revindex(struct pack_revindex *rix)\n \tsort_revindex(rix->revindex, num_ent, p->pack_size);\n }\n \n-struct revindex_entry *find_pack_revindex(struct packed_git *p, off_t ofs)\n+struct pack_revindex *revindex_for_pack(struct packed_git *p)\n {\n \tint num;\n-\tunsigned lo, hi;\n \tstruct pack_revindex *rix;\n-\tstruct revindex_entry *revindex;\n \n \tif (!pack_revindex_hashsz)\n \t\tinit_pack_revindex();\n+\n \tnum = pack_revindex_ix(p);\n \tif (num < 0)\n \t\tdie(\"internal error: pack revindex fubar\");\n@@ -217,21 +211,39 @@ struct revindex_entry *find_pack_revindex(struct packed_git *p, off_t ofs)\n \trix = &pack_revindex[num];\n \tif (!rix->revindex)\n \t\tcreate_pack_revindex(rix);\n-\trevindex = rix->revindex;\n \n-\tlo = 0;\n-\thi = p->num_objects + 1;\n+\treturn rix;\n+}\n+\n+int find_revindex_position(struct pack_revindex *pridx, off_t ofs)\n+{\n+\tint lo = 0;\n+\tint hi = pridx->p->num_objects + 1;\n+\tstruct revindex_entry *revindex = pridx->revindex;\n+\n \tdo {\n \t\tunsigned mi = lo + (hi - lo) / 2;\n \t\tif (revindex[mi].offset == ofs) {\n-\t\t\treturn revindex + mi;\n+\t\t\treturn mi;\n \t\t} else if (ofs < revindex[mi].offset)\n \t\t\thi = mi;\n \t\telse\n \t\t\tlo = mi + 1;\n \t} while (lo < hi);\n+\n \terror(\"bad offset for revindex\");\n-\treturn NULL;\n+\treturn -1;\n+}\n+\n+struct revindex_entry *find_pack_revindex(struct packed_git *p, off_t ofs)\n+{\n+\tstruct pack_revindex *pridx = revindex_for_pack(p);\n+\tint pos = find_revindex_position(pridx, ofs);\n+\n+\tif (pos < 0)\n+\t\treturn NULL;\n+\n+\treturn pridx->revindex + pos;\n }\n \n void discard_revindex(void)\ndiff --git a/pack-revindex.h b/pack-revindex.h\nindex 8d5027a..866ca9c 100644\n--- a/pack-revindex.h\n+++ b/pack-revindex.h\n@@ -6,6 +6,14 @@ struct revindex_entry {\n \tunsigned int nr;\n };\n \n+struct pack_revindex {\n+\tstruct packed_git *p;\n+\tstruct revindex_entry *revindex;\n+};\n+\n+struct pack_revindex *revindex_for_pack(struct packed_git *p);\n+int find_revindex_position(struct pack_revindex *pridx, off_t ofs);\n+\n struct revindex_entry *find_pack_revindex(struct packed_git *p, off_t ofs);\n void discard_revindex(void);\n \n-- \n1.8.5.1.399.g900e7cd\n"},{"id":"232309","messageId":"20131221135936.GC21145@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20131221135651.GA20818@sigill.intra.peff.net","subject":"[PATCH v4 03/23] pack-objects: Refactor the packing list","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-12-21T13:59:36Z","receivedAt":"2013-12-21T13:59:36Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"From: Vicent Marti <tanoku@gmail.com>\n\nThe hash table that stores the packing list for a given `pack-objects`\nrun was tightly coupled to the pack-objects code.\n\nIn this commit, we refactor the hash table and the underlying storage\narray into a `packing_data` struct. The functionality for accessing and\nadding entries to the packing list is hence accessible from other parts\nof Git besides the `pack-objects` builtin.\n\nThis refactoring is a requirement for further patches in this series\nthat will require accessing the commit packing list from outside of\n`pack-objects`.\n\nThe hash table implementation has been minimally altered: we now\nuse table sizes which are always a power of two, to ensure a uniform\nindex distribution in the array.\n\nSigned-off-by: Vicent Marti <tanoku@gmail.com>\nSigned-off-by: Jeff King <peff@peff.net>\n---\n Makefile               |   2 +\n builtin/pack-objects.c | 175 +++++++++++--------------------------------------\n pack-objects.c         | 111 +++++++++++++++++++++++++++++++\n pack-objects.h         |  47 +++++++++++++\n 4 files changed, 200 insertions(+), 135 deletions(-)\n create mode 100644 pack-objects.c\n create mode 100644 pack-objects.h\n\ndiff --git a/Makefile b/Makefile\nindex af847f8..48ff0bd 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -694,6 +694,7 @@ LIB_H += notes-merge.h\n LIB_H += notes-utils.h\n LIB_H += notes.h\n LIB_H += object.h\n+LIB_H += pack-objects.h\n LIB_H += pack-revindex.h\n LIB_H += pack.h\n LIB_H += parse-options.h\n@@ -831,6 +832,7 @@ LIB_OBJS += notes-merge.o\n LIB_OBJS += notes-utils.o\n LIB_OBJS += object.o\n LIB_OBJS += pack-check.o\n+LIB_OBJS += pack-objects.o\n LIB_OBJS += pack-revindex.o\n LIB_OBJS += pack-write.o\n LIB_OBJS += pager.o\ndiff --git a/builtin/pack-objects.c b/builtin/pack-objects.c\nindex 36273dd..f3f0cf9 100644\n--- a/builtin/pack-objects.c\n+++ b/builtin/pack-objects.c\n@@ -14,6 +14,7 @@\n #include \"diff.h\"\n #include \"revision.h\"\n #include \"list-objects.h\"\n+#include \"pack-objects.h\"\n #include \"progress.h\"\n #include \"refs.h\"\n #include \"streaming.h\"\n@@ -25,42 +26,15 @@ static const char *pack_usage[] = {\n \tNULL\n };\n \n-struct object_entry {\n-\tstruct pack_idx_entry idx;\n-\tunsigned long size;\t/* uncompressed size */\n-\tstruct packed_git *in_pack; \t/* already in pack */\n-\toff_t in_pack_offset;\n-\tstruct object_entry *delta;\t/* delta base object */\n-\tstruct object_entry *delta_child; /* deltified objects who bases me */\n-\tstruct object_entry *delta_sibling; /* other deltified objects who\n-\t\t\t\t\t     * uses the same base as me\n-\t\t\t\t\t     */\n-\tvoid *delta_data;\t/* cached delta (uncompressed) */\n-\tunsigned long delta_size;\t/* delta data size (uncompressed) */\n-\tunsigned long z_delta_size;\t/* delta data size (compressed) */\n-\tenum object_type type;\n-\tenum object_type in_pack_type;\t/* could be delta */\n-\tuint32_t hash;\t\t\t/* name hint hash */\n-\tunsigned char in_pack_header_size;\n-\tunsigned preferred_base:1; /*\n-\t\t\t\t    * we do not pack this, but is available\n-\t\t\t\t    * to be used as the base object to delta\n-\t\t\t\t    * objects against.\n-\t\t\t\t    */\n-\tunsigned no_try_delta:1;\n-\tunsigned tagged:1; /* near the very tip of refs */\n-\tunsigned filled:1; /* assigned write-order */\n-};\n-\n /*\n- * Objects we are going to pack are collected in objects array (dynamically\n- * expanded).  nr_objects & nr_alloc controls this array.  They are stored\n- * in the order we see -- typically rev-list --objects order that gives us\n- * nice \"minimum seek\" order.\n+ * Objects we are going to pack are collected in the `to_pack` structure.\n+ * It contains an array (dynamically expanded) of the object data, and a map\n+ * that can resolve SHA1s to their position in the array.\n  */\n-static struct object_entry *objects;\n+static struct packing_data to_pack;\n+\n static struct pack_idx_entry **written_list;\n-static uint32_t nr_objects, nr_alloc, nr_result, nr_written;\n+static uint32_t nr_result, nr_written;\n \n static int non_empty;\n static int reuse_delta = 1, reuse_object = 1;\n@@ -90,21 +64,11 @@ static unsigned long cache_max_small_delta_size = 1000;\n static unsigned long window_memory_limit = 0;\n \n /*\n- * The object names in objects array are hashed with this hashtable,\n- * to help looking up the entry by object name.\n- * This hashtable is built after all the objects are seen.\n- */\n-static int *object_ix;\n-static int object_ix_hashsz;\n-static struct object_entry *locate_object_entry(const unsigned char *sha1);\n-\n-/*\n  * stats\n  */\n static uint32_t written, written_delta;\n static uint32_t reused, reused_delta;\n \n-\n static void *get_delta(struct object_entry *entry)\n {\n \tunsigned long size, base_size, delta_size;\n@@ -553,12 +517,12 @@ static int mark_tagged(const char *path, const unsigned char *sha1, int flag,\n \t\t       void *cb_data)\n {\n \tunsigned char peeled[20];\n-\tstruct object_entry *entry = locate_object_entry(sha1);\n+\tstruct object_entry *entry = packlist_find(&to_pack, sha1, NULL);\n \n \tif (entry)\n \t\tentry->tagged = 1;\n \tif (!peel_ref(path, peeled)) {\n-\t\tentry = locate_object_entry(peeled);\n+\t\tentry = packlist_find(&to_pack, peeled, NULL);\n \t\tif (entry)\n \t\t\tentry->tagged = 1;\n \t}\n@@ -633,9 +597,10 @@ static struct object_entry **compute_write_order(void)\n {\n \tunsigned int i, wo_end, last_untagged;\n \n-\tstruct object_entry **wo = xmalloc(nr_objects * sizeof(*wo));\n+\tstruct object_entry **wo = xmalloc(to_pack.nr_objects * sizeof(*wo));\n+\tstruct object_entry *objects = to_pack.objects;\n \n-\tfor (i = 0; i < nr_objects; i++) {\n+\tfor (i = 0; i < to_pack.nr_objects; i++) {\n \t\tobjects[i].tagged = 0;\n \t\tobjects[i].filled = 0;\n \t\tobjects[i].delta_child = NULL;\n@@ -647,7 +612,7 @@ static struct object_entry **compute_write_order(void)\n \t * Make sure delta_sibling is sorted in the original\n \t * recency order.\n \t */\n-\tfor (i = nr_objects; i > 0;) {\n+\tfor (i = to_pack.nr_objects; i > 0;) {\n \t\tstruct object_entry *e = &objects[--i];\n \t\tif (!e->delta)\n \t\t\tcontinue;\n@@ -665,7 +630,7 @@ static struct object_entry **compute_write_order(void)\n \t * Give the objects in the original recency order until\n \t * we see a tagged tip.\n \t */\n-\tfor (i = wo_end = 0; i < nr_objects; i++) {\n+\tfor (i = wo_end = 0; i < to_pack.nr_objects; i++) {\n \t\tif (objects[i].tagged)\n \t\t\tbreak;\n \t\tadd_to_write_order(wo, &wo_end, &objects[i]);\n@@ -675,7 +640,7 @@ static struct object_entry **compute_write_order(void)\n \t/*\n \t * Then fill all the tagged tips.\n \t */\n-\tfor (; i < nr_objects; i++) {\n+\tfor (; i < to_pack.nr_objects; i++) {\n \t\tif (objects[i].tagged)\n \t\t\tadd_to_write_order(wo, &wo_end, &objects[i]);\n \t}\n@@ -683,7 +648,7 @@ static struct object_entry **compute_write_order(void)\n \t/*\n \t * And then all remaining commits and tags.\n \t */\n-\tfor (i = last_untagged; i < nr_objects; i++) {\n+\tfor (i = last_untagged; i < to_pack.nr_objects; i++) {\n \t\tif (objects[i].type != OBJ_COMMIT &&\n \t\t    objects[i].type != OBJ_TAG)\n \t\t\tcontinue;\n@@ -693,7 +658,7 @@ static struct object_entry **compute_write_order(void)\n \t/*\n \t * And then all the trees.\n \t */\n-\tfor (i = last_untagged; i < nr_objects; i++) {\n+\tfor (i = last_untagged; i < to_pack.nr_objects; i++) {\n \t\tif (objects[i].type != OBJ_TREE)\n \t\t\tcontinue;\n \t\tadd_to_write_order(wo, &wo_end, &objects[i]);\n@@ -702,13 +667,13 @@ static struct object_entry **compute_write_order(void)\n \t/*\n \t * Finally all the rest in really tight order\n \t */\n-\tfor (i = last_untagged; i < nr_objects; i++) {\n+\tfor (i = last_untagged; i < to_pack.nr_objects; i++) {\n \t\tif (!objects[i].filled)\n \t\t\tadd_family_to_write_order(wo, &wo_end, &objects[i]);\n \t}\n \n-\tif (wo_end != nr_objects)\n-\t\tdie(\"ordered %u objects, expected %\"PRIu32, wo_end, nr_objects);\n+\tif (wo_end != to_pack.nr_objects)\n+\t\tdie(\"ordered %u objects, expected %\"PRIu32, wo_end, to_pack.nr_objects);\n \n \treturn wo;\n }\n@@ -724,7 +689,7 @@ static void write_pack_file(void)\n \n \tif (progress > pack_to_stdout)\n \t\tprogress_state = start_progress(\"Writing objects\", nr_result);\n-\twritten_list = xmalloc(nr_objects * sizeof(*written_list));\n+\twritten_list = xmalloc(to_pack.nr_objects * sizeof(*written_list));\n \twrite_order = compute_write_order();\n \n \tdo {\n@@ -740,7 +705,7 @@ static void write_pack_file(void)\n \t\tif (!offset)\n \t\t\tdie_errno(\"unable to write pack header\");\n \t\tnr_written = 0;\n-\t\tfor (; i < nr_objects; i++) {\n+\t\tfor (; i < to_pack.nr_objects; i++) {\n \t\t\tstruct object_entry *e = write_order[i];\n \t\t\tif (write_one(f, e, &offset) == WRITE_ONE_BREAK)\n \t\t\t\tbreak;\n@@ -803,7 +768,7 @@ static void write_pack_file(void)\n \t\t\twritten_list[j]->offset = (off_t)-1;\n \t\t}\n \t\tnr_remaining -= nr_written;\n-\t} while (nr_remaining && i < nr_objects);\n+\t} while (nr_remaining && i < to_pack.nr_objects);\n \n \tfree(written_list);\n \tfree(write_order);\n@@ -813,53 +778,6 @@ static void write_pack_file(void)\n \t\t\twritten, nr_result);\n }\n \n-static int locate_object_entry_hash(const unsigned char *sha1)\n-{\n-\tint i;\n-\tunsigned int ui;\n-\tmemcpy(&ui, sha1, sizeof(unsigned int));\n-\ti = ui % object_ix_hashsz;\n-\twhile (0 < object_ix[i]) {\n-\t\tif (!hashcmp(sha1, objects[object_ix[i] - 1].idx.sha1))\n-\t\t\treturn i;\n-\t\tif (++i == object_ix_hashsz)\n-\t\t\ti = 0;\n-\t}\n-\treturn -1 - i;\n-}\n-\n-static struct object_entry *locate_object_entry(const unsigned char *sha1)\n-{\n-\tint i;\n-\n-\tif (!object_ix_hashsz)\n-\t\treturn NULL;\n-\n-\ti = locate_object_entry_hash(sha1);\n-\tif (0 <= i)\n-\t\treturn &objects[object_ix[i]-1];\n-\treturn NULL;\n-}\n-\n-static void rehash_objects(void)\n-{\n-\tuint32_t i;\n-\tstruct object_entry *oe;\n-\n-\tobject_ix_hashsz = nr_objects * 3;\n-\tif (object_ix_hashsz < 1024)\n-\t\tobject_ix_hashsz = 1024;\n-\tobject_ix = xrealloc(object_ix, sizeof(int) * object_ix_hashsz);\n-\tmemset(object_ix, 0, sizeof(int) * object_ix_hashsz);\n-\tfor (i = 0, oe = objects; i < nr_objects; i++, oe++) {\n-\t\tint ix = locate_object_entry_hash(oe->idx.sha1);\n-\t\tif (0 <= ix)\n-\t\t\tcontinue;\n-\t\tix = -1 - ix;\n-\t\tobject_ix[ix] = i + 1;\n-\t}\n-}\n-\n static uint32_t name_hash(const char *name)\n {\n \tuint32_t c, hash = 0;\n@@ -908,13 +826,12 @@ static int add_object_entry(const unsigned char *sha1, enum object_type type,\n \tstruct object_entry *entry;\n \tstruct packed_git *p, *found_pack = NULL;\n \toff_t found_offset = 0;\n-\tint ix;\n \tuint32_t hash = name_hash(name);\n+\tuint32_t index_pos;\n \n-\tix = nr_objects ? locate_object_entry_hash(sha1) : -1;\n-\tif (ix >= 0) {\n+\tentry = packlist_find(&to_pack, sha1, &index_pos);\n+\tif (entry) {\n \t\tif (exclude) {\n-\t\t\tentry = objects + object_ix[ix] - 1;\n \t\t\tif (!entry->preferred_base)\n \t\t\t\tnr_result--;\n \t\t\tentry->preferred_base = 1;\n@@ -947,14 +864,7 @@ static int add_object_entry(const unsigned char *sha1, enum object_type type,\n \t\t}\n \t}\n \n-\tif (nr_objects >= nr_alloc) {\n-\t\tnr_alloc = (nr_alloc  + 1024) * 3 / 2;\n-\t\tobjects = xrealloc(objects, nr_alloc * sizeof(*entry));\n-\t}\n-\n-\tentry = objects + nr_objects++;\n-\tmemset(entry, 0, sizeof(*entry));\n-\thashcpy(entry->idx.sha1, sha1);\n+\tentry = packlist_alloc(&to_pack, sha1, index_pos);\n \tentry->hash = hash;\n \tif (type)\n \t\tentry->type = type;\n@@ -967,12 +877,7 @@ static int add_object_entry(const unsigned char *sha1, enum object_type type,\n \t\tentry->in_pack_offset = found_offset;\n \t}\n \n-\tif (object_ix_hashsz * 3 <= nr_objects * 4)\n-\t\trehash_objects();\n-\telse\n-\t\tobject_ix[-1 - ix] = nr_objects;\n-\n-\tdisplay_progress(progress_state, nr_objects);\n+\tdisplay_progress(progress_state, to_pack.nr_objects);\n \n \tif (name && no_try_delta(name))\n \t\tentry->no_try_delta = 1;\n@@ -1329,7 +1234,7 @@ static void check_object(struct object_entry *entry)\n \t\t\tbreak;\n \t\t}\n \n-\t\tif (base_ref && (base_entry = locate_object_entry(base_ref))) {\n+\t\tif (base_ref && (base_entry = packlist_find(&to_pack, base_ref, NULL))) {\n \t\t\t/*\n \t\t\t * If base_ref was set above that means we wish to\n \t\t\t * reuse delta data, and we even found that base\n@@ -1403,12 +1308,12 @@ static void get_object_details(void)\n \tuint32_t i;\n \tstruct object_entry **sorted_by_offset;\n \n-\tsorted_by_offset = xcalloc(nr_objects, sizeof(struct object_entry *));\n-\tfor (i = 0; i < nr_objects; i++)\n-\t\tsorted_by_offset[i] = objects + i;\n-\tqsort(sorted_by_offset, nr_objects, sizeof(*sorted_by_offset), pack_offset_sort);\n+\tsorted_by_offset = xcalloc(to_pack.nr_objects, sizeof(struct object_entry *));\n+\tfor (i = 0; i < to_pack.nr_objects; i++)\n+\t\tsorted_by_offset[i] = to_pack.objects + i;\n+\tqsort(sorted_by_offset, to_pack.nr_objects, sizeof(*sorted_by_offset), pack_offset_sort);\n \n-\tfor (i = 0; i < nr_objects; i++) {\n+\tfor (i = 0; i < to_pack.nr_objects; i++) {\n \t\tstruct object_entry *entry = sorted_by_offset[i];\n \t\tcheck_object(entry);\n \t\tif (big_file_threshold < entry->size)\n@@ -2034,7 +1939,7 @@ static int add_ref_tag(const char *path, const unsigned char *sha1, int flag, vo\n \n \tif (!prefixcmp(path, \"refs/tags/\") && /* is a tag? */\n \t    !peel_ref(path, peeled)        && /* peelable? */\n-\t    locate_object_entry(peeled))      /* object packed? */\n+\t    packlist_find(&to_pack, peeled, NULL))      /* object packed? */\n \t\tadd_object_entry(sha1, OBJ_TAG, NULL, 0);\n \treturn 0;\n }\n@@ -2057,14 +1962,14 @@ static void prepare_pack(int window, int depth)\n \tif (!pack_to_stdout)\n \t\tdo_check_packed_object_crc = 1;\n \n-\tif (!nr_objects || !window || !depth)\n+\tif (!to_pack.nr_objects || !window || !depth)\n \t\treturn;\n \n-\tdelta_list = xmalloc(nr_objects * sizeof(*delta_list));\n+\tdelta_list = xmalloc(to_pack.nr_objects * sizeof(*delta_list));\n \tnr_deltas = n = 0;\n \n-\tfor (i = 0; i < nr_objects; i++) {\n-\t\tstruct object_entry *entry = objects + i;\n+\tfor (i = 0; i < to_pack.nr_objects; i++) {\n+\t\tstruct object_entry *entry = to_pack.objects + i;\n \n \t\tif (entry->delta)\n \t\t\t/* This happens if we decided to reuse existing\n@@ -2342,7 +2247,7 @@ static void loosen_unused_packed_objects(struct rev_info *revs)\n \n \t\tfor (i = 0; i < p->num_objects; i++) {\n \t\t\tsha1 = nth_packed_object_sha1(p, i);\n-\t\t\tif (!locate_object_entry(sha1) &&\n+\t\t\tif (!packlist_find(&to_pack, sha1, NULL) &&\n \t\t\t\t!has_sha1_pack_kept_or_nonlocal(sha1))\n \t\t\t\tif (force_object_loose(sha1, p->mtime))\n \t\t\t\t\tdie(\"unable to force loose object\");\ndiff --git a/pack-objects.c b/pack-objects.c\nnew file mode 100644\nindex 0000000..d01d851\n--- /dev/null\n+++ b/pack-objects.c\n@@ -0,0 +1,111 @@\n+#include \"cache.h\"\n+#include \"object.h\"\n+#include \"pack.h\"\n+#include \"pack-objects.h\"\n+\n+static uint32_t locate_object_entry_hash(struct packing_data *pdata,\n+\t\t\t\t\t const unsigned char *sha1,\n+\t\t\t\t\t int *found)\n+{\n+\tuint32_t i, hash, mask = (pdata->index_size - 1);\n+\n+\tmemcpy(&hash, sha1, sizeof(uint32_t));\n+\ti = hash & mask;\n+\n+\twhile (pdata->index[i] > 0) {\n+\t\tuint32_t pos = pdata->index[i] - 1;\n+\n+\t\tif (!hashcmp(sha1, pdata->objects[pos].idx.sha1)) {\n+\t\t\t*found = 1;\n+\t\t\treturn i;\n+\t\t}\n+\n+\t\ti = (i + 1) & mask;\n+\t}\n+\n+\t*found = 0;\n+\treturn i;\n+}\n+\n+static inline uint32_t closest_pow2(uint32_t v)\n+{\n+\tv = v - 1;\n+\tv |= v >> 1;\n+\tv |= v >> 2;\n+\tv |= v >> 4;\n+\tv |= v >> 8;\n+\tv |= v >> 16;\n+\treturn v + 1;\n+}\n+\n+static void rehash_objects(struct packing_data *pdata)\n+{\n+\tuint32_t i;\n+\tstruct object_entry *entry;\n+\n+\tpdata->index_size = closest_pow2(pdata->nr_objects * 3);\n+\tif (pdata->index_size < 1024)\n+\t\tpdata->index_size = 1024;\n+\n+\tpdata->index = xrealloc(pdata->index, sizeof(uint32_t) * pdata->index_size);\n+\tmemset(pdata->index, 0, sizeof(int) * pdata->index_size);\n+\n+\tentry = pdata->objects;\n+\n+\tfor (i = 0; i < pdata->nr_objects; i++) {\n+\t\tint found;\n+\t\tuint32_t ix = locate_object_entry_hash(pdata, entry->idx.sha1, &found);\n+\n+\t\tif (found)\n+\t\t\tdie(\"BUG: Duplicate object in hash\");\n+\n+\t\tpdata->index[ix] = i + 1;\n+\t\tentry++;\n+\t}\n+}\n+\n+struct object_entry *packlist_find(struct packing_data *pdata,\n+\t\t\t\t   const unsigned char *sha1,\n+\t\t\t\t   uint32_t *index_pos)\n+{\n+\tuint32_t i;\n+\tint found;\n+\n+\tif (!pdata->index_size)\n+\t\treturn NULL;\n+\n+\ti = locate_object_entry_hash(pdata, sha1, &found);\n+\n+\tif (index_pos)\n+\t\t*index_pos = i;\n+\n+\tif (!found)\n+\t\treturn NULL;\n+\n+\treturn &pdata->objects[pdata->index[i] - 1];\n+}\n+\n+struct object_entry *packlist_alloc(struct packing_data *pdata,\n+\t\t\t\t    const unsigned char *sha1,\n+\t\t\t\t    uint32_t index_pos)\n+{\n+\tstruct object_entry *new_entry;\n+\n+\tif (pdata->nr_objects >= pdata->nr_alloc) {\n+\t\tpdata->nr_alloc = (pdata->nr_alloc  + 1024) * 3 / 2;\n+\t\tpdata->objects = xrealloc(pdata->objects,\n+\t\t\t\t\t  pdata->nr_alloc * sizeof(*new_entry));\n+\t}\n+\n+\tnew_entry = pdata->objects + pdata->nr_objects++;\n+\n+\tmemset(new_entry, 0, sizeof(*new_entry));\n+\thashcpy(new_entry->idx.sha1, sha1);\n+\n+\tif (pdata->index_size * 3 <= pdata->nr_objects * 4)\n+\t\trehash_objects(pdata);\n+\telse\n+\t\tpdata->index[index_pos] = pdata->nr_objects;\n+\n+\treturn new_entry;\n+}\ndiff --git a/pack-objects.h b/pack-objects.h\nnew file mode 100644\nindex 0000000..f528215\n--- /dev/null\n+++ b/pack-objects.h\n@@ -0,0 +1,47 @@\n+#ifndef PACK_OBJECTS_H\n+#define PACK_OBJECTS_H\n+\n+struct object_entry {\n+\tstruct pack_idx_entry idx;\n+\tunsigned long size;\t/* uncompressed size */\n+\tstruct packed_git *in_pack;\t/* already in pack */\n+\toff_t in_pack_offset;\n+\tstruct object_entry *delta;\t/* delta base object */\n+\tstruct object_entry *delta_child; /* deltified objects who bases me */\n+\tstruct object_entry *delta_sibling; /* other deltified objects who\n+\t\t\t\t\t     * uses the same base as me\n+\t\t\t\t\t     */\n+\tvoid *delta_data;\t/* cached delta (uncompressed) */\n+\tunsigned long delta_size;\t/* delta data size (uncompressed) */\n+\tunsigned long z_delta_size;\t/* delta data size (compressed) */\n+\tenum object_type type;\n+\tenum object_type in_pack_type;\t/* could be delta */\n+\tuint32_t hash;\t\t\t/* name hint hash */\n+\tunsigned char in_pack_header_size;\n+\tunsigned preferred_base:1; /*\n+\t\t\t\t    * we do not pack this, but is available\n+\t\t\t\t    * to be used as the base object to delta\n+\t\t\t\t    * objects against.\n+\t\t\t\t    */\n+\tunsigned no_try_delta:1;\n+\tunsigned tagged:1; /* near the very tip of refs */\n+\tunsigned filled:1; /* assigned write-order */\n+};\n+\n+struct packing_data {\n+\tstruct object_entry *objects;\n+\tuint32_t nr_objects, nr_alloc;\n+\n+\tint32_t *index;\n+\tuint32_t index_size;\n+};\n+\n+struct object_entry *packlist_alloc(struct packing_data *pdata,\n+\t\t\t\t    const unsigned char *sha1,\n+\t\t\t\t    uint32_t index_pos);\n+\n+struct object_entry *packlist_find(struct packing_data *pdata,\n+\t\t\t\t   const unsigned char *sha1,\n+\t\t\t\t   uint32_t *index_pos);\n+\n+#endif\n-- \n1.8.5.1.399.g900e7cd\n"},{"id":"232308","messageId":"20131221135939.GD21145@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20131221135651.GA20818@sigill.intra.peff.net","subject":"[PATCH v4 04/23] pack-objects: factor out name_hash","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-12-21T13:59:39Z","receivedAt":"2013-12-21T13:59:39Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"From: Vicent Marti <tanoku@gmail.com>\n\nAs the pack-objects system grows beyond the single\npack-objects.c file, more parts (like the soon-to-exist\nbitmap code) will need to compute hashes for matching\ndeltas. Factor out name_hash to make it available to other\nfiles.\n\nSigned-off-by: Vicent Marti <tanoku@gmail.com>\nSigned-off-by: Jeff King <peff@peff.net>\n---\n builtin/pack-objects.c | 24 ++----------------------\n pack-objects.h         | 20 ++++++++++++++++++++\n 2 files changed, 22 insertions(+), 22 deletions(-)\n\ndiff --git a/builtin/pack-objects.c b/builtin/pack-objects.c\nindex f3f0cf9..faf746b 100644\n--- a/builtin/pack-objects.c\n+++ b/builtin/pack-objects.c\n@@ -778,26 +778,6 @@ static void write_pack_file(void)\n \t\t\twritten, nr_result);\n }\n \n-static uint32_t name_hash(const char *name)\n-{\n-\tuint32_t c, hash = 0;\n-\n-\tif (!name)\n-\t\treturn 0;\n-\n-\t/*\n-\t * This effectively just creates a sortable number from the\n-\t * last sixteen non-whitespace characters. Last characters\n-\t * count \"most\", so things that end in \".c\" sort together.\n-\t */\n-\twhile ((c = *name++) != 0) {\n-\t\tif (isspace(c))\n-\t\t\tcontinue;\n-\t\thash = (hash >> 2) + (c << 24);\n-\t}\n-\treturn hash;\n-}\n-\n static void setup_delta_attr_check(struct git_attr_check *check)\n {\n \tstatic struct git_attr *attr_delta;\n@@ -826,7 +806,7 @@ static int add_object_entry(const unsigned char *sha1, enum object_type type,\n \tstruct object_entry *entry;\n \tstruct packed_git *p, *found_pack = NULL;\n \toff_t found_offset = 0;\n-\tuint32_t hash = name_hash(name);\n+\tuint32_t hash = pack_name_hash(name);\n \tuint32_t index_pos;\n \n \tentry = packlist_find(&to_pack, sha1, &index_pos);\n@@ -1082,7 +1062,7 @@ static void add_preferred_base_object(const char *name)\n {\n \tstruct pbase_tree *it;\n \tint cmplen;\n-\tunsigned hash = name_hash(name);\n+\tunsigned hash = pack_name_hash(name);\n \n \tif (!num_preferred_base || check_pbase_path(hash))\n \t\treturn;\ndiff --git a/pack-objects.h b/pack-objects.h\nindex f528215..90ad0a8 100644\n--- a/pack-objects.h\n+++ b/pack-objects.h\n@@ -44,4 +44,24 @@ struct object_entry *packlist_find(struct packing_data *pdata,\n \t\t\t\t   const unsigned char *sha1,\n \t\t\t\t   uint32_t *index_pos);\n \n+static inline uint32_t pack_name_hash(const char *name)\n+{\n+\tuint32_t c, hash = 0;\n+\n+\tif (!name)\n+\t\treturn 0;\n+\n+\t/*\n+\t * This effectively just creates a sortable number from the\n+\t * last sixteen non-whitespace characters. Last characters\n+\t * count \"most\", so things that end in \".c\" sort together.\n+\t */\n+\twhile ((c = *name++) != 0) {\n+\t\tif (isspace(c))\n+\t\t\tcontinue;\n+\t\thash = (hash >> 2) + (c << 24);\n+\t}\n+\treturn hash;\n+}\n+\n #endif\n-- \n1.8.5.1.399.g900e7cd\n"},{"id":"232310","messageId":"20131221135943.GE21145@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20131221135651.GA20818@sigill.intra.peff.net","subject":"[PATCH v4 05/23] revision: allow setting custom limiter function","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-12-21T13:59:43Z","receivedAt":"2013-12-21T13:59:43Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"From: Vicent Marti <tanoku@gmail.com>\n\nThis commit enables users of `struct rev_info` to peform custom limiting\nduring a revision walk (i.e. `get_revision`).\n\nIf the field `include_check` has been set to a callback, this callback\nwill be issued once for each commit before it is added to the \"pending\"\nlist of the revwalk. If the include check returns 0, the commit will be\nmarked as added but won't be pushed to the pending list, effectively\nlimiting the walk.\n\nSigned-off-by: Vicent Marti <tanoku@gmail.com>\nSigned-off-by: Jeff King <peff@peff.net>\n---\n revision.c | 4 ++++\n revision.h | 2 ++\n 2 files changed, 6 insertions(+)\n\ndiff --git a/revision.c b/revision.c\nindex 0173e01..cddd605 100644\n--- a/revision.c\n+++ b/revision.c\n@@ -779,6 +779,10 @@ static int add_parents_to_list(struct rev_info *revs, struct commit *commit,\n \t\treturn 0;\n \tcommit->object.flags |= ADDED;\n \n+\tif (revs->include_check &&\n+\t    !revs->include_check(commit, revs->include_check_data))\n+\t\treturn 0;\n+\n \t/*\n \t * If the commit is uninteresting, don't try to\n \t * prune parents - we want the maximal uninteresting\ndiff --git a/revision.h b/revision.h\nindex e7f1d21..9957f3c 100644\n--- a/revision.h\n+++ b/revision.h\n@@ -168,6 +168,8 @@ struct rev_info {\n \tunsigned long min_age;\n \tint min_parents;\n \tint max_parents;\n+\tint (*include_check)(struct commit *, void *);\n+\tvoid *include_check_data;\n \n \t/* diff info for patches and for paths limiting */\n \tstruct diff_options diffopt;\n-- \n1.8.5.1.399.g900e7cd\n"},{"id":"232312","messageId":"20131221135946.GF21145@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20131221135651.GA20818@sigill.intra.peff.net","subject":"[PATCH v4 06/23] sha1_file: export `git_open_noatime`","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-12-21T13:59:47Z","receivedAt":"2013-12-21T13:59:47Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"From: Vicent Marti <tanoku@gmail.com>\n\nThe `git_open_noatime` helper can be of general interest for other\nconsumers of git's different on-disk formats.\n\nSigned-off-by: Vicent Marti <tanoku@gmail.com>\nSigned-off-by: Jeff King <peff@peff.net>\n---\n cache.h     | 1 +\n sha1_file.c | 4 +---\n 2 files changed, 2 insertions(+), 3 deletions(-)\n\ndiff --git a/cache.h b/cache.h\nindex 5e3fc72..f2e5aa7 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -780,6 +780,7 @@ extern int hash_sha1_file(const void *buf, unsigned long len, const char *type,\n extern int write_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *return_sha1);\n extern int pretend_sha1_file(void *, unsigned long, enum object_type, unsigned char *);\n extern int force_object_loose(const unsigned char *sha1, time_t mtime);\n+extern int git_open_noatime(const char *name);\n extern void *map_sha1_file(const unsigned char *sha1, unsigned long *size);\n extern int unpack_sha1_header(git_zstream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz);\n extern int parse_sha1_header(const char *hdr, unsigned long *sizep);\ndiff --git a/sha1_file.c b/sha1_file.c\nindex f80bbe4..4714bd8 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -239,8 +239,6 @@ char *sha1_pack_index_name(const unsigned char *sha1)\n struct alternate_object_database *alt_odb_list;\n static struct alternate_object_database **alt_odb_tail;\n \n-static int git_open_noatime(const char *name);\n-\n /*\n  * Prepare alternate object database registry.\n  *\n@@ -1357,7 +1355,7 @@ int check_sha1_signature(const unsigned char *sha1, void *map,\n \treturn hashcmp(sha1, real_sha1) ? -1 : 0;\n }\n \n-static int git_open_noatime(const char *name)\n+int git_open_noatime(const char *name)\n {\n \tstatic int sha1_file_open_flag = O_NOATIME;\n \n-- \n1.8.5.1.399.g900e7cd\n"},{"id":"232311","messageId":"20131221135950.GG21145@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20131221135651.GA20818@sigill.intra.peff.net","subject":"[PATCH v4 07/23] compat: add endianness helpers","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-12-21T13:59:50Z","receivedAt":"2013-12-21T13:59:50Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"From: Vicent Marti <tanoku@gmail.com>\n\nThe POSIX standard doesn't currently define a `ntohll`/`htonll`\nfunction pair to perform network-to-host and host-to-network\nswaps of 64-bit data. These 64-bit swaps are necessary for the on-disk\nstorage of EWAH bitmaps if they are not in native byte order.\n\nMany thanks to Ramsay Jones <ramsay@ramsay1.demon.co.uk> and\nTorsten Bögershausen <tboegi@web.de> for cygwin/mingw/msvc\nportability fixes.\n\nSigned-off-by: Vicent Marti <tanoku@gmail.com>\nSigned-off-by: Jeff King <peff@peff.net>\n---\n compat/bswap.h | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-\n 1 file changed, 75 insertions(+), 1 deletion(-)\n\ndiff --git a/compat/bswap.h b/compat/bswap.h\nindex 5061214..c18a78e 100644\n--- a/compat/bswap.h\n+++ b/compat/bswap.h\n@@ -17,7 +17,20 @@ static inline uint32_t default_swab32(uint32_t val)\n \t\t((val & 0x000000ff) << 24));\n }\n \n+static inline uint64_t default_bswap64(uint64_t val)\n+{\n+\treturn (((val & (uint64_t)0x00000000000000ffULL) << 56) |\n+\t\t((val & (uint64_t)0x000000000000ff00ULL) << 40) |\n+\t\t((val & (uint64_t)0x0000000000ff0000ULL) << 24) |\n+\t\t((val & (uint64_t)0x00000000ff000000ULL) <<  8) |\n+\t\t((val & (uint64_t)0x000000ff00000000ULL) >>  8) |\n+\t\t((val & (uint64_t)0x0000ff0000000000ULL) >> 24) |\n+\t\t((val & (uint64_t)0x00ff000000000000ULL) >> 40) |\n+\t\t((val & (uint64_t)0xff00000000000000ULL) >> 56));\n+}\n+\n #undef bswap32\n+#undef bswap64\n \n #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))\n \n@@ -32,15 +45,42 @@ static inline uint32_t git_bswap32(uint32_t x)\n \treturn result;\n }\n \n+#define bswap64 git_bswap64\n+#if defined(__x86_64__)\n+static inline uint64_t git_bswap64(uint64_t x)\n+{\n+\tuint64_t result;\n+\tif (__builtin_constant_p(x))\n+\t\tresult = default_bswap64(x);\n+\telse\n+\t\t__asm__(\"bswap %q0\" : \"=r\" (result) : \"0\" (x));\n+\treturn result;\n+}\n+#else\n+static inline uint64_t git_bswap64(uint64_t x)\n+{\n+\tunion { uint64_t i64; uint32_t i32[2]; } tmp, result;\n+\tif (__builtin_constant_p(x))\n+\t\tresult.i64 = default_bswap64(x);\n+\telse {\n+\t\ttmp.i64 = x;\n+\t\tresult.i32[0] = git_bswap32(tmp.i32[1]);\n+\t\tresult.i32[1] = git_bswap32(tmp.i32[0]);\n+\t}\n+\treturn result.i64;\n+}\n+#endif\n+\n #elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))\n \n #include <stdlib.h>\n \n #define bswap32(x) _byteswap_ulong(x)\n+#define bswap64(x) _byteswap_uint64(x)\n \n #endif\n \n-#ifdef bswap32\n+#if defined(bswap32)\n \n #undef ntohl\n #undef htonl\n@@ -48,3 +88,37 @@ static inline uint32_t git_bswap32(uint32_t x)\n #define htonl(x) bswap32(x)\n \n #endif\n+\n+#if defined(bswap64)\n+\n+#undef ntohll\n+#undef htonll\n+#define ntohll(x) bswap64(x)\n+#define htonll(x) bswap64(x)\n+\n+#else\n+\n+#undef ntohll\n+#undef htonll\n+\n+#if !defined(__BYTE_ORDER)\n+# if defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)\n+#  define __BYTE_ORDER BYTE_ORDER\n+#  define __LITTLE_ENDIAN LITTLE_ENDIAN\n+#  define __BIG_ENDIAN BIG_ENDIAN\n+# endif\n+#endif\n+\n+#if !defined(__BYTE_ORDER)\n+# error \"Cannot determine endianness\"\n+#endif\n+\n+#if __BYTE_ORDER == __BIG_ENDIAN\n+# define ntohll(n) (n)\n+# define htonll(n) (n)\n+#else\n+# define ntohll(n) default_bswap64(n)\n+# define htonll(n) default_bswap64(n)\n+#endif\n+\n+#endif\n-- \n1.8.5.1.399.g900e7cd\n"},{"id":"232313","messageId":"20131221135953.GH21145@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20131221135651.GA20818@sigill.intra.peff.net","subject":"[PATCH v4 08/23] ewah: compressed bitmap implementation","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-12-21T13:59:54Z","receivedAt":"2013-12-21T13:59:54Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"From: Vicent Marti <tanoku@gmail.com>\n\nEWAH is a word-aligned compressed variant of a bitset (i.e. a data\nstructure that acts as a 0-indexed boolean array for many entries).\n\nIt uses a 64-bit run-length encoding (RLE) compression scheme,\ntrading some compression for better processing speed.\n\nThe goal of this word-aligned implementation is not to achieve\nthe best compression, but rather to improve query processing time.\nAs it stands right now, this EWAH implementation will always be more\nefficient storage-wise than its uncompressed alternative.\n\nEWAH arrays will be used as the on-disk format to store reachability\nbitmaps for all objects in a repository while keeping reasonable sizes,\nin the same way that JGit does.\n\nThis EWAH implementation is a mostly straightforward port of the\noriginal `javaewah` library that JGit currently uses. The library is\nself-contained and has been embedded whole (4 files) inside the `ewah`\nfolder to ease redistribution.\n\nThe library is re-licensed under the GPLv2 with the permission of Daniel\nLemire, the original author. The source code for the C version can\nbe found on GitHub:\n\n\thttps://github.com/vmg/libewok\n\nThe original Java implementation can also be found on GitHub:\n\n\thttps://github.com/lemire/javaewah\n\nSigned-off-by: Vicent Marti <tanoku@gmail.com>\nSigned-off-by: Jeff King <peff@peff.net>\n---\n Makefile           |  11 +-\n ewah/bitmap.c      | 221 ++++++++++++++++\n ewah/ewah_bitmap.c | 726 +++++++++++++++++++++++++++++++++++++++++++++++++++++\n ewah/ewah_io.c     | 193 ++++++++++++++\n ewah/ewah_rlw.c    | 115 +++++++++\n ewah/ewok.h        | 235 +++++++++++++++++\n ewah/ewok_rlw.h    | 114 +++++++++\n 7 files changed, 1613 insertions(+), 2 deletions(-)\n create mode 100644 ewah/bitmap.c\n create mode 100644 ewah/ewah_bitmap.c\n create mode 100644 ewah/ewah_io.c\n create mode 100644 ewah/ewah_rlw.c\n create mode 100644 ewah/ewok.h\n create mode 100644 ewah/ewok_rlw.h\n\ndiff --git a/Makefile b/Makefile\nindex 48ff0bd..64a1ed7 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -667,6 +667,8 @@ LIB_H += diff.h\n LIB_H += diffcore.h\n LIB_H += dir.h\n LIB_H += exec_cmd.h\n+LIB_H += ewah/ewok.h\n+LIB_H += ewah/ewok_rlw.h\n LIB_H += fetch-pack.h\n LIB_H += fmt-merge-msg.h\n LIB_H += fsck.h\n@@ -800,6 +802,10 @@ LIB_OBJS += dir.o\n LIB_OBJS += editor.o\n LIB_OBJS += entry.o\n LIB_OBJS += environment.o\n+LIB_OBJS += ewah/bitmap.o\n+LIB_OBJS += ewah/ewah_bitmap.o\n+LIB_OBJS += ewah/ewah_io.o\n+LIB_OBJS += ewah/ewah_rlw.o\n LIB_OBJS += exec_cmd.o\n LIB_OBJS += fetch-pack.o\n LIB_OBJS += fsck.o\n@@ -2474,8 +2480,9 @@ profile-clean:\n \t$(RM) $(addsuffix *.gcno,$(addprefix $(PROFILE_DIR)/, $(object_dirs)))\n \n clean: profile-clean coverage-clean\n-\t$(RM) *.o *.res block-sha1/*.o ppc/*.o compat/*.o compat/*/*.o xdiff/*.o vcs-svn/*.o \\\n-\t\tbuiltin/*.o $(LIB_FILE) $(XDIFF_LIB) $(VCSSVN_LIB)\n+\t$(RM) *.o *.res block-sha1/*.o ppc/*.o compat/*.o compat/*/*.o\n+\t$(RM) xdiff/*.o vcs-svn/*.o ewah/*.o builtin/*.o\n+\t$(RM) $(LIB_FILE) $(XDIFF_LIB) $(VCSSVN_LIB)\n \t$(RM) $(ALL_PROGRAMS) $(SCRIPT_LIB) $(BUILT_INS) git$X\n \t$(RM) $(TEST_PROGRAMS) $(NO_INSTALL)\n \t$(RM) -r bin-wrappers $(dep_dirs)\ndiff --git a/ewah/bitmap.c b/ewah/bitmap.c\nnew file mode 100644\nindex 0000000..710e58c\n--- /dev/null\n+++ b/ewah/bitmap.c\n@@ -0,0 +1,221 @@\n+/**\n+ * Copyright 2013, GitHub, Inc\n+ * Copyright 2009-2013, Daniel Lemire, Cliff Moon,\n+ *\tDavid McIntosh, Robert Becho, Google Inc. and Veronika Zenz\n+ *\n+ * This program is free software; you can redistribute it and/or\n+ * modify it under the terms of the GNU General Public License\n+ * as published by the Free Software Foundation; either version 2\n+ * of the License, or (at your option) any later version.\n+ *\n+ * This program is distributed in the hope that it will be useful,\n+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\n+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n+ * GNU General Public License for more details.\n+ *\n+ * You should have received a copy of the GNU General Public License\n+ * along with this program; if not, write to the Free Software\n+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\n+ */\n+#include \"git-compat-util.h\"\n+#include \"ewok.h\"\n+\n+#define MASK(x) ((eword_t)1 << (x % BITS_IN_WORD))\n+#define BLOCK(x) (x / BITS_IN_WORD)\n+\n+struct bitmap *bitmap_new(void)\n+{\n+\tstruct bitmap *bitmap = ewah_malloc(sizeof(struct bitmap));\n+\tbitmap->words = ewah_calloc(32, sizeof(eword_t));\n+\tbitmap->word_alloc = 32;\n+\treturn bitmap;\n+}\n+\n+void bitmap_set(struct bitmap *self, size_t pos)\n+{\n+\tsize_t block = BLOCK(pos);\n+\n+\tif (block >= self->word_alloc) {\n+\t\tsize_t old_size = self->word_alloc;\n+\t\tself->word_alloc = block * 2;\n+\t\tself->words = ewah_realloc(self->words,\n+\t\t\tself->word_alloc * sizeof(eword_t));\n+\n+\t\tmemset(self->words + old_size, 0x0,\n+\t\t\t(self->word_alloc - old_size) * sizeof(eword_t));\n+\t}\n+\n+\tself->words[block] |= MASK(pos);\n+}\n+\n+void bitmap_clear(struct bitmap *self, size_t pos)\n+{\n+\tsize_t block = BLOCK(pos);\n+\n+\tif (block < self->word_alloc)\n+\t\tself->words[block] &= ~MASK(pos);\n+}\n+\n+int bitmap_get(struct bitmap *self, size_t pos)\n+{\n+\tsize_t block = BLOCK(pos);\n+\treturn block < self->word_alloc &&\n+\t\t(self->words[block] & MASK(pos)) != 0;\n+}\n+\n+struct ewah_bitmap *bitmap_to_ewah(struct bitmap *bitmap)\n+{\n+\tstruct ewah_bitmap *ewah = ewah_new();\n+\tsize_t i, running_empty_words = 0;\n+\teword_t last_word = 0;\n+\n+\tfor (i = 0; i < bitmap->word_alloc; ++i) {\n+\t\tif (bitmap->words[i] == 0) {\n+\t\t\trunning_empty_words++;\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\tif (last_word != 0)\n+\t\t\tewah_add(ewah, last_word);\n+\n+\t\tif (running_empty_words > 0) {\n+\t\t\tewah_add_empty_words(ewah, 0, running_empty_words);\n+\t\t\trunning_empty_words = 0;\n+\t\t}\n+\n+\t\tlast_word = bitmap->words[i];\n+\t}\n+\n+\tewah_add(ewah, last_word);\n+\treturn ewah;\n+}\n+\n+struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah)\n+{\n+\tstruct bitmap *bitmap = bitmap_new();\n+\tstruct ewah_iterator it;\n+\teword_t blowup;\n+\tsize_t i = 0;\n+\n+\tewah_iterator_init(&it, ewah);\n+\n+\twhile (ewah_iterator_next(&blowup, &it)) {\n+\t\tif (i >= bitmap->word_alloc) {\n+\t\t\tbitmap->word_alloc *= 1.5;\n+\t\t\tbitmap->words = ewah_realloc(\n+\t\t\t\tbitmap->words, bitmap->word_alloc * sizeof(eword_t));\n+\t\t}\n+\n+\t\tbitmap->words[i++] = blowup;\n+\t}\n+\n+\tbitmap->word_alloc = i;\n+\treturn bitmap;\n+}\n+\n+void bitmap_and_not(struct bitmap *self, struct bitmap *other)\n+{\n+\tconst size_t count = (self->word_alloc < other->word_alloc) ?\n+\t\tself->word_alloc : other->word_alloc;\n+\n+\tsize_t i;\n+\n+\tfor (i = 0; i < count; ++i)\n+\t\tself->words[i] &= ~other->words[i];\n+}\n+\n+void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)\n+{\n+\tsize_t original_size = self->word_alloc;\n+\tsize_t other_final = (other->bit_size / BITS_IN_WORD) + 1;\n+\tsize_t i = 0;\n+\tstruct ewah_iterator it;\n+\teword_t word;\n+\n+\tif (self->word_alloc < other_final) {\n+\t\tself->word_alloc = other_final;\n+\t\tself->words = ewah_realloc(self->words,\n+\t\t\tself->word_alloc * sizeof(eword_t));\n+\t\tmemset(self->words + original_size, 0x0,\n+\t\t\t(self->word_alloc - original_size) * sizeof(eword_t));\n+\t}\n+\n+\tewah_iterator_init(&it, other);\n+\n+\twhile (ewah_iterator_next(&word, &it))\n+\t\tself->words[i++] |= word;\n+}\n+\n+void bitmap_each_bit(struct bitmap *self, ewah_callback callback, void *data)\n+{\n+\tsize_t pos = 0, i;\n+\n+\tfor (i = 0; i < self->word_alloc; ++i) {\n+\t\teword_t word = self->words[i];\n+\t\tuint32_t offset;\n+\n+\t\tif (word == (eword_t)~0) {\n+\t\t\tfor (offset = 0; offset < BITS_IN_WORD; ++offset)\n+\t\t\t\tcallback(pos++, data);\n+\t\t} else {\n+\t\t\tfor (offset = 0; offset < BITS_IN_WORD; ++offset) {\n+\t\t\t\tif ((word >> offset) == 0)\n+\t\t\t\t\tbreak;\n+\n+\t\t\t\toffset += ewah_bit_ctz64(word >> offset);\n+\t\t\t\tcallback(pos + offset, data);\n+\t\t\t}\n+\t\t\tpos += BITS_IN_WORD;\n+\t\t}\n+\t}\n+}\n+\n+size_t bitmap_popcount(struct bitmap *self)\n+{\n+\tsize_t i, count = 0;\n+\n+\tfor (i = 0; i < self->word_alloc; ++i)\n+\t\tcount += ewah_bit_popcount64(self->words[i]);\n+\n+\treturn count;\n+}\n+\n+int bitmap_equals(struct bitmap *self, struct bitmap *other)\n+{\n+\tstruct bitmap *big, *small;\n+\tsize_t i;\n+\n+\tif (self->word_alloc < other->word_alloc) {\n+\t\tsmall = self;\n+\t\tbig = other;\n+\t} else {\n+\t\tsmall = other;\n+\t\tbig = self;\n+\t}\n+\n+\tfor (i = 0; i < small->word_alloc; ++i) {\n+\t\tif (small->words[i] != big->words[i])\n+\t\t\treturn 0;\n+\t}\n+\n+\tfor (; i < big->word_alloc; ++i) {\n+\t\tif (big->words[i] != 0)\n+\t\t\treturn 0;\n+\t}\n+\n+\treturn 1;\n+}\n+\n+void bitmap_reset(struct bitmap *bitmap)\n+{\n+\tmemset(bitmap->words, 0x0, bitmap->word_alloc * sizeof(eword_t));\n+}\n+\n+void bitmap_free(struct bitmap *bitmap)\n+{\n+\tif (bitmap == NULL)\n+\t\treturn;\n+\n+\tfree(bitmap->words);\n+\tfree(bitmap);\n+}\ndiff --git a/ewah/ewah_bitmap.c b/ewah/ewah_bitmap.c\nnew file mode 100644\nindex 0000000..f104b87\n--- /dev/null\n+++ b/ewah/ewah_bitmap.c\n@@ -0,0 +1,726 @@\n+/**\n+ * Copyright 2013, GitHub, Inc\n+ * Copyright 2009-2013, Daniel Lemire, Cliff Moon,\n+ *\tDavid McIntosh, Robert Becho, Google Inc. and Veronika Zenz\n+ *\n+ * This program is free software; you can redistribute it and/or\n+ * modify it under the terms of the GNU General Public License\n+ * as published by the Free Software Foundation; either version 2\n+ * of the License, or (at your option) any later version.\n+ *\n+ * This program is distributed in the hope that it will be useful,\n+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\n+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n+ * GNU General Public License for more details.\n+ *\n+ * You should have received a copy of the GNU General Public License\n+ * along with this program; if not, write to the Free Software\n+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\n+ */\n+#include \"git-compat-util.h\"\n+#include \"ewok.h\"\n+#include \"ewok_rlw.h\"\n+\n+static inline size_t min_size(size_t a, size_t b)\n+{\n+\treturn a < b ? a : b;\n+}\n+\n+static inline size_t max_size(size_t a, size_t b)\n+{\n+\treturn a > b ? a : b;\n+}\n+\n+static inline void buffer_grow(struct ewah_bitmap *self, size_t new_size)\n+{\n+\tsize_t rlw_offset = (uint8_t *)self->rlw - (uint8_t *)self->buffer;\n+\n+\tif (self->alloc_size >= new_size)\n+\t\treturn;\n+\n+\tself->alloc_size = new_size;\n+\tself->buffer = ewah_realloc(self->buffer,\n+\t\tself->alloc_size * sizeof(eword_t));\n+\tself->rlw = self->buffer + (rlw_offset / sizeof(size_t));\n+}\n+\n+static inline void buffer_push(struct ewah_bitmap *self, eword_t value)\n+{\n+\tif (self->buffer_size + 1 >= self->alloc_size)\n+\t\tbuffer_grow(self, self->buffer_size * 3 / 2);\n+\n+\tself->buffer[self->buffer_size++] = value;\n+}\n+\n+static void buffer_push_rlw(struct ewah_bitmap *self, eword_t value)\n+{\n+\tbuffer_push(self, value);\n+\tself->rlw = self->buffer + self->buffer_size - 1;\n+}\n+\n+static size_t add_empty_words(struct ewah_bitmap *self, int v, size_t number)\n+{\n+\tsize_t added = 0;\n+\teword_t runlen, can_add;\n+\n+\tif (rlw_get_run_bit(self->rlw) != v && rlw_size(self->rlw) == 0) {\n+\t\trlw_set_run_bit(self->rlw, v);\n+\t} else if (rlw_get_literal_words(self->rlw) != 0 ||\n+\t\t\trlw_get_run_bit(self->rlw) != v) {\n+\t\tbuffer_push_rlw(self, 0);\n+\t\tif (v) rlw_set_run_bit(self->rlw, v);\n+\t\tadded++;\n+\t}\n+\n+\trunlen = rlw_get_running_len(self->rlw);\n+\tcan_add = min_size(number, RLW_LARGEST_RUNNING_COUNT - runlen);\n+\n+\trlw_set_running_len(self->rlw, runlen + can_add);\n+\tnumber -= can_add;\n+\n+\twhile (number >= RLW_LARGEST_RUNNING_COUNT) {\n+\t\tbuffer_push_rlw(self, 0);\n+\t\tadded++;\n+\t\tif (v) rlw_set_run_bit(self->rlw, v);\n+\t\trlw_set_running_len(self->rlw, RLW_LARGEST_RUNNING_COUNT);\n+\t\tnumber -= RLW_LARGEST_RUNNING_COUNT;\n+\t}\n+\n+\tif (number > 0) {\n+\t\tbuffer_push_rlw(self, 0);\n+\t\tadded++;\n+\n+\t\tif (v) rlw_set_run_bit(self->rlw, v);\n+\t\trlw_set_running_len(self->rlw, number);\n+\t}\n+\n+\treturn added;\n+}\n+\n+size_t ewah_add_empty_words(struct ewah_bitmap *self, int v, size_t number)\n+{\n+\tif (number == 0)\n+\t\treturn 0;\n+\n+\tself->bit_size += number * BITS_IN_WORD;\n+\treturn add_empty_words(self, v, number);\n+}\n+\n+static size_t add_literal(struct ewah_bitmap *self, eword_t new_data)\n+{\n+\teword_t current_num = rlw_get_literal_words(self->rlw);\n+\n+\tif (current_num >= RLW_LARGEST_LITERAL_COUNT) {\n+\t\tbuffer_push_rlw(self, 0);\n+\n+\t\trlw_set_literal_words(self->rlw, 1);\n+\t\tbuffer_push(self, new_data);\n+\t\treturn 2;\n+\t}\n+\n+\trlw_set_literal_words(self->rlw, current_num + 1);\n+\n+\t/* sanity check */\n+\tassert(rlw_get_literal_words(self->rlw) == current_num + 1);\n+\n+\tbuffer_push(self, new_data);\n+\treturn 1;\n+}\n+\n+void ewah_add_dirty_words(\n+\tstruct ewah_bitmap *self, const eword_t *buffer,\n+\tsize_t number, int negate)\n+{\n+\tsize_t literals, can_add;\n+\n+\twhile (1) {\n+\t\tliterals = rlw_get_literal_words(self->rlw);\n+\t\tcan_add = min_size(number, RLW_LARGEST_LITERAL_COUNT - literals);\n+\n+\t\trlw_set_literal_words(self->rlw, literals + can_add);\n+\n+\t\tif (self->buffer_size + can_add >= self->alloc_size)\n+\t\t\tbuffer_grow(self, (self->buffer_size + can_add) * 3 / 2);\n+\n+\t\tif (negate) {\n+\t\t\tsize_t i;\n+\t\t\tfor (i = 0; i < can_add; ++i)\n+\t\t\t\tself->buffer[self->buffer_size++] = ~buffer[i];\n+\t\t} else {\n+\t\t\tmemcpy(self->buffer + self->buffer_size,\n+\t\t\t\tbuffer, can_add * sizeof(eword_t));\n+\t\t\tself->buffer_size += can_add;\n+\t\t}\n+\n+\t\tself->bit_size += can_add * BITS_IN_WORD;\n+\n+\t\tif (number - can_add == 0)\n+\t\t\tbreak;\n+\n+\t\tbuffer_push_rlw(self, 0);\n+\t\tbuffer += can_add;\n+\t\tnumber -= can_add;\n+\t}\n+}\n+\n+static size_t add_empty_word(struct ewah_bitmap *self, int v)\n+{\n+\tint no_literal = (rlw_get_literal_words(self->rlw) == 0);\n+\teword_t run_len = rlw_get_running_len(self->rlw);\n+\n+\tif (no_literal && run_len == 0) {\n+\t\trlw_set_run_bit(self->rlw, v);\n+\t\tassert(rlw_get_run_bit(self->rlw) == v);\n+\t}\n+\n+\tif (no_literal && rlw_get_run_bit(self->rlw) == v &&\n+\t\trun_len < RLW_LARGEST_RUNNING_COUNT) {\n+\t\trlw_set_running_len(self->rlw, run_len + 1);\n+\t\tassert(rlw_get_running_len(self->rlw) == run_len + 1);\n+\t\treturn 0;\n+\t} else {\n+\t\tbuffer_push_rlw(self, 0);\n+\n+\t\tassert(rlw_get_running_len(self->rlw) == 0);\n+\t\tassert(rlw_get_run_bit(self->rlw) == 0);\n+\t\tassert(rlw_get_literal_words(self->rlw) == 0);\n+\n+\t\trlw_set_run_bit(self->rlw, v);\n+\t\tassert(rlw_get_run_bit(self->rlw) == v);\n+\n+\t\trlw_set_running_len(self->rlw, 1);\n+\t\tassert(rlw_get_running_len(self->rlw) == 1);\n+\t\tassert(rlw_get_literal_words(self->rlw) == 0);\n+\t\treturn 1;\n+\t}\n+}\n+\n+size_t ewah_add(struct ewah_bitmap *self, eword_t word)\n+{\n+\tself->bit_size += BITS_IN_WORD;\n+\n+\tif (word == 0)\n+\t\treturn add_empty_word(self, 0);\n+\n+\tif (word == (eword_t)(~0))\n+\t\treturn add_empty_word(self, 1);\n+\n+\treturn add_literal(self, word);\n+}\n+\n+void ewah_set(struct ewah_bitmap *self, size_t i)\n+{\n+\tconst size_t dist =\n+\t\t(i + BITS_IN_WORD) / BITS_IN_WORD -\n+\t\t(self->bit_size + BITS_IN_WORD - 1) / BITS_IN_WORD;\n+\n+\tassert(i >= self->bit_size);\n+\n+\tself->bit_size = i + 1;\n+\n+\tif (dist > 0) {\n+\t\tif (dist > 1)\n+\t\t\tadd_empty_words(self, 0, dist - 1);\n+\n+\t\tadd_literal(self, (eword_t)1 << (i % BITS_IN_WORD));\n+\t\treturn;\n+\t}\n+\n+\tif (rlw_get_literal_words(self->rlw) == 0) {\n+\t\trlw_set_running_len(self->rlw,\n+\t\t\trlw_get_running_len(self->rlw) - 1);\n+\t\tadd_literal(self, (eword_t)1 << (i % BITS_IN_WORD));\n+\t\treturn;\n+\t}\n+\n+\tself->buffer[self->buffer_size - 1] |=\n+\t\t((eword_t)1 << (i % BITS_IN_WORD));\n+\n+\t/* check if we just completed a stream of 1s */\n+\tif (self->buffer[self->buffer_size - 1] == (eword_t)(~0)) {\n+\t\tself->buffer[--self->buffer_size] = 0;\n+\t\trlw_set_literal_words(self->rlw,\n+\t\t\trlw_get_literal_words(self->rlw) - 1);\n+\t\tadd_empty_word(self, 1);\n+\t}\n+}\n+\n+void ewah_each_bit(struct ewah_bitmap *self, void (*callback)(size_t, void*), void *payload)\n+{\n+\tsize_t pos = 0;\n+\tsize_t pointer = 0;\n+\tsize_t k;\n+\n+\twhile (pointer < self->buffer_size) {\n+\t\teword_t *word = &self->buffer[pointer];\n+\n+\t\tif (rlw_get_run_bit(word)) {\n+\t\t\tsize_t len = rlw_get_running_len(word) * BITS_IN_WORD;\n+\t\t\tfor (k = 0; k < len; ++k, ++pos)\n+\t\t\t\tcallback(pos, payload);\n+\t\t} else {\n+\t\t\tpos += rlw_get_running_len(word) * BITS_IN_WORD;\n+\t\t}\n+\n+\t\t++pointer;\n+\n+\t\tfor (k = 0; k < rlw_get_literal_words(word); ++k) {\n+\t\t\tint c;\n+\n+\t\t\t/* todo: zero count optimization */\n+\t\t\tfor (c = 0; c < BITS_IN_WORD; ++c, ++pos) {\n+\t\t\t\tif ((self->buffer[pointer] & ((eword_t)1 << c)) != 0)\n+\t\t\t\t\tcallback(pos, payload);\n+\t\t\t}\n+\n+\t\t\t++pointer;\n+\t\t}\n+\t}\n+}\n+\n+struct ewah_bitmap *ewah_new(void)\n+{\n+\tstruct ewah_bitmap *self;\n+\n+\tself = ewah_malloc(sizeof(struct ewah_bitmap));\n+\tif (self == NULL)\n+\t\treturn NULL;\n+\n+\tself->buffer = ewah_malloc(32 * sizeof(eword_t));\n+\tself->alloc_size = 32;\n+\n+\tewah_clear(self);\n+\treturn self;\n+}\n+\n+void ewah_clear(struct ewah_bitmap *self)\n+{\n+\tself->buffer_size = 1;\n+\tself->buffer[0] = 0;\n+\tself->bit_size = 0;\n+\tself->rlw = self->buffer;\n+}\n+\n+void ewah_free(struct ewah_bitmap *self)\n+{\n+\tif (!self)\n+\t\treturn;\n+\n+\tif (self->alloc_size)\n+\t\tfree(self->buffer);\n+\n+\tfree(self);\n+}\n+\n+static void read_new_rlw(struct ewah_iterator *it)\n+{\n+\tconst eword_t *word = NULL;\n+\n+\tit->literals = 0;\n+\tit->compressed = 0;\n+\n+\twhile (1) {\n+\t\tword = &it->buffer[it->pointer];\n+\n+\t\tit->rl = rlw_get_running_len(word);\n+\t\tit->lw = rlw_get_literal_words(word);\n+\t\tit->b = rlw_get_run_bit(word);\n+\n+\t\tif (it->rl || it->lw)\n+\t\t\treturn;\n+\n+\t\tif (it->pointer < it->buffer_size - 1) {\n+\t\t\tit->pointer++;\n+\t\t} else {\n+\t\t\tit->pointer = it->buffer_size;\n+\t\t\treturn;\n+\t\t}\n+\t}\n+}\n+\n+int ewah_iterator_next(eword_t *next, struct ewah_iterator *it)\n+{\n+\tif (it->pointer >= it->buffer_size)\n+\t\treturn 0;\n+\n+\tif (it->compressed < it->rl) {\n+\t\tit->compressed++;\n+\t\t*next = it->b ? (eword_t)(~0) : 0;\n+\t} else {\n+\t\tassert(it->literals < it->lw);\n+\n+\t\tit->literals++;\n+\t\tit->pointer++;\n+\n+\t\tassert(it->pointer < it->buffer_size);\n+\n+\t\t*next = it->buffer[it->pointer];\n+\t}\n+\n+\tif (it->compressed == it->rl && it->literals == it->lw) {\n+\t\tif (++it->pointer < it->buffer_size)\n+\t\t\tread_new_rlw(it);\n+\t}\n+\n+\treturn 1;\n+}\n+\n+void ewah_iterator_init(struct ewah_iterator *it, struct ewah_bitmap *parent)\n+{\n+\tit->buffer = parent->buffer;\n+\tit->buffer_size = parent->buffer_size;\n+\tit->pointer = 0;\n+\n+\tit->lw = 0;\n+\tit->rl = 0;\n+\tit->compressed = 0;\n+\tit->literals = 0;\n+\tit->b = 0;\n+\n+\tif (it->pointer < it->buffer_size)\n+\t\tread_new_rlw(it);\n+}\n+\n+void ewah_dump(struct ewah_bitmap *self)\n+{\n+\tsize_t i;\n+\tfprintf(stderr, \"%\"PRIuMAX\" bits | %\"PRIuMAX\" words | \",\n+\t\t(uintmax_t)self->bit_size, (uintmax_t)self->buffer_size);\n+\n+\tfor (i = 0; i < self->buffer_size; ++i)\n+\t\tfprintf(stderr, \"%016\"PRIx64\" \", (uint64_t)self->buffer[i]);\n+\n+\tfprintf(stderr, \"\\n\");\n+}\n+\n+void ewah_not(struct ewah_bitmap *self)\n+{\n+\tsize_t pointer = 0;\n+\n+\twhile (pointer < self->buffer_size) {\n+\t\teword_t *word = &self->buffer[pointer];\n+\t\tsize_t literals, k;\n+\n+\t\trlw_xor_run_bit(word);\n+\t\t++pointer;\n+\n+\t\tliterals = rlw_get_literal_words(word);\n+\t\tfor (k = 0; k < literals; ++k) {\n+\t\t\tself->buffer[pointer] = ~self->buffer[pointer];\n+\t\t\t++pointer;\n+\t\t}\n+\t}\n+}\n+\n+void ewah_xor(\n+\tstruct ewah_bitmap *ewah_i,\n+\tstruct ewah_bitmap *ewah_j,\n+\tstruct ewah_bitmap *out)\n+{\n+\tstruct rlw_iterator rlw_i;\n+\tstruct rlw_iterator rlw_j;\n+\tsize_t literals;\n+\n+\trlwit_init(&rlw_i, ewah_i);\n+\trlwit_init(&rlw_j, ewah_j);\n+\n+\twhile (rlwit_word_size(&rlw_i) > 0 && rlwit_word_size(&rlw_j) > 0) {\n+\t\twhile (rlw_i.rlw.running_len > 0 || rlw_j.rlw.running_len > 0) {\n+\t\t\tstruct rlw_iterator *prey, *predator;\n+\t\t\tsize_t index;\n+\t\t\tint negate_words;\n+\n+\t\t\tif (rlw_i.rlw.running_len < rlw_j.rlw.running_len) {\n+\t\t\t\tprey = &rlw_i;\n+\t\t\t\tpredator = &rlw_j;\n+\t\t\t} else {\n+\t\t\t\tprey = &rlw_j;\n+\t\t\t\tpredator = &rlw_i;\n+\t\t\t}\n+\n+\t\t\tnegate_words = !!predator->rlw.running_bit;\n+\t\t\tindex = rlwit_discharge(prey, out,\n+\t\t\t\tpredator->rlw.running_len, negate_words);\n+\n+\t\t\tewah_add_empty_words(out, negate_words,\n+\t\t\t\tpredator->rlw.running_len - index);\n+\n+\t\t\trlwit_discard_first_words(predator,\n+\t\t\t\tpredator->rlw.running_len);\n+\t\t}\n+\n+\t\tliterals = min_size(\n+\t\t\trlw_i.rlw.literal_words,\n+\t\t\trlw_j.rlw.literal_words);\n+\n+\t\tif (literals) {\n+\t\t\tsize_t k;\n+\n+\t\t\tfor (k = 0; k < literals; ++k) {\n+\t\t\t\tewah_add(out,\n+\t\t\t\t\trlw_i.buffer[rlw_i.literal_word_start + k] ^\n+\t\t\t\t\trlw_j.buffer[rlw_j.literal_word_start + k]\n+\t\t\t\t);\n+\t\t\t}\n+\n+\t\t\trlwit_discard_first_words(&rlw_i, literals);\n+\t\t\trlwit_discard_first_words(&rlw_j, literals);\n+\t\t}\n+\t}\n+\n+\tif (rlwit_word_size(&rlw_i) > 0)\n+\t\trlwit_discharge(&rlw_i, out, ~0, 0);\n+\telse\n+\t\trlwit_discharge(&rlw_j, out, ~0, 0);\n+\n+\tout->bit_size = max_size(ewah_i->bit_size, ewah_j->bit_size);\n+}\n+\n+void ewah_and(\n+\tstruct ewah_bitmap *ewah_i,\n+\tstruct ewah_bitmap *ewah_j,\n+\tstruct ewah_bitmap *out)\n+{\n+\tstruct rlw_iterator rlw_i;\n+\tstruct rlw_iterator rlw_j;\n+\tsize_t literals;\n+\n+\trlwit_init(&rlw_i, ewah_i);\n+\trlwit_init(&rlw_j, ewah_j);\n+\n+\twhile (rlwit_word_size(&rlw_i) > 0 && rlwit_word_size(&rlw_j) > 0) {\n+\t\twhile (rlw_i.rlw.running_len > 0 || rlw_j.rlw.running_len > 0) {\n+\t\t\tstruct rlw_iterator *prey, *predator;\n+\n+\t\t\tif (rlw_i.rlw.running_len < rlw_j.rlw.running_len) {\n+\t\t\t\tprey = &rlw_i;\n+\t\t\t\tpredator = &rlw_j;\n+\t\t\t} else {\n+\t\t\t\tprey = &rlw_j;\n+\t\t\t\tpredator = &rlw_i;\n+\t\t\t}\n+\n+\t\t\tif (predator->rlw.running_bit == 0) {\n+\t\t\t\tewah_add_empty_words(out, 0,\n+\t\t\t\t\tpredator->rlw.running_len);\n+\t\t\t\trlwit_discard_first_words(prey,\n+\t\t\t\t\tpredator->rlw.running_len);\n+\t\t\t\trlwit_discard_first_words(predator,\n+\t\t\t\t\tpredator->rlw.running_len);\n+\t\t\t} else {\n+\t\t\t\tsize_t index = rlwit_discharge(prey, out,\n+\t\t\t\t\tpredator->rlw.running_len, 0);\n+\t\t\t\tewah_add_empty_words(out, 0,\n+\t\t\t\t\tpredator->rlw.running_len - index);\n+\t\t\t\trlwit_discard_first_words(predator,\n+\t\t\t\t\tpredator->rlw.running_len);\n+\t\t\t}\n+\t\t}\n+\n+\t\tliterals = min_size(\n+\t\t\trlw_i.rlw.literal_words,\n+\t\t\trlw_j.rlw.literal_words);\n+\n+\t\tif (literals) {\n+\t\t\tsize_t k;\n+\n+\t\t\tfor (k = 0; k < literals; ++k) {\n+\t\t\t\tewah_add(out,\n+\t\t\t\t\trlw_i.buffer[rlw_i.literal_word_start + k] &\n+\t\t\t\t\trlw_j.buffer[rlw_j.literal_word_start + k]\n+\t\t\t\t);\n+\t\t\t}\n+\n+\t\t\trlwit_discard_first_words(&rlw_i, literals);\n+\t\t\trlwit_discard_first_words(&rlw_j, literals);\n+\t\t}\n+\t}\n+\n+\tif (rlwit_word_size(&rlw_i) > 0)\n+\t\trlwit_discharge_empty(&rlw_i, out);\n+\telse\n+\t\trlwit_discharge_empty(&rlw_j, out);\n+\n+\tout->bit_size = max_size(ewah_i->bit_size, ewah_j->bit_size);\n+}\n+\n+void ewah_and_not(\n+\tstruct ewah_bitmap *ewah_i,\n+\tstruct ewah_bitmap *ewah_j,\n+\tstruct ewah_bitmap *out)\n+{\n+\tstruct rlw_iterator rlw_i;\n+\tstruct rlw_iterator rlw_j;\n+\tsize_t literals;\n+\n+\trlwit_init(&rlw_i, ewah_i);\n+\trlwit_init(&rlw_j, ewah_j);\n+\n+\twhile (rlwit_word_size(&rlw_i) > 0 && rlwit_word_size(&rlw_j) > 0) {\n+\t\twhile (rlw_i.rlw.running_len > 0 || rlw_j.rlw.running_len > 0) {\n+\t\t\tstruct rlw_iterator *prey, *predator;\n+\n+\t\t\tif (rlw_i.rlw.running_len < rlw_j.rlw.running_len) {\n+\t\t\t\tprey = &rlw_i;\n+\t\t\t\tpredator = &rlw_j;\n+\t\t\t} else {\n+\t\t\t\tprey = &rlw_j;\n+\t\t\t\tpredator = &rlw_i;\n+\t\t\t}\n+\n+\t\t\tif ((predator->rlw.running_bit && prey == &rlw_i) ||\n+\t\t\t\t(!predator->rlw.running_bit && prey != &rlw_i)) {\n+\t\t\t\tewah_add_empty_words(out, 0,\n+\t\t\t\t\tpredator->rlw.running_len);\n+\t\t\t\trlwit_discard_first_words(prey,\n+\t\t\t\t\tpredator->rlw.running_len);\n+\t\t\t\trlwit_discard_first_words(predator,\n+\t\t\t\t\tpredator->rlw.running_len);\n+\t\t\t} else {\n+\t\t\t\tsize_t index;\n+\t\t\t\tint negate_words;\n+\n+\t\t\t\tnegate_words = (&rlw_i != prey);\n+\t\t\t\tindex = rlwit_discharge(prey, out,\n+\t\t\t\t\tpredator->rlw.running_len, negate_words);\n+\t\t\t\tewah_add_empty_words(out, negate_words,\n+\t\t\t\t\tpredator->rlw.running_len - index);\n+\t\t\t\trlwit_discard_first_words(predator,\n+\t\t\t\t\tpredator->rlw.running_len);\n+\t\t\t}\n+\t\t}\n+\n+\t\tliterals = min_size(\n+\t\t\trlw_i.rlw.literal_words,\n+\t\t\trlw_j.rlw.literal_words);\n+\n+\t\tif (literals) {\n+\t\t\tsize_t k;\n+\n+\t\t\tfor (k = 0; k < literals; ++k) {\n+\t\t\t\tewah_add(out,\n+\t\t\t\t\trlw_i.buffer[rlw_i.literal_word_start + k] &\n+\t\t\t\t\t~(rlw_j.buffer[rlw_j.literal_word_start + k])\n+\t\t\t\t);\n+\t\t\t}\n+\n+\t\t\trlwit_discard_first_words(&rlw_i, literals);\n+\t\t\trlwit_discard_first_words(&rlw_j, literals);\n+\t\t}\n+\t}\n+\n+\tif (rlwit_word_size(&rlw_i) > 0)\n+\t\trlwit_discharge(&rlw_i, out, ~0, 0);\n+\telse\n+\t\trlwit_discharge_empty(&rlw_j, out);\n+\n+\tout->bit_size = max_size(ewah_i->bit_size, ewah_j->bit_size);\n+}\n+\n+void ewah_or(\n+\tstruct ewah_bitmap *ewah_i,\n+\tstruct ewah_bitmap *ewah_j,\n+\tstruct ewah_bitmap *out)\n+{\n+\tstruct rlw_iterator rlw_i;\n+\tstruct rlw_iterator rlw_j;\n+\tsize_t literals;\n+\n+\trlwit_init(&rlw_i, ewah_i);\n+\trlwit_init(&rlw_j, ewah_j);\n+\n+\twhile (rlwit_word_size(&rlw_i) > 0 && rlwit_word_size(&rlw_j) > 0) {\n+\t\twhile (rlw_i.rlw.running_len > 0 || rlw_j.rlw.running_len > 0) {\n+\t\t\tstruct rlw_iterator *prey, *predator;\n+\n+\t\t\tif (rlw_i.rlw.running_len < rlw_j.rlw.running_len) {\n+\t\t\t\tprey = &rlw_i;\n+\t\t\t\tpredator = &rlw_j;\n+\t\t\t} else {\n+\t\t\t\tprey = &rlw_j;\n+\t\t\t\tpredator = &rlw_i;\n+\t\t\t}\n+\n+\t\t\tif (predator->rlw.running_bit) {\n+\t\t\t\tewah_add_empty_words(out, 0,\n+\t\t\t\t\tpredator->rlw.running_len);\n+\t\t\t\trlwit_discard_first_words(prey,\n+\t\t\t\t\tpredator->rlw.running_len);\n+\t\t\t\trlwit_discard_first_words(predator,\n+\t\t\t\t\tpredator->rlw.running_len);\n+\t\t\t} else {\n+\t\t\t\tsize_t index = rlwit_discharge(prey, out,\n+\t\t\t\t\tpredator->rlw.running_len, 0);\n+\t\t\t\tewah_add_empty_words(out, 0,\n+\t\t\t\t\tpredator->rlw.running_len - index);\n+\t\t\t\trlwit_discard_first_words(predator,\n+\t\t\t\t\tpredator->rlw.running_len);\n+\t\t\t}\n+\t\t}\n+\n+\t\tliterals = min_size(\n+\t\t\trlw_i.rlw.literal_words,\n+\t\t\trlw_j.rlw.literal_words);\n+\n+\t\tif (literals) {\n+\t\t\tsize_t k;\n+\n+\t\t\tfor (k = 0; k < literals; ++k) {\n+\t\t\t\tewah_add(out,\n+\t\t\t\t\trlw_i.buffer[rlw_i.literal_word_start + k] |\n+\t\t\t\t\trlw_j.buffer[rlw_j.literal_word_start + k]\n+\t\t\t\t);\n+\t\t\t}\n+\n+\t\t\trlwit_discard_first_words(&rlw_i, literals);\n+\t\t\trlwit_discard_first_words(&rlw_j, literals);\n+\t\t}\n+\t}\n+\n+\tif (rlwit_word_size(&rlw_i) > 0)\n+\t\trlwit_discharge(&rlw_i, out, ~0, 0);\n+\telse\n+\t\trlwit_discharge(&rlw_j, out, ~0, 0);\n+\n+\tout->bit_size = max_size(ewah_i->bit_size, ewah_j->bit_size);\n+}\n+\n+\n+#define BITMAP_POOL_MAX 16\n+static struct ewah_bitmap *bitmap_pool[BITMAP_POOL_MAX];\n+static size_t bitmap_pool_size;\n+\n+struct ewah_bitmap *ewah_pool_new(void)\n+{\n+\tif (bitmap_pool_size)\n+\t\treturn bitmap_pool[--bitmap_pool_size];\n+\n+\treturn ewah_new();\n+}\n+\n+void ewah_pool_free(struct ewah_bitmap *self)\n+{\n+\tif (self == NULL)\n+\t\treturn;\n+\n+\tif (bitmap_pool_size == BITMAP_POOL_MAX ||\n+\t\tself->alloc_size == 0) {\n+\t\tewah_free(self);\n+\t\treturn;\n+\t}\n+\n+\tewah_clear(self);\n+\tbitmap_pool[bitmap_pool_size++] = self;\n+}\n+\n+uint32_t ewah_checksum(struct ewah_bitmap *self)\n+{\n+\tconst uint8_t *p = (uint8_t *)self->buffer;\n+\tuint32_t crc = (uint32_t)self->bit_size;\n+\tsize_t size = self->buffer_size * sizeof(eword_t);\n+\n+\twhile (size--)\n+\t\tcrc = (crc << 5) - crc + (uint32_t)*p++;\n+\n+\treturn crc;\n+}\ndiff --git a/ewah/ewah_io.c b/ewah/ewah_io.c\nnew file mode 100644\nindex 0000000..aed0da6\n--- /dev/null\n+++ b/ewah/ewah_io.c\n@@ -0,0 +1,193 @@\n+/**\n+ * Copyright 2013, GitHub, Inc\n+ * Copyright 2009-2013, Daniel Lemire, Cliff Moon,\n+ *\tDavid McIntosh, Robert Becho, Google Inc. and Veronika Zenz\n+ *\n+ * This program is free software; you can redistribute it and/or\n+ * modify it under the terms of the GNU General Public License\n+ * as published by the Free Software Foundation; either version 2\n+ * of the License, or (at your option) any later version.\n+ *\n+ * This program is distributed in the hope that it will be useful,\n+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\n+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n+ * GNU General Public License for more details.\n+ *\n+ * You should have received a copy of the GNU General Public License\n+ * along with this program; if not, write to the Free Software\n+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\n+ */\n+#include \"git-compat-util.h\"\n+#include \"ewok.h\"\n+\n+int ewah_serialize_native(struct ewah_bitmap *self, int fd)\n+{\n+\tuint32_t write32;\n+\tsize_t to_write = self->buffer_size * 8;\n+\n+\t/* 32 bit -- bit size for the map */\n+\twrite32 = (uint32_t)self->bit_size;\n+\tif (write(fd, &write32, 4) != 4)\n+\t\treturn -1;\n+\n+\t/** 32 bit -- number of compressed 64-bit words */\n+\twrite32 = (uint32_t)self->buffer_size;\n+\tif (write(fd, &write32, 4) != 4)\n+\t\treturn -1;\n+\n+\tif (write(fd, self->buffer, to_write) != to_write)\n+\t\treturn -1;\n+\n+\t/** 32 bit -- position for the RLW */\n+\twrite32 = self->rlw - self->buffer;\n+\tif (write(fd, &write32, 4) != 4)\n+\t\treturn -1;\n+\n+\treturn (3 * 4) + to_write;\n+}\n+\n+int ewah_serialize_to(struct ewah_bitmap *self,\n+\t\t      int (*write_fun)(void *, const void *, size_t),\n+\t\t      void *data)\n+{\n+\tsize_t i;\n+\teword_t dump[2048];\n+\tconst size_t words_per_dump = sizeof(dump) / sizeof(eword_t);\n+\tuint32_t bitsize, word_count, rlw_pos;\n+\n+\tconst eword_t *buffer;\n+\tsize_t words_left;\n+\n+\t/* 32 bit -- bit size for the map */\n+\tbitsize =  htonl((uint32_t)self->bit_size);\n+\tif (write_fun(data, &bitsize, 4) != 4)\n+\t\treturn -1;\n+\n+\t/** 32 bit -- number of compressed 64-bit words */\n+\tword_count =  htonl((uint32_t)self->buffer_size);\n+\tif (write_fun(data, &word_count, 4) != 4)\n+\t\treturn -1;\n+\n+\t/** 64 bit x N -- compressed words */\n+\tbuffer = self->buffer;\n+\twords_left = self->buffer_size;\n+\n+\twhile (words_left >= words_per_dump) {\n+\t\tfor (i = 0; i < words_per_dump; ++i, ++buffer)\n+\t\t\tdump[i] = htonll(*buffer);\n+\n+\t\tif (write_fun(data, dump, sizeof(dump)) != sizeof(dump))\n+\t\t\treturn -1;\n+\n+\t\twords_left -= words_per_dump;\n+\t}\n+\n+\tif (words_left) {\n+\t\tfor (i = 0; i < words_left; ++i, ++buffer)\n+\t\t\tdump[i] = htonll(*buffer);\n+\n+\t\tif (write_fun(data, dump, words_left * 8) != words_left * 8)\n+\t\t\treturn -1;\n+\t}\n+\n+\t/** 32 bit -- position for the RLW */\n+\trlw_pos = (uint8_t*)self->rlw - (uint8_t *)self->buffer;\n+\trlw_pos = htonl(rlw_pos / sizeof(eword_t));\n+\n+\tif (write_fun(data, &rlw_pos, 4) != 4)\n+\t\treturn -1;\n+\n+\treturn (3 * 4) + (self->buffer_size * 8);\n+}\n+\n+static int write_helper(void *fd, const void *buf, size_t len)\n+{\n+\treturn write((intptr_t)fd, buf, len);\n+}\n+\n+int ewah_serialize(struct ewah_bitmap *self, int fd)\n+{\n+\treturn ewah_serialize_to(self, write_helper, (void *)(intptr_t)fd);\n+}\n+\n+int ewah_read_mmap(struct ewah_bitmap *self, void *map, size_t len)\n+{\n+\tuint32_t *read32 = map;\n+\teword_t *read64;\n+\tsize_t i;\n+\n+\tself->bit_size = ntohl(*read32++);\n+\tself->buffer_size = self->alloc_size = ntohl(*read32++);\n+\tself->buffer = ewah_realloc(self->buffer,\n+\t\tself->alloc_size * sizeof(eword_t));\n+\n+\tif (!self->buffer)\n+\t\treturn -1;\n+\n+\tfor (i = 0, read64 = (void *)read32; i < self->buffer_size; ++i)\n+\t\tself->buffer[i] = ntohll(*read64++);\n+\n+\tread32 = (void *)read64;\n+\tself->rlw = self->buffer + ntohl(*read32++);\n+\n+\treturn (3 * 4) + (self->buffer_size * 8);\n+}\n+\n+int ewah_deserialize(struct ewah_bitmap *self, int fd)\n+{\n+\tsize_t i;\n+\teword_t dump[2048];\n+\tconst size_t words_per_dump = sizeof(dump) / sizeof(eword_t);\n+\tuint32_t bitsize, word_count, rlw_pos;\n+\n+\teword_t *buffer = NULL;\n+\tsize_t words_left;\n+\n+\tewah_clear(self);\n+\n+\t/* 32 bit -- bit size for the map */\n+\tif (read(fd, &bitsize, 4) != 4)\n+\t\treturn -1;\n+\n+\tself->bit_size = (size_t)ntohl(bitsize);\n+\n+\t/** 32 bit -- number of compressed 64-bit words */\n+\tif (read(fd, &word_count, 4) != 4)\n+\t\treturn -1;\n+\n+\tself->buffer_size = self->alloc_size = (size_t)ntohl(word_count);\n+\tself->buffer = ewah_realloc(self->buffer,\n+\t\tself->alloc_size * sizeof(eword_t));\n+\n+\tif (!self->buffer)\n+\t\treturn -1;\n+\n+\t/** 64 bit x N -- compressed words */\n+\tbuffer = self->buffer;\n+\twords_left = self->buffer_size;\n+\n+\twhile (words_left >= words_per_dump) {\n+\t\tif (read(fd, dump, sizeof(dump)) != sizeof(dump))\n+\t\t\treturn -1;\n+\n+\t\tfor (i = 0; i < words_per_dump; ++i, ++buffer)\n+\t\t\t*buffer = ntohll(dump[i]);\n+\n+\t\twords_left -= words_per_dump;\n+\t}\n+\n+\tif (words_left) {\n+\t\tif (read(fd, dump, words_left * 8) != words_left * 8)\n+\t\t\treturn -1;\n+\n+\t\tfor (i = 0; i < words_left; ++i, ++buffer)\n+\t\t\t*buffer = ntohll(dump[i]);\n+\t}\n+\n+\t/** 32 bit -- position for the RLW */\n+\tif (read(fd, &rlw_pos, 4) != 4)\n+\t\treturn -1;\n+\n+\tself->rlw = self->buffer + ntohl(rlw_pos);\n+\treturn 0;\n+}\ndiff --git a/ewah/ewah_rlw.c b/ewah/ewah_rlw.c\nnew file mode 100644\nindex 0000000..c723f1a\n--- /dev/null\n+++ b/ewah/ewah_rlw.c\n@@ -0,0 +1,115 @@\n+/**\n+ * Copyright 2013, GitHub, Inc\n+ * Copyright 2009-2013, Daniel Lemire, Cliff Moon,\n+ *\tDavid McIntosh, Robert Becho, Google Inc. and Veronika Zenz\n+ *\n+ * This program is free software; you can redistribute it and/or\n+ * modify it under the terms of the GNU General Public License\n+ * as published by the Free Software Foundation; either version 2\n+ * of the License, or (at your option) any later version.\n+ *\n+ * This program is distributed in the hope that it will be useful,\n+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\n+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n+ * GNU General Public License for more details.\n+ *\n+ * You should have received a copy of the GNU General Public License\n+ * along with this program; if not, write to the Free Software\n+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\n+ */\n+#include \"git-compat-util.h\"\n+#include \"ewok.h\"\n+#include \"ewok_rlw.h\"\n+\n+static inline int next_word(struct rlw_iterator *it)\n+{\n+\tif (it->pointer >= it->size)\n+\t\treturn 0;\n+\n+\tit->rlw.word = &it->buffer[it->pointer];\n+\tit->pointer += rlw_get_literal_words(it->rlw.word) + 1;\n+\n+\tit->rlw.literal_words = rlw_get_literal_words(it->rlw.word);\n+\tit->rlw.running_len = rlw_get_running_len(it->rlw.word);\n+\tit->rlw.running_bit = rlw_get_run_bit(it->rlw.word);\n+\tit->rlw.literal_word_offset = 0;\n+\n+\treturn 1;\n+}\n+\n+void rlwit_init(struct rlw_iterator *it, struct ewah_bitmap *from_ewah)\n+{\n+\tit->buffer = from_ewah->buffer;\n+\tit->size = from_ewah->buffer_size;\n+\tit->pointer = 0;\n+\n+\tnext_word(it);\n+\n+\tit->literal_word_start = rlwit_literal_words(it) +\n+\t\tit->rlw.literal_word_offset;\n+}\n+\n+void rlwit_discard_first_words(struct rlw_iterator *it, size_t x)\n+{\n+\twhile (x > 0) {\n+\t\tsize_t discard;\n+\n+\t\tif (it->rlw.running_len > x) {\n+\t\t\tit->rlw.running_len -= x;\n+\t\t\treturn;\n+\t\t}\n+\n+\t\tx -= it->rlw.running_len;\n+\t\tit->rlw.running_len = 0;\n+\n+\t\tdiscard = (x > it->rlw.literal_words) ? it->rlw.literal_words : x;\n+\n+\t\tit->literal_word_start += discard;\n+\t\tit->rlw.literal_words -= discard;\n+\t\tx -= discard;\n+\n+\t\tif (x > 0 || rlwit_word_size(it) == 0) {\n+\t\t\tif (!next_word(it))\n+\t\t\t\tbreak;\n+\n+\t\t\tit->literal_word_start =\n+\t\t\t\trlwit_literal_words(it) + it->rlw.literal_word_offset;\n+\t\t}\n+\t}\n+}\n+\n+size_t rlwit_discharge(\n+\tstruct rlw_iterator *it, struct ewah_bitmap *out, size_t max, int negate)\n+{\n+\tsize_t index = 0;\n+\n+\twhile (index < max && rlwit_word_size(it) > 0) {\n+\t\tsize_t pd, pl = it->rlw.running_len;\n+\n+\t\tif (index + pl > max)\n+\t\t\tpl = max - index;\n+\n+\t\tewah_add_empty_words(out, it->rlw.running_bit ^ negate, pl);\n+\t\tindex += pl;\n+\n+\t\tpd = it->rlw.literal_words;\n+\t\tif (pd + index > max)\n+\t\t\tpd = max - index;\n+\n+\t\tewah_add_dirty_words(out,\n+\t\t\tit->buffer + it->literal_word_start, pd, negate);\n+\n+\t\trlwit_discard_first_words(it, pd + pl);\n+\t\tindex += pd;\n+\t}\n+\n+\treturn index;\n+}\n+\n+void rlwit_discharge_empty(struct rlw_iterator *it, struct ewah_bitmap *out)\n+{\n+\twhile (rlwit_word_size(it) > 0) {\n+\t\tewah_add_empty_words(out, 0, rlwit_word_size(it));\n+\t\trlwit_discard_first_words(it, rlwit_word_size(it));\n+\t}\n+}\ndiff --git a/ewah/ewok.h b/ewah/ewok.h\nnew file mode 100644\nindex 0000000..619afaa\n--- /dev/null\n+++ b/ewah/ewok.h\n@@ -0,0 +1,235 @@\n+/**\n+ * Copyright 2013, GitHub, Inc\n+ * Copyright 2009-2013, Daniel Lemire, Cliff Moon,\n+ *\tDavid McIntosh, Robert Becho, Google Inc. and Veronika Zenz\n+ *\n+ * This program is free software; you can redistribute it and/or\n+ * modify it under the terms of the GNU General Public License\n+ * as published by the Free Software Foundation; either version 2\n+ * of the License, or (at your option) any later version.\n+ *\n+ * This program is distributed in the hope that it will be useful,\n+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\n+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n+ * GNU General Public License for more details.\n+ *\n+ * You should have received a copy of the GNU General Public License\n+ * along with this program; if not, write to the Free Software\n+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\n+ */\n+#ifndef __EWOK_BITMAP_H__\n+#define __EWOK_BITMAP_H__\n+\n+#ifndef ewah_malloc\n+#\tdefine ewah_malloc xmalloc\n+#endif\n+#ifndef ewah_realloc\n+#\tdefine ewah_realloc xrealloc\n+#endif\n+#ifndef ewah_calloc\n+#\tdefine ewah_calloc xcalloc\n+#endif\n+\n+typedef uint64_t eword_t;\n+#define BITS_IN_WORD (sizeof(eword_t) * 8)\n+\n+/**\n+ * Do not use __builtin_popcountll. The GCC implementation\n+ * is notoriously slow on all platforms.\n+ *\n+ * See: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36041\n+ */\n+static inline uint32_t ewah_bit_popcount64(uint64_t x)\n+{\n+\tx = (x & 0x5555555555555555ULL) + ((x >>  1) & 0x5555555555555555ULL);\n+\tx = (x & 0x3333333333333333ULL) + ((x >>  2) & 0x3333333333333333ULL);\n+\tx = (x & 0x0F0F0F0F0F0F0F0FULL) + ((x >>  4) & 0x0F0F0F0F0F0F0F0FULL);\n+\treturn (x * 0x0101010101010101ULL) >> 56;\n+}\n+\n+#ifdef __GNUC__\n+#define ewah_bit_ctz64(x) __builtin_ctzll(x)\n+#else\n+static inline int ewah_bit_ctz64(uint64_t x)\n+{\n+\tint n = 0;\n+\tif ((x & 0xffffffff) == 0) { x >>= 32; n += 32; }\n+\tif ((x &     0xffff) == 0) { x >>= 16; n += 16; }\n+\tif ((x &       0xff) == 0) { x >>=  8; n +=  8; }\n+\tif ((x &        0xf) == 0) { x >>=  4; n +=  4; }\n+\tif ((x &        0x3) == 0) { x >>=  2; n +=  2; }\n+\tif ((x &        0x1) == 0) { x >>=  1; n +=  1; }\n+\treturn n + !x;\n+}\n+#endif\n+\n+struct ewah_bitmap {\n+\teword_t *buffer;\n+\tsize_t buffer_size;\n+\tsize_t alloc_size;\n+\tsize_t bit_size;\n+\teword_t *rlw;\n+};\n+\n+typedef void (*ewah_callback)(size_t pos, void *);\n+\n+struct ewah_bitmap *ewah_pool_new(void);\n+void ewah_pool_free(struct ewah_bitmap *self);\n+\n+/**\n+ * Allocate a new EWAH Compressed bitmap\n+ */\n+struct ewah_bitmap *ewah_new(void);\n+\n+/**\n+ * Clear all the bits in the bitmap. Does not free or resize\n+ * memory.\n+ */\n+void ewah_clear(struct ewah_bitmap *self);\n+\n+/**\n+ * Free all the memory of the bitmap\n+ */\n+void ewah_free(struct ewah_bitmap *self);\n+\n+int ewah_serialize_to(struct ewah_bitmap *self,\n+\t\t      int (*write_fun)(void *out, const void *buf, size_t len),\n+\t\t      void *out);\n+int ewah_serialize(struct ewah_bitmap *self, int fd);\n+int ewah_serialize_native(struct ewah_bitmap *self, int fd);\n+\n+int ewah_deserialize(struct ewah_bitmap *self, int fd);\n+int ewah_read_mmap(struct ewah_bitmap *self, void *map, size_t len);\n+int ewah_read_mmap_native(struct ewah_bitmap *self, void *map, size_t len);\n+\n+uint32_t ewah_checksum(struct ewah_bitmap *self);\n+\n+/**\n+ * Logical not (bitwise negation) in-place on the bitmap\n+ *\n+ * This operation is linear time based on the size of the bitmap.\n+ */\n+void ewah_not(struct ewah_bitmap *self);\n+\n+/**\n+ * Call the given callback with the position of every single bit\n+ * that has been set on the bitmap.\n+ *\n+ * This is an efficient operation that does not fully decompress\n+ * the bitmap.\n+ */\n+void ewah_each_bit(struct ewah_bitmap *self, ewah_callback callback, void *payload);\n+\n+/**\n+ * Set a given bit on the bitmap.\n+ *\n+ * The bit at position `pos` will be set to true. Because of the\n+ * way that the bitmap is compressed, a set bit cannot be unset\n+ * later on.\n+ *\n+ * Furthermore, since the bitmap uses streaming compression, bits\n+ * can only set incrementally.\n+ *\n+ * E.g.\n+ *\t\tewah_set(bitmap, 1); // ok\n+ *\t\tewah_set(bitmap, 76); // ok\n+ *\t\tewah_set(bitmap, 77); // ok\n+ *\t\tewah_set(bitmap, 8712800127); // ok\n+ *\t\tewah_set(bitmap, 25); // failed, assert raised\n+ */\n+void ewah_set(struct ewah_bitmap *self, size_t i);\n+\n+struct ewah_iterator {\n+\tconst eword_t *buffer;\n+\tsize_t buffer_size;\n+\n+\tsize_t pointer;\n+\teword_t compressed, literals;\n+\teword_t rl, lw;\n+\tint b;\n+};\n+\n+/**\n+ * Initialize a new iterator to run through the bitmap in uncompressed form.\n+ *\n+ * The iterator can be stack allocated. The underlying bitmap must not be freed\n+ * before the iteration is over.\n+ *\n+ * E.g.\n+ *\n+ *\t\tstruct ewah_bitmap *bitmap = ewah_new();\n+ *\t\tstruct ewah_iterator it;\n+ *\n+ *\t\tewah_iterator_init(&it, bitmap);\n+ */\n+void ewah_iterator_init(struct ewah_iterator *it, struct ewah_bitmap *parent);\n+\n+/**\n+ * Yield every single word in the bitmap in uncompressed form. This is:\n+ * yield single words (32-64 bits) where each bit represents an actual\n+ * bit from the bitmap.\n+ *\n+ * Return: true if a word was yield, false if there are no words left\n+ */\n+int ewah_iterator_next(eword_t *next, struct ewah_iterator *it);\n+\n+void ewah_or(\n+\tstruct ewah_bitmap *ewah_i,\n+\tstruct ewah_bitmap *ewah_j,\n+\tstruct ewah_bitmap *out);\n+\n+void ewah_and_not(\n+\tstruct ewah_bitmap *ewah_i,\n+\tstruct ewah_bitmap *ewah_j,\n+\tstruct ewah_bitmap *out);\n+\n+void ewah_xor(\n+\tstruct ewah_bitmap *ewah_i,\n+\tstruct ewah_bitmap *ewah_j,\n+\tstruct ewah_bitmap *out);\n+\n+void ewah_and(\n+\tstruct ewah_bitmap *ewah_i,\n+\tstruct ewah_bitmap *ewah_j,\n+\tstruct ewah_bitmap *out);\n+\n+void ewah_dump(struct ewah_bitmap *self);\n+\n+/**\n+ * Direct word access\n+ */\n+size_t ewah_add_empty_words(struct ewah_bitmap *self, int v, size_t number);\n+void ewah_add_dirty_words(\n+\tstruct ewah_bitmap *self, const eword_t *buffer, size_t number, int negate);\n+size_t ewah_add(struct ewah_bitmap *self, eword_t word);\n+\n+\n+/**\n+ * Uncompressed, old-school bitmap that can be efficiently compressed\n+ * into an `ewah_bitmap`.\n+ */\n+struct bitmap {\n+\teword_t *words;\n+\tsize_t word_alloc;\n+};\n+\n+struct bitmap *bitmap_new(void);\n+void bitmap_set(struct bitmap *self, size_t pos);\n+void bitmap_clear(struct bitmap *self, size_t pos);\n+int bitmap_get(struct bitmap *self, size_t pos);\n+void bitmap_reset(struct bitmap *self);\n+void bitmap_free(struct bitmap *self);\n+int bitmap_equals(struct bitmap *self, struct bitmap *other);\n+int bitmap_is_subset(struct bitmap *self, struct bitmap *super);\n+\n+struct ewah_bitmap * bitmap_to_ewah(struct bitmap *bitmap);\n+struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah);\n+\n+void bitmap_and_not(struct bitmap *self, struct bitmap *other);\n+void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other);\n+void bitmap_or(struct bitmap *self, const struct bitmap *other);\n+\n+void bitmap_each_bit(struct bitmap *self, ewah_callback callback, void *data);\n+size_t bitmap_popcount(struct bitmap *self);\n+\n+#endif\ndiff --git a/ewah/ewok_rlw.h b/ewah/ewok_rlw.h\nnew file mode 100644\nindex 0000000..63efdf9\n--- /dev/null\n+++ b/ewah/ewok_rlw.h\n@@ -0,0 +1,114 @@\n+/**\n+ * Copyright 2013, GitHub, Inc\n+ * Copyright 2009-2013, Daniel Lemire, Cliff Moon,\n+ *\tDavid McIntosh, Robert Becho, Google Inc. and Veronika Zenz\n+ *\n+ * This program is free software; you can redistribute it and/or\n+ * modify it under the terms of the GNU General Public License\n+ * as published by the Free Software Foundation; either version 2\n+ * of the License, or (at your option) any later version.\n+ *\n+ * This program is distributed in the hope that it will be useful,\n+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\n+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n+ * GNU General Public License for more details.\n+ *\n+ * You should have received a copy of the GNU General Public License\n+ * along with this program; if not, write to the Free Software\n+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\n+ */\n+#ifndef __EWOK_RLW_H__\n+#define __EWOK_RLW_H__\n+\n+#define RLW_RUNNING_BITS (sizeof(eword_t) * 4)\n+#define RLW_LITERAL_BITS (sizeof(eword_t) * 8 - 1 - RLW_RUNNING_BITS)\n+\n+#define RLW_LARGEST_RUNNING_COUNT (((eword_t)1 << RLW_RUNNING_BITS) - 1)\n+#define RLW_LARGEST_LITERAL_COUNT (((eword_t)1 << RLW_LITERAL_BITS) - 1)\n+\n+#define RLW_LARGEST_RUNNING_COUNT_SHIFT (RLW_LARGEST_RUNNING_COUNT << 1)\n+\n+#define RLW_RUNNING_LEN_PLUS_BIT (((eword_t)1 << (RLW_RUNNING_BITS + 1)) - 1)\n+\n+static int rlw_get_run_bit(const eword_t *word)\n+{\n+\treturn *word & (eword_t)1;\n+}\n+\n+static inline void rlw_set_run_bit(eword_t *word, int b)\n+{\n+\tif (b) {\n+\t\t*word |= (eword_t)1;\n+\t} else {\n+\t\t*word &= (eword_t)(~1);\n+\t}\n+}\n+\n+static inline void rlw_xor_run_bit(eword_t *word)\n+{\n+\tif (*word & 1) {\n+\t\t*word &= (eword_t)(~1);\n+\t} else {\n+\t\t*word |= (eword_t)1;\n+\t}\n+}\n+\n+static inline void rlw_set_running_len(eword_t *word, eword_t l)\n+{\n+\t*word |= RLW_LARGEST_RUNNING_COUNT_SHIFT;\n+\t*word &= (l << 1) | (~RLW_LARGEST_RUNNING_COUNT_SHIFT);\n+}\n+\n+static inline eword_t rlw_get_running_len(const eword_t *word)\n+{\n+\treturn (*word >> 1) & RLW_LARGEST_RUNNING_COUNT;\n+}\n+\n+static inline eword_t rlw_get_literal_words(const eword_t *word)\n+{\n+\treturn *word >> (1 + RLW_RUNNING_BITS);\n+}\n+\n+static inline void rlw_set_literal_words(eword_t *word, eword_t l)\n+{\n+\t*word |= ~RLW_RUNNING_LEN_PLUS_BIT;\n+\t*word &= (l << (RLW_RUNNING_BITS + 1)) | RLW_RUNNING_LEN_PLUS_BIT;\n+}\n+\n+static inline eword_t rlw_size(const eword_t *self)\n+{\n+\treturn rlw_get_running_len(self) + rlw_get_literal_words(self);\n+}\n+\n+struct rlw_iterator {\n+\tconst eword_t *buffer;\n+\tsize_t size;\n+\tsize_t pointer;\n+\tsize_t literal_word_start;\n+\n+\tstruct {\n+\t\tconst eword_t *word;\n+\t\tint literal_words;\n+\t\tint running_len;\n+\t\tint literal_word_offset;\n+\t\tint running_bit;\n+\t} rlw;\n+};\n+\n+void rlwit_init(struct rlw_iterator *it, struct ewah_bitmap *bitmap);\n+void rlwit_discard_first_words(struct rlw_iterator *it, size_t x);\n+size_t rlwit_discharge(\n+\tstruct rlw_iterator *it, struct ewah_bitmap *out, size_t max, int negate);\n+void rlwit_discharge_empty(struct rlw_iterator *it, struct ewah_bitmap *out);\n+\n+static inline size_t rlwit_word_size(struct rlw_iterator *it)\n+{\n+\treturn it->rlw.running_len + it->rlw.literal_words;\n+}\n+\n+static inline size_t rlwit_literal_words(struct rlw_iterator *it)\n+{\n+\treturn it->pointer - it->rlw.literal_words;\n+}\n+\n+#endif\n-- \n1.8.5.1.399.g900e7cd\n"},{"id":"232316","messageId":"20131221135957.GI21145@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20131221135651.GA20818@sigill.intra.peff.net","subject":"[PATCH v4 09/23] documentation: add documentation for the bitmap format","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-12-21T13:59:58Z","receivedAt":"2013-12-21T13:59:58Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"From: Vicent Marti <tanoku@gmail.com>\n\nThis is the technical documentation for the JGit-compatible Bitmap v1\non-disk format.\n\nSigned-off-by: Vicent Marti <tanoku@gmail.com>\nSigned-off-by: Jeff King <peff@peff.net>\n---\n Documentation/technical/bitmap-format.txt | 131 ++++++++++++++++++++++++++++++\n 1 file changed, 131 insertions(+)\n create mode 100644 Documentation/technical/bitmap-format.txt\n\ndiff --git a/Documentation/technical/bitmap-format.txt b/Documentation/technical/bitmap-format.txt\nnew file mode 100644\nindex 0000000..7a86bd7\n--- /dev/null\n+++ b/Documentation/technical/bitmap-format.txt\n@@ -0,0 +1,131 @@\n+GIT bitmap v1 format\n+====================\n+\n+\t- A header appears at the beginning:\n+\n+\t\t4-byte signature: {'B', 'I', 'T', 'M'}\n+\n+\t\t2-byte version number (network byte order)\n+\t\t\tThe current implementation only supports version 1\n+\t\t\tof the bitmap index (the same one as JGit).\n+\n+\t\t2-byte flags (network byte order)\n+\n+\t\t\tThe following flags are supported:\n+\n+\t\t\t- BITMAP_OPT_FULL_DAG (0x1) REQUIRED\n+\t\t\tThis flag must always be present. It implies that the bitmap\n+\t\t\tindex has been generated for a packfile with full closure\n+\t\t\t(i.e. where every single object in the packfile can find\n+\t\t\t its parent links inside the same packfile). This is a\n+\t\t\trequirement for the bitmap index format, also present in JGit,\n+\t\t\tthat greatly reduces the complexity of the implementation.\n+\n+\t\t4-byte entry count (network byte order)\n+\n+\t\t\tThe total count of entries (bitmapped commits) in this bitmap index.\n+\n+\t\t20-byte checksum\n+\n+\t\t\tThe SHA1 checksum of the pack this bitmap index belongs to.\n+\n+\t- 4 EWAH bitmaps that act as type indexes\n+\n+\t\tType indexes are serialized after the hash cache in the shape\n+\t\tof four EWAH bitmaps stored consecutively (see Appendix A for\n+\t\tthe serialization format of an EWAH bitmap).\n+\n+\t\tThere is a bitmap for each Git object type, stored in the following\n+\t\torder:\n+\n+\t\t\t- Commits\n+\t\t\t- Trees\n+\t\t\t- Blobs\n+\t\t\t- Tags\n+\n+\t\tIn each bitmap, the `n`th bit is set to true if the `n`th object\n+\t\tin the packfile is of that type.\n+\n+\t\tThe obvious consequence is that the OR of all 4 bitmaps will result\n+\t\tin a full set (all bits set), and the AND of all 4 bitmaps will\n+\t\tresult in an empty bitmap (no bits set).\n+\n+\t- N entries with compressed bitmaps, one for each indexed commit\n+\n+\t\tWhere `N` is the total amount of entries in this bitmap index.\n+\t\tEach entry contains the following:\n+\n+\t\t- 4-byte object position (network byte order)\n+\t\t\tThe position **in the index for the packfile** where the\n+\t\t\tbitmap for this commit is found.\n+\n+\t\t- 1-byte XOR-offset\n+\t\t\tThe xor offset used to compress this bitmap. For an entry\n+\t\t\tin position `x`, a XOR offset of `y` means that the actual\n+\t\t\tbitmap representing this commit is composed by XORing the\n+\t\t\tbitmap for this entry with the bitmap in entry `x-y` (i.e.\n+\t\t\tthe bitmap `y` entries before this one).\n+\n+\t\t\tNote that this compression can be recursive. In order to\n+\t\t\tXOR this entry with a previous one, the previous entry needs\n+\t\t\tto be decompressed first, and so on.\n+\n+\t\t\tThe hard-limit for this offset is 160 (an entry can only be\n+\t\t\txor'ed against one of the 160 entries preceding it). This\n+\t\t\tnumber is always positive, and hence entries are always xor'ed\n+\t\t\twith **previous** bitmaps, not bitmaps that will come afterwards\n+\t\t\tin the index.\n+\n+\t\t- 1-byte flags for this bitmap\n+\t\t\tAt the moment the only available flag is `0x1`, which hints\n+\t\t\tthat this bitmap can be re-used when rebuilding bitmap indexes\n+\t\t\tfor the repository.\n+\n+\t\t- The compressed bitmap itself, see Appendix A.\n+\n+== Appendix A: Serialization format for an EWAH bitmap\n+\n+Ewah bitmaps are serialized in the same protocol as the JAVAEWAH\n+library, making them backwards compatible with the JGit\n+implementation:\n+\n+\t- 4-byte number of bits of the resulting UNCOMPRESSED bitmap\n+\n+\t- 4-byte number of words of the COMPRESSED bitmap, when stored\n+\n+\t- N x 8-byte words, as specified by the previous field\n+\n+\t\tThis is the actual content of the compressed bitmap.\n+\n+\t- 4-byte position of the current RLW for the compressed\n+\t\tbitmap\n+\n+All words are stored in network byte order for their corresponding\n+sizes.\n+\n+The compressed bitmap is stored in a form of run-length encoding, as\n+follows.  It consists of a concatenation of an arbitrary number of\n+chunks.  Each chunk consists of one or more 64-bit words\n+\n+     H  L_1  L_2  L_3 .... L_M\n+\n+H is called RLW (run length word).  It consists of (from lower to higher\n+order bits):\n+\n+     - 1 bit: the repeated bit B\n+\n+     - 32 bits: repetition count K (unsigned)\n+\n+     - 31 bits: literal word count M (unsigned)\n+\n+The bitstream represented by the above chunk is then:\n+\n+     - K repetitions of B\n+\n+     - The bits stored in `L_1` through `L_M`.  Within a word, bits at\n+       lower order come earlier in the stream than those at higher\n+       order.\n+\n+The next word after `L_M` (if any) must again be a RLW, for the next\n+chunk.  For efficient appending to the bitstream, the EWAH stores a\n+pointer to the last RLW in the stream.\n-- \n1.8.5.1.399.g900e7cd\n"},{"id":"232314","messageId":"20131221140001.GJ21145@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20131221135651.GA20818@sigill.intra.peff.net","subject":"[PATCH v4 10/23] pack-bitmap: add support for bitmap indexes","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-12-21T14:00:01Z","receivedAt":"2013-12-21T14:00:01Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"From: Vicent Marti <tanoku@gmail.com>\n\nA bitmap index is a `.bitmap` file that can be found inside\n`$GIT_DIR/objects/pack/`, next to its corresponding packfile, and\ncontains precalculated reachability information for selected commits.\nThe full specification of the format for these bitmap indexes can be found\nin `Documentation/technical/bitmap-format.txt`.\n\nFor a given commit SHA1, if it happens to be available in the bitmap\nindex, its bitmap will represent every single object that is reachable\nfrom the commit itself. The nth bit in the bitmap is the nth object in\nthe packfile; if it's set to 1, the object is reachable.\n\nBy using the bitmaps available in the index, this commit implements\nseveral new functions:\n\n\t- `prepare_bitmap_git`\n\t- `prepare_bitmap_walk`\n\t- `traverse_bitmap_commit_list`\n\t- `reuse_partial_packfile_from_bitmap`\n\nThe `prepare_bitmap_walk` function tries to build a bitmap of all the\nobjects that can be reached from the commit roots of a given `rev_info`\nstruct by using the following algorithm:\n\n- If all the interesting commits for a revision walk are available in\nthe index, the resulting reachability bitmap is the bitwise OR of all\nthe individual bitmaps.\n\n- When the full set of WANTs is not available in the index, we perform a\npartial revision walk using the commits that don't have bitmaps as\nroots, and limiting the revision walk as soon as we reach a commit that\nhas a corresponding bitmap. The earlier OR'ed bitmap with all the\nindexed commits can now be completed as this walk progresses, so the end\nresult is the full reachability list.\n\n- For revision walks with a HAVEs set (a set of commits that are deemed\nuninteresting), first we perform the same method as for the WANTs, but\nusing our HAVEs as roots, in order to obtain a full reachability bitmap\nof all the uninteresting commits. This bitmap then can be used to:\n\n\ta) limit the subsequent walk when building the WANTs bitmap\n\tb) finding the final set of interesting commits by performing an\n\t   AND-NOT of the WANTs and the HAVEs.\n\nIf `prepare_bitmap_walk` runs successfully, the resulting bitmap is\nstored and the equivalent of a `traverse_commit_list` call can be\nperformed by using `traverse_bitmap_commit_list`; the bitmap version\nof this call yields the objects straight from the packfile index\n(without having to look them up or parse them) and hence is several\norders of magnitude faster.\n\nAs an extra optimization, when `prepare_bitmap_walk` succeeds, the\n`reuse_partial_packfile_from_bitmap` call can be attempted: it will find\nthe amount of objects at the beginning of the on-disk packfile that can\nbe reused as-is, and return an offset into the packfile. The source\npackfile can then be loaded and the bytes up to `offset` can be written\ndirectly to the result without having to consider the entires inside the\npackfile individually.\n\nIf the `prepare_bitmap_walk` call fails (e.g. because no bitmap files\nare available), the `rev_info` struct is left untouched, and can be used\nto perform a manual rev-walk using `traverse_commit_list`.\n\nHence, this new set of functions are a generic API that allows to\nperform the equivalent of\n\n\tgit rev-list --objects [roots...] [^uninteresting...]\n\nfor any set of commits, even if they don't have specific bitmaps\ngenerated for them.\n\nIn further patches, we'll use this bitmap traversal optimization to\nspeed up the `pack-objects` and `rev-list` commands.\n\nSigned-off-by: Vicent Marti <tanoku@gmail.com>\nSigned-off-by: Jeff King <peff@peff.net>\n---\n Makefile      |   2 +\n khash.h       | 338 ++++++++++++++++++++\n pack-bitmap.c | 970 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n pack-bitmap.h |  43 +++\n 4 files changed, 1353 insertions(+)\n create mode 100644 khash.h\n create mode 100644 pack-bitmap.c\n create mode 100644 pack-bitmap.h\n\ndiff --git a/Makefile b/Makefile\nindex 64a1ed7..b983d78 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -699,6 +699,7 @@ LIB_H += object.h\n LIB_H += pack-objects.h\n LIB_H += pack-revindex.h\n LIB_H += pack.h\n+LIB_H += pack-bitmap.h\n LIB_H += parse-options.h\n LIB_H += patch-ids.h\n LIB_H += pathspec.h\n@@ -837,6 +838,7 @@ LIB_OBJS += notes-cache.o\n LIB_OBJS += notes-merge.o\n LIB_OBJS += notes-utils.o\n LIB_OBJS += object.o\n+LIB_OBJS += pack-bitmap.o\n LIB_OBJS += pack-check.o\n LIB_OBJS += pack-objects.o\n LIB_OBJS += pack-revindex.o\ndiff --git a/khash.h b/khash.h\nnew file mode 100644\nindex 0000000..57ff603\n--- /dev/null\n+++ b/khash.h\n@@ -0,0 +1,338 @@\n+/* The MIT License\n+\n+   Copyright (c) 2008, 2009, 2011 by Attractive Chaos <attractor@live.co.uk>\n+\n+   Permission is hereby granted, free of charge, to any person obtaining\n+   a copy of this software and associated documentation files (the\n+   \"Software\"), to deal in the Software without restriction, including\n+   without limitation the rights to use, copy, modify, merge, publish,\n+   distribute, sublicense, and/or sell copies of the Software, and to\n+   permit persons to whom the Software is furnished to do so, subject to\n+   the following conditions:\n+\n+   The above copyright notice and this permission notice shall be\n+   included in all copies or substantial portions of the Software.\n+\n+   THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n+   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n+   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n+   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\n+   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\n+   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\n+   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n+   SOFTWARE.\n+*/\n+\n+#ifndef __AC_KHASH_H\n+#define __AC_KHASH_H\n+\n+#define AC_VERSION_KHASH_H \"0.2.8\"\n+\n+typedef uint32_t khint32_t;\n+typedef uint64_t khint64_t;\n+\n+typedef khint32_t khint_t;\n+typedef khint_t khiter_t;\n+\n+#define __ac_isempty(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&2)\n+#define __ac_isdel(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&1)\n+#define __ac_iseither(flag, i) ((flag[i>>4]>>((i&0xfU)<<1))&3)\n+#define __ac_set_isdel_false(flag, i) (flag[i>>4]&=~(1ul<<((i&0xfU)<<1)))\n+#define __ac_set_isempty_false(flag, i) (flag[i>>4]&=~(2ul<<((i&0xfU)<<1)))\n+#define __ac_set_isboth_false(flag, i) (flag[i>>4]&=~(3ul<<((i&0xfU)<<1)))\n+#define __ac_set_isdel_true(flag, i) (flag[i>>4]|=1ul<<((i&0xfU)<<1))\n+\n+#define __ac_fsize(m) ((m) < 16? 1 : (m)>>4)\n+\n+#define kroundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))\n+\n+static inline khint_t __ac_X31_hash_string(const char *s)\n+{\n+\tkhint_t h = (khint_t)*s;\n+\tif (h) for (++s ; *s; ++s) h = (h << 5) - h + (khint_t)*s;\n+\treturn h;\n+}\n+\n+#define kh_str_hash_func(key) __ac_X31_hash_string(key)\n+#define kh_str_hash_equal(a, b) (strcmp(a, b) == 0)\n+\n+static const double __ac_HASH_UPPER = 0.77;\n+\n+#define __KHASH_TYPE(name, khkey_t, khval_t) \\\n+\ttypedef struct { \\\n+\t\tkhint_t n_buckets, size, n_occupied, upper_bound; \\\n+\t\tkhint32_t *flags; \\\n+\t\tkhkey_t *keys; \\\n+\t\tkhval_t *vals; \\\n+\t} kh_##name##_t;\n+\n+#define __KHASH_PROTOTYPES(name, khkey_t, khval_t)\t \t\t\t\t\t\\\n+\textern kh_##name##_t *kh_init_##name(void);\t\t\t\t\t\t\t\\\n+\textern void kh_destroy_##name(kh_##name##_t *h);\t\t\t\t\t\\\n+\textern void kh_clear_##name(kh_##name##_t *h);\t\t\t\t\t\t\\\n+\textern khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key); \t\\\n+\textern int kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets); \\\n+\textern khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret); \\\n+\textern void kh_del_##name(kh_##name##_t *h, khint_t x);\n+\n+#define __KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \\\n+\tSCOPE kh_##name##_t *kh_init_##name(void) {\t\t\t\t\t\t\t\\\n+\t\treturn (kh_##name##_t*)xcalloc(1, sizeof(kh_##name##_t));\t\t\\\n+\t}\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\tSCOPE void kh_destroy_##name(kh_##name##_t *h)\t\t\t\t\t\t\\\n+\t{\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\tif (h) {\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t\tfree((void *)h->keys); free(h->flags);\t\t\t\t\t\\\n+\t\t\tfree((void *)h->vals);\t\t\t\t\t\t\t\t\t\t\\\n+\t\t\tfree(h);\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t}\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t}\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\tSCOPE void kh_clear_##name(kh_##name##_t *h)\t\t\t\t\t\t\\\n+\t{\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\tif (h && h->flags) {\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t\tmemset(h->flags, 0xaa, __ac_fsize(h->n_buckets) * sizeof(khint32_t)); \\\n+\t\t\th->size = h->n_occupied = 0;\t\t\t\t\t\t\t\t\\\n+\t\t}\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t}\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\tSCOPE khint_t kh_get_##name(const kh_##name##_t *h, khkey_t key) \t\\\n+\t{\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\tif (h->n_buckets) {\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t\tkhint_t k, i, last, mask, step = 0; \\\n+\t\t\tmask = h->n_buckets - 1;\t\t\t\t\t\t\t\t\t\\\n+\t\t\tk = __hash_func(key); i = k & mask;\t\t\t\t\t\t\t\\\n+\t\t\tlast = i; \\\n+\t\t\twhile (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \\\n+\t\t\t\ti = (i + (++step)) & mask; \\\n+\t\t\t\tif (i == last) return h->n_buckets;\t\t\t\t\t\t\\\n+\t\t\t}\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t\treturn __ac_iseither(h->flags, i)? h->n_buckets : i;\t\t\\\n+\t\t} else return 0;\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t}\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\tSCOPE int kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets) \\\n+\t{ /* This function uses 0.25*n_buckets bytes of working space instead of [sizeof(key_t+val_t)+.25]*n_buckets. */ \\\n+\t\tkhint32_t *new_flags = NULL;\t\t\t\t\t\t\t\t\t\t\\\n+\t\tkhint_t j = 1;\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t{\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t\tkroundup32(new_n_buckets); \t\t\t\t\t\t\t\t\t\\\n+\t\t\tif (new_n_buckets < 4) new_n_buckets = 4;\t\t\t\t\t\\\n+\t\t\tif (h->size >= (khint_t)(new_n_buckets * __ac_HASH_UPPER + 0.5)) j = 0;\t/* requested size is too small */ \\\n+\t\t\telse { /* hash table size to be changed (shrink or expand); rehash */ \\\n+\t\t\t\tnew_flags = (khint32_t*)xmalloc(__ac_fsize(new_n_buckets) * sizeof(khint32_t));\t\\\n+\t\t\t\tif (!new_flags) return -1;\t\t\t\t\t\t\t\t\\\n+\t\t\t\tmemset(new_flags, 0xaa, __ac_fsize(new_n_buckets) * sizeof(khint32_t)); \\\n+\t\t\t\tif (h->n_buckets < new_n_buckets) {\t/* expand */\t\t\\\n+\t\t\t\t\tkhkey_t *new_keys = (khkey_t*)xrealloc((void *)h->keys, new_n_buckets * sizeof(khkey_t)); \\\n+\t\t\t\t\tif (!new_keys) return -1;\t\t\t\t\t\t\t\\\n+\t\t\t\t\th->keys = new_keys;\t\t\t\t\t\t\t\t\t\\\n+\t\t\t\t\tif (kh_is_map) {\t\t\t\t\t\t\t\t\t\\\n+\t\t\t\t\t\tkhval_t *new_vals = (khval_t*)xrealloc((void *)h->vals, new_n_buckets * sizeof(khval_t)); \\\n+\t\t\t\t\t\tif (!new_vals) return -1;\t\t\t\t\t\t\\\n+\t\t\t\t\t\th->vals = new_vals;\t\t\t\t\t\t\t\t\\\n+\t\t\t\t\t}\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t\t\t} /* otherwise shrink */\t\t\t\t\t\t\t\t\\\n+\t\t\t}\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t}\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\tif (j) { /* rehashing is needed */\t\t\t\t\t\t\t\t\\\n+\t\t\tfor (j = 0; j != h->n_buckets; ++j) {\t\t\t\t\t\t\\\n+\t\t\t\tif (__ac_iseither(h->flags, j) == 0) {\t\t\t\t\t\\\n+\t\t\t\t\tkhkey_t key = h->keys[j];\t\t\t\t\t\t\t\\\n+\t\t\t\t\tkhval_t val;\t\t\t\t\t\t\t\t\t\t\\\n+\t\t\t\t\tkhint_t new_mask;\t\t\t\t\t\t\t\t\t\\\n+\t\t\t\t\tnew_mask = new_n_buckets - 1; \t\t\t\t\t\t\\\n+\t\t\t\t\tif (kh_is_map) val = h->vals[j];\t\t\t\t\t\\\n+\t\t\t\t\t__ac_set_isdel_true(h->flags, j);\t\t\t\t\t\\\n+\t\t\t\t\twhile (1) { /* kick-out process; sort of like in Cuckoo hashing */ \\\n+\t\t\t\t\t\tkhint_t k, i, step = 0; \\\n+\t\t\t\t\t\tk = __hash_func(key);\t\t\t\t\t\t\t\\\n+\t\t\t\t\t\ti = k & new_mask;\t\t\t\t\t\t\t\t\\\n+\t\t\t\t\t\twhile (!__ac_isempty(new_flags, i)) i = (i + (++step)) & new_mask; \\\n+\t\t\t\t\t\t__ac_set_isempty_false(new_flags, i);\t\t\t\\\n+\t\t\t\t\t\tif (i < h->n_buckets && __ac_iseither(h->flags, i) == 0) { /* kick out the existing element */ \\\n+\t\t\t\t\t\t\t{ khkey_t tmp = h->keys[i]; h->keys[i] = key; key = tmp; } \\\n+\t\t\t\t\t\t\tif (kh_is_map) { khval_t tmp = h->vals[i]; h->vals[i] = val; val = tmp; } \\\n+\t\t\t\t\t\t\t__ac_set_isdel_true(h->flags, i); /* mark it as deleted in the old hash table */ \\\n+\t\t\t\t\t\t} else { /* write the element and jump out of the loop */ \\\n+\t\t\t\t\t\t\th->keys[i] = key;\t\t\t\t\t\t\t\\\n+\t\t\t\t\t\t\tif (kh_is_map) h->vals[i] = val;\t\t\t\\\n+\t\t\t\t\t\t\tbreak;\t\t\t\t\t\t\t\t\t\t\\\n+\t\t\t\t\t\t}\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t\t\t\t}\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t\t\t}\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t\t}\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t\tif (h->n_buckets > new_n_buckets) { /* shrink the hash table */ \\\n+\t\t\t\th->keys = (khkey_t*)xrealloc((void *)h->keys, new_n_buckets * sizeof(khkey_t)); \\\n+\t\t\t\tif (kh_is_map) h->vals = (khval_t*)xrealloc((void *)h->vals, new_n_buckets * sizeof(khval_t)); \\\n+\t\t\t}\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t\tfree(h->flags); /* free the working space */\t\t\t\t\\\n+\t\t\th->flags = new_flags;\t\t\t\t\t\t\t\t\t\t\\\n+\t\t\th->n_buckets = new_n_buckets;\t\t\t\t\t\t\t\t\\\n+\t\t\th->n_occupied = h->size;\t\t\t\t\t\t\t\t\t\\\n+\t\t\th->upper_bound = (khint_t)(h->n_buckets * __ac_HASH_UPPER + 0.5); \\\n+\t\t}\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\treturn 0;\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t}\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\tSCOPE khint_t kh_put_##name(kh_##name##_t *h, khkey_t key, int *ret) \\\n+\t{\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\tkhint_t x;\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\tif (h->n_occupied >= h->upper_bound) { /* update the hash table */ \\\n+\t\t\tif (h->n_buckets > (h->size<<1)) {\t\t\t\t\t\t\t\\\n+\t\t\t\tif (kh_resize_##name(h, h->n_buckets - 1) < 0) { /* clear \"deleted\" elements */ \\\n+\t\t\t\t\t*ret = -1; return h->n_buckets;\t\t\t\t\t\t\\\n+\t\t\t\t}\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t\t} else if (kh_resize_##name(h, h->n_buckets + 1) < 0) { /* expand the hash table */ \\\n+\t\t\t\t*ret = -1; return h->n_buckets;\t\t\t\t\t\t\t\\\n+\t\t\t}\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t} /* TODO: to implement automatically shrinking; resize() already support shrinking */ \\\n+\t\t{\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t\tkhint_t k, i, site, last, mask = h->n_buckets - 1, step = 0; \\\n+\t\t\tx = site = h->n_buckets; k = __hash_func(key); i = k & mask; \\\n+\t\t\tif (__ac_isempty(h->flags, i)) x = i; /* for speed up */\t\\\n+\t\t\telse {\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t\t\tlast = i; \\\n+\t\t\t\twhile (!__ac_isempty(h->flags, i) && (__ac_isdel(h->flags, i) || !__hash_equal(h->keys[i], key))) { \\\n+\t\t\t\t\tif (__ac_isdel(h->flags, i)) site = i;\t\t\t\t\\\n+\t\t\t\t\ti = (i + (++step)) & mask; \\\n+\t\t\t\t\tif (i == last) { x = site; break; }\t\t\t\t\t\\\n+\t\t\t\t}\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t\t\tif (x == h->n_buckets) {\t\t\t\t\t\t\t\t\\\n+\t\t\t\t\tif (__ac_isempty(h->flags, i) && site != h->n_buckets) x = site; \\\n+\t\t\t\t\telse x = i;\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t\t\t}\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t\t}\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t}\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\tif (__ac_isempty(h->flags, x)) { /* not present at all */\t\t\\\n+\t\t\th->keys[x] = key;\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t\t__ac_set_isboth_false(h->flags, x);\t\t\t\t\t\t\t\\\n+\t\t\t++h->size; ++h->n_occupied;\t\t\t\t\t\t\t\t\t\\\n+\t\t\t*ret = 1;\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t} else if (__ac_isdel(h->flags, x)) { /* deleted */\t\t\t\t\\\n+\t\t\th->keys[x] = key;\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t\t__ac_set_isboth_false(h->flags, x);\t\t\t\t\t\t\t\\\n+\t\t\t++h->size;\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t\t*ret = 2;\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t} else *ret = 0; /* Don't touch h->keys[x] if present and not deleted */ \\\n+\t\treturn x;\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t}\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\tSCOPE void kh_del_##name(kh_##name##_t *h, khint_t x)\t\t\t\t\\\n+\t{\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\tif (x != h->n_buckets && !__ac_iseither(h->flags, x)) {\t\t\t\\\n+\t\t\t__ac_set_isdel_true(h->flags, x);\t\t\t\t\t\t\t\\\n+\t\t\t--h->size;\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t\t}\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t}\n+\n+#define KHASH_DECLARE(name, khkey_t, khval_t)\t\t \t\t\t\t\t\\\n+\t__KHASH_TYPE(name, khkey_t, khval_t) \t\t\t\t\t\t\t\t\\\n+\t__KHASH_PROTOTYPES(name, khkey_t, khval_t)\n+\n+#define KHASH_INIT2(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \\\n+\t__KHASH_TYPE(name, khkey_t, khval_t) \t\t\t\t\t\t\t\t\\\n+\t__KHASH_IMPL(name, SCOPE, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal)\n+\n+#define KHASH_INIT(name, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \\\n+\tKHASH_INIT2(name, static inline, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal)\n+\n+/* Other convenient macros... */\n+\n+/*! @function\n+  @abstract     Test whether a bucket contains data.\n+  @param  h     Pointer to the hash table [khash_t(name)*]\n+  @param  x     Iterator to the bucket [khint_t]\n+  @return       1 if containing data; 0 otherwise [int]\n+ */\n+#define kh_exist(h, x) (!__ac_iseither((h)->flags, (x)))\n+\n+/*! @function\n+  @abstract     Get key given an iterator\n+  @param  h     Pointer to the hash table [khash_t(name)*]\n+  @param  x     Iterator to the bucket [khint_t]\n+  @return       Key [type of keys]\n+ */\n+#define kh_key(h, x) ((h)->keys[x])\n+\n+/*! @function\n+  @abstract     Get value given an iterator\n+  @param  h     Pointer to the hash table [khash_t(name)*]\n+  @param  x     Iterator to the bucket [khint_t]\n+  @return       Value [type of values]\n+  @discussion   For hash sets, calling this results in segfault.\n+ */\n+#define kh_val(h, x) ((h)->vals[x])\n+\n+/*! @function\n+  @abstract     Alias of kh_val()\n+ */\n+#define kh_value(h, x) ((h)->vals[x])\n+\n+/*! @function\n+  @abstract     Get the start iterator\n+  @param  h     Pointer to the hash table [khash_t(name)*]\n+  @return       The start iterator [khint_t]\n+ */\n+#define kh_begin(h) (khint_t)(0)\n+\n+/*! @function\n+  @abstract     Get the end iterator\n+  @param  h     Pointer to the hash table [khash_t(name)*]\n+  @return       The end iterator [khint_t]\n+ */\n+#define kh_end(h) ((h)->n_buckets)\n+\n+/*! @function\n+  @abstract     Get the number of elements in the hash table\n+  @param  h     Pointer to the hash table [khash_t(name)*]\n+  @return       Number of elements in the hash table [khint_t]\n+ */\n+#define kh_size(h) ((h)->size)\n+\n+/*! @function\n+  @abstract     Get the number of buckets in the hash table\n+  @param  h     Pointer to the hash table [khash_t(name)*]\n+  @return       Number of buckets in the hash table [khint_t]\n+ */\n+#define kh_n_buckets(h) ((h)->n_buckets)\n+\n+/*! @function\n+  @abstract     Iterate over the entries in the hash table\n+  @param  h     Pointer to the hash table [khash_t(name)*]\n+  @param  kvar  Variable to which key will be assigned\n+  @param  vvar  Variable to which value will be assigned\n+  @param  code  Block of code to execute\n+ */\n+#define kh_foreach(h, kvar, vvar, code) { khint_t __i;\t\t\\\n+\tfor (__i = kh_begin(h); __i != kh_end(h); ++__i) {\t\t\\\n+\t\tif (!kh_exist(h,__i)) continue;\t\t\t\t\t\t\\\n+\t\t(kvar) = kh_key(h,__i);\t\t\t\t\t\t\t\t\\\n+\t\t(vvar) = kh_val(h,__i);\t\t\t\t\t\t\t\t\\\n+\t\tcode;\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t} }\n+\n+/*! @function\n+  @abstract     Iterate over the values in the hash table\n+  @param  h     Pointer to the hash table [khash_t(name)*]\n+  @param  vvar  Variable to which value will be assigned\n+  @param  code  Block of code to execute\n+ */\n+#define kh_foreach_value(h, vvar, code) { khint_t __i;\t\t\\\n+\tfor (__i = kh_begin(h); __i != kh_end(h); ++__i) {\t\t\\\n+\t\tif (!kh_exist(h,__i)) continue;\t\t\t\t\t\t\\\n+\t\t(vvar) = kh_val(h,__i);\t\t\t\t\t\t\t\t\\\n+\t\tcode;\t\t\t\t\t\t\t\t\t\t\t\t\\\n+\t} }\n+\n+static inline khint_t __kh_oid_hash(const unsigned char *oid)\n+{\n+\tkhint_t hash;\n+\tmemcpy(&hash, oid, sizeof(hash));\n+\treturn hash;\n+}\n+\n+#define __kh_oid_cmp(a, b) (hashcmp(a, b) == 0)\n+\n+KHASH_INIT(sha1, const unsigned char *, void *, 1, __kh_oid_hash, __kh_oid_cmp)\n+typedef kh_sha1_t khash_sha1;\n+\n+KHASH_INIT(sha1_pos, const unsigned char *, int, 1, __kh_oid_hash, __kh_oid_cmp)\n+typedef kh_sha1_pos_t khash_sha1_pos;\n+\n+#endif /* __AC_KHASH_H */\ndiff --git a/pack-bitmap.c b/pack-bitmap.c\nnew file mode 100644\nindex 0000000..33e7482\n--- /dev/null\n+++ b/pack-bitmap.c\n@@ -0,0 +1,970 @@\n+#include \"cache.h\"\n+#include \"commit.h\"\n+#include \"tag.h\"\n+#include \"diff.h\"\n+#include \"revision.h\"\n+#include \"progress.h\"\n+#include \"list-objects.h\"\n+#include \"pack.h\"\n+#include \"pack-bitmap.h\"\n+#include \"pack-revindex.h\"\n+#include \"pack-objects.h\"\n+\n+/*\n+ * An entry on the bitmap index, representing the bitmap for a given\n+ * commit.\n+ */\n+struct stored_bitmap {\n+\tunsigned char sha1[20];\n+\tstruct ewah_bitmap *root;\n+\tstruct stored_bitmap *xor;\n+\tint flags;\n+};\n+\n+/*\n+ * The currently active bitmap index. By design, repositories only have\n+ * a single bitmap index available (the index for the biggest packfile in\n+ * the repository), since bitmap indexes need full closure.\n+ *\n+ * If there is more than one bitmap index available (e.g. because of alternates),\n+ * the active bitmap index is the largest one.\n+ */\n+static struct bitmap_index {\n+\t/* Packfile to which this bitmap index belongs to */\n+\tstruct packed_git *pack;\n+\n+\t/* reverse index for the packfile */\n+\tstruct pack_revindex *reverse_index;\n+\n+\t/*\n+\t * Mark the first `reuse_objects` in the packfile as reused:\n+\t * they will be sent as-is without using them for repacking\n+\t * calculations\n+\t */\n+\tuint32_t reuse_objects;\n+\n+\t/* mmapped buffer of the whole bitmap index */\n+\tunsigned char *map;\n+\tsize_t map_size; /* size of the mmaped buffer */\n+\tsize_t map_pos; /* current position when loading the index */\n+\n+\t/*\n+\t * Type indexes.\n+\t *\n+\t * Each bitmap marks which objects in the packfile  are of the given\n+\t * type. This provides type information when yielding the objects from\n+\t * the packfile during a walk, which allows for better delta bases.\n+\t */\n+\tstruct ewah_bitmap *commits;\n+\tstruct ewah_bitmap *trees;\n+\tstruct ewah_bitmap *blobs;\n+\tstruct ewah_bitmap *tags;\n+\n+\t/* Map from SHA1 -> `stored_bitmap` for all the bitmapped comits */\n+\tkhash_sha1 *bitmaps;\n+\n+\t/* Number of bitmapped commits */\n+\tuint32_t entry_count;\n+\n+\t/*\n+\t * Extended index.\n+\t *\n+\t * When trying to perform bitmap operations with objects that are not\n+\t * packed in `pack`, these objects are added to this \"fake index\" and\n+\t * are assumed to appear at the end of the packfile for all operations\n+\t */\n+\tstruct eindex {\n+\t\tstruct object **objects;\n+\t\tuint32_t *hashes;\n+\t\tuint32_t count, alloc;\n+\t\tkhash_sha1_pos *positions;\n+\t} ext_index;\n+\n+\t/* Bitmap result of the last performed walk */\n+\tstruct bitmap *result;\n+\n+\t/* Version of the bitmap index */\n+\tunsigned int version;\n+\n+\tunsigned loaded : 1;\n+\n+} bitmap_git;\n+\n+static struct ewah_bitmap *lookup_stored_bitmap(struct stored_bitmap *st)\n+{\n+\tstruct ewah_bitmap *parent;\n+\tstruct ewah_bitmap *composed;\n+\n+\tif (st->xor == NULL)\n+\t\treturn st->root;\n+\n+\tcomposed = ewah_pool_new();\n+\tparent = lookup_stored_bitmap(st->xor);\n+\tewah_xor(st->root, parent, composed);\n+\n+\tewah_pool_free(st->root);\n+\tst->root = composed;\n+\tst->xor = NULL;\n+\n+\treturn composed;\n+}\n+\n+/*\n+ * Read a bitmap from the current read position on the mmaped\n+ * index, and increase the read position accordingly\n+ */\n+static struct ewah_bitmap *read_bitmap_1(struct bitmap_index *index)\n+{\n+\tstruct ewah_bitmap *b = ewah_pool_new();\n+\n+\tint bitmap_size = ewah_read_mmap(b,\n+\t\tindex->map + index->map_pos,\n+\t\tindex->map_size - index->map_pos);\n+\n+\tif (bitmap_size < 0) {\n+\t\terror(\"Failed to load bitmap index (corrupted?)\");\n+\t\tewah_pool_free(b);\n+\t\treturn NULL;\n+\t}\n+\n+\tindex->map_pos += bitmap_size;\n+\treturn b;\n+}\n+\n+static int load_bitmap_header(struct bitmap_index *index)\n+{\n+\tstruct bitmap_disk_header *header = (void *)index->map;\n+\n+\tif (index->map_size < sizeof(*header) + 20)\n+\t\treturn error(\"Corrupted bitmap index (missing header data)\");\n+\n+\tif (memcmp(header->magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)) != 0)\n+\t\treturn error(\"Corrupted bitmap index file (wrong header)\");\n+\n+\tindex->version = ntohs(header->version);\n+\tif (index->version != 1)\n+\t\treturn error(\"Unsupported version for bitmap index file (%d)\", index->version);\n+\n+\t/* Parse known bitmap format options */\n+\t{\n+\t\tuint32_t flags = ntohs(header->options);\n+\n+\t\tif ((flags & BITMAP_OPT_FULL_DAG) == 0)\n+\t\t\treturn error(\"Unsupported options for bitmap index file \"\n+\t\t\t\t\"(Git requires BITMAP_OPT_FULL_DAG)\");\n+\t}\n+\n+\tindex->entry_count = ntohl(header->entry_count);\n+\tindex->map_pos += sizeof(*header);\n+\treturn 0;\n+}\n+\n+static struct stored_bitmap *store_bitmap(struct bitmap_index *index,\n+\t\t\t\t\t  struct ewah_bitmap *root,\n+\t\t\t\t\t  const unsigned char *sha1,\n+\t\t\t\t\t  struct stored_bitmap *xor_with,\n+\t\t\t\t\t  int flags)\n+{\n+\tstruct stored_bitmap *stored;\n+\tkhiter_t hash_pos;\n+\tint ret;\n+\n+\tstored = xmalloc(sizeof(struct stored_bitmap));\n+\tstored->root = root;\n+\tstored->xor = xor_with;\n+\tstored->flags = flags;\n+\thashcpy(stored->sha1, sha1);\n+\n+\thash_pos = kh_put_sha1(index->bitmaps, stored->sha1, &ret);\n+\n+\t/* a 0 return code means the insertion succeeded with no changes,\n+\t * because the SHA1 already existed on the map. this is bad, there\n+\t * shouldn't be duplicated commits in the index */\n+\tif (ret == 0) {\n+\t\terror(\"Duplicate entry in bitmap index: %s\", sha1_to_hex(sha1));\n+\t\treturn NULL;\n+\t}\n+\n+\tkh_value(index->bitmaps, hash_pos) = stored;\n+\treturn stored;\n+}\n+\n+static int load_bitmap_entries_v1(struct bitmap_index *index)\n+{\n+\tstatic const size_t MAX_XOR_OFFSET = 160;\n+\n+\tuint32_t i;\n+\tstruct stored_bitmap **recent_bitmaps;\n+\tstruct bitmap_disk_entry *entry;\n+\n+\trecent_bitmaps = xcalloc(MAX_XOR_OFFSET, sizeof(struct stored_bitmap));\n+\n+\tfor (i = 0; i < index->entry_count; ++i) {\n+\t\tint xor_offset, flags;\n+\t\tstruct ewah_bitmap *bitmap = NULL;\n+\t\tstruct stored_bitmap *xor_bitmap = NULL;\n+\t\tuint32_t commit_idx_pos;\n+\t\tconst unsigned char *sha1;\n+\n+\t\tentry = (struct bitmap_disk_entry *)(index->map + index->map_pos);\n+\t\tindex->map_pos += sizeof(struct bitmap_disk_entry);\n+\n+\t\tcommit_idx_pos = ntohl(entry->object_pos);\n+\t\tsha1 = nth_packed_object_sha1(index->pack, commit_idx_pos);\n+\n+\t\txor_offset = (int)entry->xor_offset;\n+\t\tflags = (int)entry->flags;\n+\n+\t\tbitmap = read_bitmap_1(index);\n+\t\tif (!bitmap)\n+\t\t\treturn -1;\n+\n+\t\tif (xor_offset > MAX_XOR_OFFSET || xor_offset > i)\n+\t\t\treturn error(\"Corrupted bitmap pack index\");\n+\n+\t\tif (xor_offset > 0) {\n+\t\t\txor_bitmap = recent_bitmaps[(i - xor_offset) % MAX_XOR_OFFSET];\n+\n+\t\t\tif (xor_bitmap == NULL)\n+\t\t\t\treturn error(\"Invalid XOR offset in bitmap pack index\");\n+\t\t}\n+\n+\t\trecent_bitmaps[i % MAX_XOR_OFFSET] = store_bitmap(\n+\t\t\tindex, bitmap, sha1, xor_bitmap, flags);\n+\t}\n+\n+\treturn 0;\n+}\n+\n+static int open_pack_bitmap_1(struct packed_git *packfile)\n+{\n+\tint fd;\n+\tstruct stat st;\n+\tchar *idx_name;\n+\n+\tif (open_pack_index(packfile))\n+\t\treturn -1;\n+\n+\tidx_name = pack_bitmap_filename(packfile);\n+\tfd = git_open_noatime(idx_name);\n+\tfree(idx_name);\n+\n+\tif (fd < 0)\n+\t\treturn -1;\n+\n+\tif (fstat(fd, &st)) {\n+\t\tclose(fd);\n+\t\treturn -1;\n+\t}\n+\n+\tif (bitmap_git.pack) {\n+\t\twarning(\"ignoring extra bitmap file: %s\", packfile->pack_name);\n+\t\tclose(fd);\n+\t\treturn -1;\n+\t}\n+\n+\tbitmap_git.pack = packfile;\n+\tbitmap_git.map_size = xsize_t(st.st_size);\n+\tbitmap_git.map = xmmap(NULL, bitmap_git.map_size, PROT_READ, MAP_PRIVATE, fd, 0);\n+\tbitmap_git.map_pos = 0;\n+\tclose(fd);\n+\n+\tif (load_bitmap_header(&bitmap_git) < 0) {\n+\t\tmunmap(bitmap_git.map, bitmap_git.map_size);\n+\t\tbitmap_git.map = NULL;\n+\t\tbitmap_git.map_size = 0;\n+\t\treturn -1;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+static int load_pack_bitmap(void)\n+{\n+\tassert(bitmap_git.map && !bitmap_git.loaded);\n+\n+\tbitmap_git.bitmaps = kh_init_sha1();\n+\tbitmap_git.ext_index.positions = kh_init_sha1_pos();\n+\tbitmap_git.reverse_index = revindex_for_pack(bitmap_git.pack);\n+\n+\tif (!(bitmap_git.commits = read_bitmap_1(&bitmap_git)) ||\n+\t\t!(bitmap_git.trees = read_bitmap_1(&bitmap_git)) ||\n+\t\t!(bitmap_git.blobs = read_bitmap_1(&bitmap_git)) ||\n+\t\t!(bitmap_git.tags = read_bitmap_1(&bitmap_git)))\n+\t\tgoto failed;\n+\n+\tif (load_bitmap_entries_v1(&bitmap_git) < 0)\n+\t\tgoto failed;\n+\n+\tbitmap_git.loaded = 1;\n+\treturn 0;\n+\n+failed:\n+\tmunmap(bitmap_git.map, bitmap_git.map_size);\n+\tbitmap_git.map = NULL;\n+\tbitmap_git.map_size = 0;\n+\treturn -1;\n+}\n+\n+char *pack_bitmap_filename(struct packed_git *p)\n+{\n+\tchar *idx_name;\n+\tint len;\n+\n+\tlen = strlen(p->pack_name) - strlen(\".pack\");\n+\tidx_name = xmalloc(len + strlen(\".bitmap\") + 1);\n+\n+\tmemcpy(idx_name, p->pack_name, len);\n+\tmemcpy(idx_name + len, \".bitmap\", strlen(\".bitmap\") + 1);\n+\n+\treturn idx_name;\n+}\n+\n+static int open_pack_bitmap(void)\n+{\n+\tstruct packed_git *p;\n+\tint ret = -1;\n+\n+\tassert(!bitmap_git.map && !bitmap_git.loaded);\n+\n+\tprepare_packed_git();\n+\tfor (p = packed_git; p; p = p->next) {\n+\t\tif (open_pack_bitmap_1(p) == 0)\n+\t\t\tret = 0;\n+\t}\n+\n+\treturn ret;\n+}\n+\n+int prepare_bitmap_git(void)\n+{\n+\tif (bitmap_git.loaded)\n+\t\treturn 0;\n+\n+\tif (!open_pack_bitmap())\n+\t\treturn load_pack_bitmap();\n+\n+\treturn -1;\n+}\n+\n+struct include_data {\n+\tstruct bitmap *base;\n+\tstruct bitmap *seen;\n+};\n+\n+static inline int bitmap_position_extended(const unsigned char *sha1)\n+{\n+\tkhash_sha1_pos *positions = bitmap_git.ext_index.positions;\n+\tkhiter_t pos = kh_get_sha1_pos(positions, sha1);\n+\n+\tif (pos < kh_end(positions)) {\n+\t\tint bitmap_pos = kh_value(positions, pos);\n+\t\treturn bitmap_pos + bitmap_git.pack->num_objects;\n+\t}\n+\n+\treturn -1;\n+}\n+\n+static inline int bitmap_position_packfile(const unsigned char *sha1)\n+{\n+\toff_t offset = find_pack_entry_one(sha1, bitmap_git.pack);\n+\tif (!offset)\n+\t\treturn -1;\n+\n+\treturn find_revindex_position(bitmap_git.reverse_index, offset);\n+}\n+\n+static int bitmap_position(const unsigned char *sha1)\n+{\n+\tint pos = bitmap_position_packfile(sha1);\n+\treturn (pos >= 0) ? pos : bitmap_position_extended(sha1);\n+}\n+\n+static int ext_index_add_object(struct object *object, const char *name)\n+{\n+\tstruct eindex *eindex = &bitmap_git.ext_index;\n+\n+\tkhiter_t hash_pos;\n+\tint hash_ret;\n+\tint bitmap_pos;\n+\n+\thash_pos = kh_put_sha1_pos(eindex->positions, object->sha1, &hash_ret);\n+\tif (hash_ret > 0) {\n+\t\tif (eindex->count >= eindex->alloc) {\n+\t\t\teindex->alloc = (eindex->alloc + 16) * 3 / 2;\n+\t\t\teindex->objects = xrealloc(eindex->objects,\n+\t\t\t\teindex->alloc * sizeof(struct object *));\n+\t\t\teindex->hashes = xrealloc(eindex->hashes,\n+\t\t\t\teindex->alloc * sizeof(uint32_t));\n+\t\t}\n+\n+\t\tbitmap_pos = eindex->count;\n+\t\teindex->objects[eindex->count] = object;\n+\t\teindex->hashes[eindex->count] = pack_name_hash(name);\n+\t\tkh_value(eindex->positions, hash_pos) = bitmap_pos;\n+\t\teindex->count++;\n+\t} else {\n+\t\tbitmap_pos = kh_value(eindex->positions, hash_pos);\n+\t}\n+\n+\treturn bitmap_pos + bitmap_git.pack->num_objects;\n+}\n+\n+static void show_object(struct object *object, const struct name_path *path,\n+\t\t\tconst char *last, void *data)\n+{\n+\tstruct bitmap *base = data;\n+\tint bitmap_pos;\n+\n+\tbitmap_pos = bitmap_position(object->sha1);\n+\n+\tif (bitmap_pos < 0) {\n+\t\tchar *name = path_name(path, last);\n+\t\tbitmap_pos = ext_index_add_object(object, name);\n+\t\tfree(name);\n+\t}\n+\n+\tbitmap_set(base, bitmap_pos);\n+}\n+\n+static void show_commit(struct commit *commit, void *data)\n+{\n+}\n+\n+static int add_to_include_set(struct include_data *data,\n+\t\t\t      const unsigned char *sha1,\n+\t\t\t      int bitmap_pos)\n+{\n+\tkhiter_t hash_pos;\n+\n+\tif (data->seen && bitmap_get(data->seen, bitmap_pos))\n+\t\treturn 0;\n+\n+\tif (bitmap_get(data->base, bitmap_pos))\n+\t\treturn 0;\n+\n+\thash_pos = kh_get_sha1(bitmap_git.bitmaps, sha1);\n+\tif (hash_pos < kh_end(bitmap_git.bitmaps)) {\n+\t\tstruct stored_bitmap *st = kh_value(bitmap_git.bitmaps, hash_pos);\n+\t\tbitmap_or_ewah(data->base, lookup_stored_bitmap(st));\n+\t\treturn 0;\n+\t}\n+\n+\tbitmap_set(data->base, bitmap_pos);\n+\treturn 1;\n+}\n+\n+static int should_include(struct commit *commit, void *_data)\n+{\n+\tstruct include_data *data = _data;\n+\tint bitmap_pos;\n+\n+\tbitmap_pos = bitmap_position(commit->object.sha1);\n+\tif (bitmap_pos < 0)\n+\t\tbitmap_pos = ext_index_add_object((struct object *)commit, NULL);\n+\n+\tif (!add_to_include_set(data, commit->object.sha1, bitmap_pos)) {\n+\t\tstruct commit_list *parent = commit->parents;\n+\n+\t\twhile (parent) {\n+\t\t\tparent->item->object.flags |= SEEN;\n+\t\t\tparent = parent->next;\n+\t\t}\n+\n+\t\treturn 0;\n+\t}\n+\n+\treturn 1;\n+}\n+\n+static struct bitmap *find_objects(struct rev_info *revs,\n+\t\t\t\t   struct object_list *roots,\n+\t\t\t\t   struct bitmap *seen)\n+{\n+\tstruct bitmap *base = NULL;\n+\tint needs_walk = 0;\n+\n+\tstruct object_list *not_mapped = NULL;\n+\n+\t/*\n+\t * Go through all the roots for the walk. The ones that have bitmaps\n+\t * on the bitmap index will be `or`ed together to form an initial\n+\t * global reachability analysis.\n+\t *\n+\t * The ones without bitmaps in the index will be stored in the\n+\t * `not_mapped_list` for further processing.\n+\t */\n+\twhile (roots) {\n+\t\tstruct object *object = roots->item;\n+\t\troots = roots->next;\n+\n+\t\tif (object->type == OBJ_COMMIT) {\n+\t\t\tkhiter_t pos = kh_get_sha1(bitmap_git.bitmaps, object->sha1);\n+\n+\t\t\tif (pos < kh_end(bitmap_git.bitmaps)) {\n+\t\t\t\tstruct stored_bitmap *st = kh_value(bitmap_git.bitmaps, pos);\n+\t\t\t\tstruct ewah_bitmap *or_with = lookup_stored_bitmap(st);\n+\n+\t\t\t\tif (base == NULL)\n+\t\t\t\t\tbase = ewah_to_bitmap(or_with);\n+\t\t\t\telse\n+\t\t\t\t\tbitmap_or_ewah(base, or_with);\n+\n+\t\t\t\tobject->flags |= SEEN;\n+\t\t\t\tcontinue;\n+\t\t\t}\n+\t\t}\n+\n+\t\tobject_list_insert(object, &not_mapped);\n+\t}\n+\n+\t/*\n+\t * Best case scenario: We found bitmaps for all the roots,\n+\t * so the resulting `or` bitmap has the full reachability analysis\n+\t */\n+\tif (not_mapped == NULL)\n+\t\treturn base;\n+\n+\troots = not_mapped;\n+\n+\t/*\n+\t * Let's iterate through all the roots that don't have bitmaps to\n+\t * check if we can determine them to be reachable from the existing\n+\t * global bitmap.\n+\t *\n+\t * If we cannot find them in the existing global bitmap, we'll need\n+\t * to push them to an actual walk and run it until we can confirm\n+\t * they are reachable\n+\t */\n+\twhile (roots) {\n+\t\tstruct object *object = roots->item;\n+\t\tint pos;\n+\n+\t\troots = roots->next;\n+\t\tpos = bitmap_position(object->sha1);\n+\n+\t\tif (pos < 0 || base == NULL || !bitmap_get(base, pos)) {\n+\t\t\tobject->flags &= ~UNINTERESTING;\n+\t\t\tadd_pending_object(revs, object, \"\");\n+\t\t\tneeds_walk = 1;\n+\t\t} else {\n+\t\t\tobject->flags |= SEEN;\n+\t\t}\n+\t}\n+\n+\tif (needs_walk) {\n+\t\tstruct include_data incdata;\n+\n+\t\tif (base == NULL)\n+\t\t\tbase = bitmap_new();\n+\n+\t\tincdata.base = base;\n+\t\tincdata.seen = seen;\n+\n+\t\trevs->include_check = should_include;\n+\t\trevs->include_check_data = &incdata;\n+\n+\t\tif (prepare_revision_walk(revs))\n+\t\t\tdie(\"revision walk setup failed\");\n+\n+\t\ttraverse_commit_list(revs, show_commit, show_object, base);\n+\t}\n+\n+\treturn base;\n+}\n+\n+static void show_extended_objects(struct bitmap *objects,\n+\t\t\t\t  show_reachable_fn show_reach)\n+{\n+\tstruct eindex *eindex = &bitmap_git.ext_index;\n+\tuint32_t i;\n+\n+\tfor (i = 0; i < eindex->count; ++i) {\n+\t\tstruct object *obj;\n+\n+\t\tif (!bitmap_get(objects, bitmap_git.pack->num_objects + i))\n+\t\t\tcontinue;\n+\n+\t\tobj = eindex->objects[i];\n+\t\tshow_reach(obj->sha1, obj->type, 0, eindex->hashes[i], NULL, 0);\n+\t}\n+}\n+\n+static void show_objects_for_type(\n+\tstruct bitmap *objects,\n+\tstruct ewah_bitmap *type_filter,\n+\tenum object_type object_type,\n+\tshow_reachable_fn show_reach)\n+{\n+\tsize_t pos = 0, i = 0;\n+\tuint32_t offset;\n+\n+\tstruct ewah_iterator it;\n+\teword_t filter;\n+\n+\tif (bitmap_git.reuse_objects == bitmap_git.pack->num_objects)\n+\t\treturn;\n+\n+\tewah_iterator_init(&it, type_filter);\n+\n+\twhile (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {\n+\t\teword_t word = objects->words[i] & filter;\n+\n+\t\tfor (offset = 0; offset < BITS_IN_WORD; ++offset) {\n+\t\t\tconst unsigned char *sha1;\n+\t\t\tstruct revindex_entry *entry;\n+\t\t\tuint32_t hash = 0;\n+\n+\t\t\tif ((word >> offset) == 0)\n+\t\t\t\tbreak;\n+\n+\t\t\toffset += ewah_bit_ctz64(word >> offset);\n+\n+\t\t\tif (pos + offset < bitmap_git.reuse_objects)\n+\t\t\t\tcontinue;\n+\n+\t\t\tentry = &bitmap_git.reverse_index->revindex[pos + offset];\n+\t\t\tsha1 = nth_packed_object_sha1(bitmap_git.pack, entry->nr);\n+\n+\t\t\tshow_reach(sha1, object_type, 0, hash, bitmap_git.pack, entry->offset);\n+\t\t}\n+\n+\t\tpos += BITS_IN_WORD;\n+\t\ti++;\n+\t}\n+}\n+\n+static int in_bitmapped_pack(struct object_list *roots)\n+{\n+\twhile (roots) {\n+\t\tstruct object *object = roots->item;\n+\t\troots = roots->next;\n+\n+\t\tif (find_pack_entry_one(object->sha1, bitmap_git.pack) > 0)\n+\t\t\treturn 1;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+int prepare_bitmap_walk(struct rev_info *revs)\n+{\n+\tunsigned int i;\n+\tunsigned int pending_nr = revs->pending.nr;\n+\tstruct object_array_entry *pending_e = revs->pending.objects;\n+\n+\tstruct object_list *wants = NULL;\n+\tstruct object_list *haves = NULL;\n+\n+\tstruct bitmap *wants_bitmap = NULL;\n+\tstruct bitmap *haves_bitmap = NULL;\n+\n+\tif (!bitmap_git.loaded) {\n+\t\t/* try to open a bitmapped pack, but don't parse it yet\n+\t\t * because we may not need to use it */\n+\t\tif (open_pack_bitmap() < 0)\n+\t\t\treturn -1;\n+\t}\n+\n+\tfor (i = 0; i < pending_nr; ++i) {\n+\t\tstruct object *object = pending_e[i].item;\n+\n+\t\tif (object->type == OBJ_NONE)\n+\t\t\tparse_object_or_die(object->sha1, NULL);\n+\n+\t\twhile (object->type == OBJ_TAG) {\n+\t\t\tstruct tag *tag = (struct tag *) object;\n+\n+\t\t\tif (object->flags & UNINTERESTING)\n+\t\t\t\tobject_list_insert(object, &haves);\n+\t\t\telse\n+\t\t\t\tobject_list_insert(object, &wants);\n+\n+\t\t\tif (!tag->tagged)\n+\t\t\t\tdie(\"bad tag\");\n+\t\t\tobject = parse_object_or_die(tag->tagged->sha1, NULL);\n+\t\t}\n+\n+\t\tif (object->flags & UNINTERESTING)\n+\t\t\tobject_list_insert(object, &haves);\n+\t\telse\n+\t\t\tobject_list_insert(object, &wants);\n+\t}\n+\n+\t/*\n+\t * if we have a HAVES list, but none of those haves is contained\n+\t * in the packfile that has a bitmap, we don't have anything to\n+\t * optimize here\n+\t */\n+\tif (haves && !in_bitmapped_pack(haves))\n+\t\treturn -1;\n+\n+\t/* if we don't want anything, we're done here */\n+\tif (!wants)\n+\t\treturn -1;\n+\n+\t/*\n+\t * now we're going to use bitmaps, so load the actual bitmap entries\n+\t * from disk. this is the point of no return; after this the rev_list\n+\t * becomes invalidated and we must perform the revwalk through bitmaps\n+\t */\n+\tif (!bitmap_git.loaded && load_pack_bitmap() < 0)\n+\t\treturn -1;\n+\n+\trevs->pending.nr = 0;\n+\trevs->pending.alloc = 0;\n+\trevs->pending.objects = NULL;\n+\n+\tif (haves) {\n+\t\thaves_bitmap = find_objects(revs, haves, NULL);\n+\t\treset_revision_walk();\n+\n+\t\tif (haves_bitmap == NULL)\n+\t\t\tdie(\"BUG: failed to perform bitmap walk\");\n+\t}\n+\n+\twants_bitmap = find_objects(revs, wants, haves_bitmap);\n+\n+\tif (!wants_bitmap)\n+\t\tdie(\"BUG: failed to perform bitmap walk\");\n+\n+\tif (haves_bitmap)\n+\t\tbitmap_and_not(wants_bitmap, haves_bitmap);\n+\n+\tbitmap_git.result = wants_bitmap;\n+\n+\tbitmap_free(haves_bitmap);\n+\treturn 0;\n+}\n+\n+int reuse_partial_packfile_from_bitmap(struct packed_git **packfile,\n+\t\t\t\t       uint32_t *entries,\n+\t\t\t\t       off_t *up_to)\n+{\n+\t/*\n+\t * Reuse the packfile content if we need more than\n+\t * 90% of its objects\n+\t */\n+\tstatic const double REUSE_PERCENT = 0.9;\n+\n+\tstruct bitmap *result = bitmap_git.result;\n+\tuint32_t reuse_threshold;\n+\tuint32_t i, reuse_objects = 0;\n+\n+\tassert(result);\n+\n+\tfor (i = 0; i < result->word_alloc; ++i) {\n+\t\tif (result->words[i] != (eword_t)~0) {\n+\t\t\treuse_objects += ewah_bit_ctz64(~result->words[i]);\n+\t\t\tbreak;\n+\t\t}\n+\n+\t\treuse_objects += BITS_IN_WORD;\n+\t}\n+\n+#ifdef GIT_BITMAP_DEBUG\n+\t{\n+\t\tconst unsigned char *sha1;\n+\t\tstruct revindex_entry *entry;\n+\n+\t\tentry = &bitmap_git.reverse_index->revindex[reuse_objects];\n+\t\tsha1 = nth_packed_object_sha1(bitmap_git.pack, entry->nr);\n+\n+\t\tfprintf(stderr, \"Failed to reuse at %d (%016llx)\\n\",\n+\t\t\treuse_objects, result->words[i]);\n+\t\tfprintf(stderr, \" %s\\n\", sha1_to_hex(sha1));\n+\t}\n+#endif\n+\n+\tif (!reuse_objects)\n+\t\treturn -1;\n+\n+\tif (reuse_objects >= bitmap_git.pack->num_objects) {\n+\t\tbitmap_git.reuse_objects = *entries = bitmap_git.pack->num_objects;\n+\t\t*up_to = -1; /* reuse the full pack */\n+\t\t*packfile = bitmap_git.pack;\n+\t\treturn 0;\n+\t}\n+\n+\treuse_threshold = bitmap_popcount(bitmap_git.result) * REUSE_PERCENT;\n+\n+\tif (reuse_objects < reuse_threshold)\n+\t\treturn -1;\n+\n+\tbitmap_git.reuse_objects = *entries = reuse_objects;\n+\t*up_to = bitmap_git.reverse_index->revindex[reuse_objects].offset;\n+\t*packfile = bitmap_git.pack;\n+\n+\treturn 0;\n+}\n+\n+void traverse_bitmap_commit_list(show_reachable_fn show_reachable)\n+{\n+\tassert(bitmap_git.result);\n+\n+\tshow_objects_for_type(bitmap_git.result, bitmap_git.commits,\n+\t\tOBJ_COMMIT, show_reachable);\n+\tshow_objects_for_type(bitmap_git.result, bitmap_git.trees,\n+\t\tOBJ_TREE, show_reachable);\n+\tshow_objects_for_type(bitmap_git.result, bitmap_git.blobs,\n+\t\tOBJ_BLOB, show_reachable);\n+\tshow_objects_for_type(bitmap_git.result, bitmap_git.tags,\n+\t\tOBJ_TAG, show_reachable);\n+\n+\tshow_extended_objects(bitmap_git.result, show_reachable);\n+\n+\tbitmap_free(bitmap_git.result);\n+\tbitmap_git.result = NULL;\n+}\n+\n+static uint32_t count_object_type(struct bitmap *objects,\n+\t\t\t\t  enum object_type type)\n+{\n+\tstruct eindex *eindex = &bitmap_git.ext_index;\n+\n+\tuint32_t i = 0, count = 0;\n+\tstruct ewah_iterator it;\n+\teword_t filter;\n+\n+\tswitch (type) {\n+\tcase OBJ_COMMIT:\n+\t\tewah_iterator_init(&it, bitmap_git.commits);\n+\t\tbreak;\n+\n+\tcase OBJ_TREE:\n+\t\tewah_iterator_init(&it, bitmap_git.trees);\n+\t\tbreak;\n+\n+\tcase OBJ_BLOB:\n+\t\tewah_iterator_init(&it, bitmap_git.blobs);\n+\t\tbreak;\n+\n+\tcase OBJ_TAG:\n+\t\tewah_iterator_init(&it, bitmap_git.tags);\n+\t\tbreak;\n+\n+\tdefault:\n+\t\treturn 0;\n+\t}\n+\n+\twhile (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {\n+\t\teword_t word = objects->words[i++] & filter;\n+\t\tcount += ewah_bit_popcount64(word);\n+\t}\n+\n+\tfor (i = 0; i < eindex->count; ++i) {\n+\t\tif (eindex->objects[i]->type == type &&\n+\t\t\tbitmap_get(objects, bitmap_git.pack->num_objects + i))\n+\t\t\tcount++;\n+\t}\n+\n+\treturn count;\n+}\n+\n+void count_bitmap_commit_list(uint32_t *commits, uint32_t *trees,\n+\t\t\t      uint32_t *blobs, uint32_t *tags)\n+{\n+\tassert(bitmap_git.result);\n+\n+\tif (commits)\n+\t\t*commits = count_object_type(bitmap_git.result, OBJ_COMMIT);\n+\n+\tif (trees)\n+\t\t*trees = count_object_type(bitmap_git.result, OBJ_TREE);\n+\n+\tif (blobs)\n+\t\t*blobs = count_object_type(bitmap_git.result, OBJ_BLOB);\n+\n+\tif (tags)\n+\t\t*tags = count_object_type(bitmap_git.result, OBJ_TAG);\n+}\n+\n+struct bitmap_test_data {\n+\tstruct bitmap *base;\n+\tstruct progress *prg;\n+\tsize_t seen;\n+};\n+\n+static void test_show_object(struct object *object,\n+\t\t\t     const struct name_path *path,\n+\t\t\t     const char *last, void *data)\n+{\n+\tstruct bitmap_test_data *tdata = data;\n+\tint bitmap_pos;\n+\n+\tbitmap_pos = bitmap_position(object->sha1);\n+\tif (bitmap_pos < 0)\n+\t\tdie(\"Object not in bitmap: %s\\n\", sha1_to_hex(object->sha1));\n+\n+\tbitmap_set(tdata->base, bitmap_pos);\n+\tdisplay_progress(tdata->prg, ++tdata->seen);\n+}\n+\n+static void test_show_commit(struct commit *commit, void *data)\n+{\n+\tstruct bitmap_test_data *tdata = data;\n+\tint bitmap_pos;\n+\n+\tbitmap_pos = bitmap_position(commit->object.sha1);\n+\tif (bitmap_pos < 0)\n+\t\tdie(\"Object not in bitmap: %s\\n\", sha1_to_hex(commit->object.sha1));\n+\n+\tbitmap_set(tdata->base, bitmap_pos);\n+\tdisplay_progress(tdata->prg, ++tdata->seen);\n+}\n+\n+void test_bitmap_walk(struct rev_info *revs)\n+{\n+\tstruct object *root;\n+\tstruct bitmap *result = NULL;\n+\tkhiter_t pos;\n+\tsize_t result_popcnt;\n+\tstruct bitmap_test_data tdata;\n+\n+\tif (prepare_bitmap_git())\n+\t\tdie(\"failed to load bitmap indexes\");\n+\n+\tif (revs->pending.nr != 1)\n+\t\tdie(\"you must specify exactly one commit to test\");\n+\n+\tfprintf(stderr, \"Bitmap v%d test (%d entries loaded)\\n\",\n+\t\tbitmap_git.version, bitmap_git.entry_count);\n+\n+\troot = revs->pending.objects[0].item;\n+\tpos = kh_get_sha1(bitmap_git.bitmaps, root->sha1);\n+\n+\tif (pos < kh_end(bitmap_git.bitmaps)) {\n+\t\tstruct stored_bitmap *st = kh_value(bitmap_git.bitmaps, pos);\n+\t\tstruct ewah_bitmap *bm = lookup_stored_bitmap(st);\n+\n+\t\tfprintf(stderr, \"Found bitmap for %s. %d bits / %08x checksum\\n\",\n+\t\t\tsha1_to_hex(root->sha1), (int)bm->bit_size, ewah_checksum(bm));\n+\n+\t\tresult = ewah_to_bitmap(bm);\n+\t}\n+\n+\tif (result == NULL)\n+\t\tdie(\"Commit %s doesn't have an indexed bitmap\", sha1_to_hex(root->sha1));\n+\n+\trevs->tag_objects = 1;\n+\trevs->tree_objects = 1;\n+\trevs->blob_objects = 1;\n+\n+\tresult_popcnt = bitmap_popcount(result);\n+\n+\tif (prepare_revision_walk(revs))\n+\t\tdie(\"revision walk setup failed\");\n+\n+\ttdata.base = bitmap_new();\n+\ttdata.prg = start_progress(\"Verifying bitmap entries\", result_popcnt);\n+\ttdata.seen = 0;\n+\n+\ttraverse_commit_list(revs, &test_show_commit, &test_show_object, &tdata);\n+\n+\tstop_progress(&tdata.prg);\n+\n+\tif (bitmap_equals(result, tdata.base))\n+\t\tfprintf(stderr, \"OK!\\n\");\n+\telse\n+\t\tfprintf(stderr, \"Mismatch!\\n\");\n+}\ndiff --git a/pack-bitmap.h b/pack-bitmap.h\nnew file mode 100644\nindex 0000000..b4510d5\n--- /dev/null\n+++ b/pack-bitmap.h\n@@ -0,0 +1,43 @@\n+#ifndef PACK_BITMAP_H\n+#define PACK_BITMAP_H\n+\n+#include \"ewah/ewok.h\"\n+#include \"khash.h\"\n+\n+struct bitmap_disk_entry {\n+\tuint32_t object_pos;\n+\tuint8_t xor_offset;\n+\tuint8_t flags;\n+} __attribute__((packed));\n+\n+struct bitmap_disk_header {\n+\tchar magic[4];\n+\tuint16_t version;\n+\tuint16_t options;\n+\tuint32_t entry_count;\n+\tunsigned char checksum[20];\n+};\n+\n+static const char BITMAP_IDX_SIGNATURE[] = {'B', 'I', 'T', 'M'};\n+\n+enum pack_bitmap_opts {\n+\tBITMAP_OPT_FULL_DAG = 1\n+};\n+\n+typedef int (*show_reachable_fn)(\n+\tconst unsigned char *sha1,\n+\tenum object_type type,\n+\tint flags,\n+\tuint32_t hash,\n+\tstruct packed_git *found_pack,\n+\toff_t found_offset);\n+\n+int prepare_bitmap_git(void);\n+void count_bitmap_commit_list(uint32_t *commits, uint32_t *trees, uint32_t *blobs, uint32_t *tags);\n+void traverse_bitmap_commit_list(show_reachable_fn show_reachable);\n+void test_bitmap_walk(struct rev_info *revs);\n+char *pack_bitmap_filename(struct packed_git *p);\n+int prepare_bitmap_walk(struct rev_info *revs);\n+int reuse_partial_packfile_from_bitmap(struct packed_git **packfile, uint32_t *entries, off_t *up_to);\n+\n+#endif\n-- \n1.8.5.1.399.g900e7cd\n"},{"id":"232315","messageId":"20131221140005.GK21145@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20131221135651.GA20818@sigill.intra.peff.net","subject":"[PATCH v4 11/23] pack-objects: split add_object_entry","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-12-21T14:00:06Z","receivedAt":"2013-12-21T14:00:06Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"This function actually does three things:\n\n  1. Check whether we've already added the object to our\n     packing list.\n\n  2. Check whether the object meets our criteria for adding.\n\n  3. Actually add the object to our packing list.\n\nIt's a little hard to see these three phases, because they\nhappen linearly in the rather long function. Instead, this\npatch breaks them up into three separate helper functions.\n\nThe result is a little easier to follow, though it\nunfortunately suffers from some optimization\ninterdependencies between the stages (e.g., during step 3 we\nuse the packing list index from step 1 and the packfile\ninformation from step 2).\n\nMore importantly, though, the various parts can be\ncomposed differently, as they will be in the next patch.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\n builtin/pack-objects.c | 98 +++++++++++++++++++++++++++++++++++++++-----------\n 1 file changed, 78 insertions(+), 20 deletions(-)\n\ndiff --git a/builtin/pack-objects.c b/builtin/pack-objects.c\nindex faf746b..13b171d 100644\n--- a/builtin/pack-objects.c\n+++ b/builtin/pack-objects.c\n@@ -800,41 +800,69 @@ static int no_try_delta(const char *path)\n \treturn 0;\n }\n \n-static int add_object_entry(const unsigned char *sha1, enum object_type type,\n-\t\t\t    const char *name, int exclude)\n+/*\n+ * When adding an object, check whether we have already added it\n+ * to our packing list. If so, we can skip. However, if we are\n+ * being asked to excludei t, but the previous mention was to include\n+ * it, make sure to adjust its flags and tweak our numbers accordingly.\n+ *\n+ * As an optimization, we pass out the index position where we would have\n+ * found the item, since that saves us from having to look it up again a\n+ * few lines later when we want to add the new entry.\n+ */\n+static int have_duplicate_entry(const unsigned char *sha1,\n+\t\t\t\tint exclude,\n+\t\t\t\tuint32_t *index_pos)\n {\n \tstruct object_entry *entry;\n-\tstruct packed_git *p, *found_pack = NULL;\n-\toff_t found_offset = 0;\n-\tuint32_t hash = pack_name_hash(name);\n-\tuint32_t index_pos;\n \n-\tentry = packlist_find(&to_pack, sha1, &index_pos);\n-\tif (entry) {\n-\t\tif (exclude) {\n-\t\t\tif (!entry->preferred_base)\n-\t\t\t\tnr_result--;\n-\t\t\tentry->preferred_base = 1;\n-\t\t}\n+\tentry = packlist_find(&to_pack, sha1, index_pos);\n+\tif (!entry)\n \t\treturn 0;\n+\n+\tif (exclude) {\n+\t\tif (!entry->preferred_base)\n+\t\t\tnr_result--;\n+\t\tentry->preferred_base = 1;\n \t}\n \n+\treturn 1;\n+}\n+\n+/*\n+ * Check whether we want the object in the pack (e.g., we do not want\n+ * objects found in non-local stores if the \"--local\" option was used).\n+ *\n+ * As a side effect of this check, we will find the packed version of this\n+ * object, if any. We therefore pass out the pack information to avoid having\n+ * to look it up again later.\n+ */\n+static int want_object_in_pack(const unsigned char *sha1,\n+\t\t\t       int exclude,\n+\t\t\t       struct packed_git **found_pack,\n+\t\t\t       off_t *found_offset)\n+{\n+\tstruct packed_git *p;\n+\n \tif (!exclude && local && has_loose_object_nonlocal(sha1))\n \t\treturn 0;\n \n+\t*found_pack = NULL;\n+\t*found_offset = 0;\n+\n \tfor (p = packed_git; p; p = p->next) {\n \t\toff_t offset = find_pack_entry_one(sha1, p);\n \t\tif (offset) {\n-\t\t\tif (!found_pack) {\n+\t\t\tif (!*found_pack) {\n \t\t\t\tif (!is_pack_valid(p)) {\n \t\t\t\t\twarning(\"packfile %s cannot be accessed\", p->pack_name);\n \t\t\t\t\tcontinue;\n \t\t\t\t}\n-\t\t\t\tfound_offset = offset;\n-\t\t\t\tfound_pack = p;\n+\t\t\t\t*found_offset = offset;\n+\t\t\t\t*found_pack = p;\n \t\t\t}\n \t\t\tif (exclude)\n-\t\t\t\tbreak;\n+\t\t\t\treturn 1;\n \t\t\tif (incremental)\n \t\t\t\treturn 0;\n \t\t\tif (local && !p->pack_local)\n@@ -844,6 +872,20 @@ static int add_object_entry(const unsigned char *sha1, enum object_type type,\n \t\t}\n \t}\n \n+\treturn 1;\n+}\n+\n+static void create_object_entry(const unsigned char *sha1,\n+\t\t\t\tenum object_type type,\n+\t\t\t\tuint32_t hash,\n+\t\t\t\tint exclude,\n+\t\t\t\tint no_try_delta,\n+\t\t\t\tuint32_t index_pos,\n+\t\t\t\tstruct packed_git *found_pack,\n+\t\t\t\toff_t found_offset)\n+{\n+\tstruct object_entry *entry;\n+\n \tentry = packlist_alloc(&to_pack, sha1, index_pos);\n \tentry->hash = hash;\n \tif (type)\n@@ -857,11 +899,27 @@ static int add_object_entry(const unsigned char *sha1, enum object_type type,\n \t\tentry->in_pack_offset = found_offset;\n \t}\n \n-\tdisplay_progress(progress_state, to_pack.nr_objects);\n+\tentry->no_try_delta = no_try_delta;\n+}\n+\n+static int add_object_entry(const unsigned char *sha1, enum object_type type,\n+\t\t\t    const char *name, int exclude)\n+{\n+\tstruct packed_git *found_pack;\n+\toff_t found_offset;\n+\tuint32_t index_pos;\n \n-\tif (name && no_try_delta(name))\n-\t\tentry->no_try_delta = 1;\n+\tif (have_duplicate_entry(sha1, exclude, &index_pos))\n+\t\treturn 0;\n \n+\tif (!want_object_in_pack(sha1, exclude, &found_pack, &found_offset))\n+\t\treturn 0;\n+\n+\tcreate_object_entry(sha1, type, pack_name_hash(name),\n+\t\t\t    exclude, name && no_try_delta(name),\n+\t\t\t    index_pos, found_pack, found_offset);\n+\n+\tdisplay_progress(progress_state, to_pack.nr_objects);\n \treturn 1;\n }\n \n-- \n1.8.5.1.399.g900e7cd\n"},{"id":"232317","messageId":"20131221140009.GL21145@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20131221135651.GA20818@sigill.intra.peff.net","subject":"[PATCH v4 12/23] pack-objects: use bitmaps when packing objects","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-12-21T14:00:09Z","receivedAt":"2013-12-21T14:00:09Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"From: Vicent Marti <tanoku@gmail.com>\n\nIn this patch, we use the bitmap API to perform the `Counting Objects`\nphase in pack-objects, rather than a traditional walk through the object\ngraph. For a reasonably-packed large repo, the time to fetch and clone\nis often dominated by the full-object revision walk during the Counting\nObjects phase. Using bitmaps can reduce the CPU time required on the\nserver (and therefore start sending the actual pack data with less\ndelay).\n\nFor bitmaps to be used, the following must be true:\n\n  1. We must be packing to stdout (as a normal `pack-objects` from\n     `upload-pack` would do).\n\n  2. There must be a .bitmap index containing at least one of the\n     \"have\" objects that the client is asking for.\n\n  3. Bitmaps must be enabled (they are enabled by default, but can be\n     disabled by setting `pack.usebitmaps` to false, or by using\n     `--no-use-bitmap-index` on the command-line).\n\nIf any of these is not true, we fall back to doing a normal walk of the\nobject graph.\n\nHere are some sample timings from a full pack of `torvalds/linux` (i.e.\nsomething very similar to what would be generated for a clone of the\nrepository) that show the speedup produced by various\nmethods:\n\n    [existing graph traversal]\n    $ time git pack-objects --all --stdout --no-use-bitmap-index \\\n\t\t\t    </dev/null >/dev/null\n    Counting objects: 3237103, done.\n    Compressing objects: 100% (508752/508752), done.\n    Total 3237103 (delta 2699584), reused 3237103 (delta 2699584)\n\n    real    0m44.111s\n    user    0m42.396s\n    sys     0m3.544s\n\n    [bitmaps only, without partial pack reuse; note that\n     pack reuse is automatic, so timing this required a\n     patch to disable it]\n    $ time git pack-objects --all --stdout </dev/null >/dev/null\n    Counting objects: 3237103, done.\n    Compressing objects: 100% (508752/508752), done.\n    Total 3237103 (delta 2699584), reused 3237103 (delta 2699584)\n\n    real    0m5.413s\n    user    0m5.604s\n    sys     0m1.804s\n\n    [bitmaps with pack reuse (what you get with this patch)]\n    $ time git pack-objects --all --stdout </dev/null >/dev/null\n    Reusing existing pack: 3237103, done.\n    Total 3237103 (delta 0), reused 0 (delta 0)\n\n    real    0m1.636s\n    user    0m1.460s\n    sys     0m0.172s\n\nSigned-off-by: Vicent Marti <tanoku@gmail.com>\nSigned-off-by: Jeff King <peff@peff.net>\n---\n Documentation/config.txt |   6 +++\n builtin/pack-objects.c   | 107 +++++++++++++++++++++++++++++++++++++++++++++++\n 2 files changed, 113 insertions(+)\n\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex ab26963..a981369 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -1858,6 +1858,12 @@ pack.packSizeLimit::\n \tCommon unit suffixes of 'k', 'm', or 'g' are\n \tsupported.\n \n+pack.useBitmaps::\n+\tWhen true, git will use pack bitmaps (if available) when packing\n+\tto stdout (e.g., during the server side of a fetch). Defaults to\n+\ttrue. You should not generally need to turn this off unless\n+\tyou are debugging pack bitmaps.\n+\n pager.<cmd>::\n \tIf the value is boolean, turns on or off pagination of the\n \toutput of a particular Git subcommand when writing to a tty.\ndiff --git a/builtin/pack-objects.c b/builtin/pack-objects.c\nindex 13b171d..030d894 100644\n--- a/builtin/pack-objects.c\n+++ b/builtin/pack-objects.c\n@@ -19,6 +19,7 @@\n #include \"refs.h\"\n #include \"streaming.h\"\n #include \"thread-utils.h\"\n+#include \"pack-bitmap.h\"\n \n static const char *pack_usage[] = {\n \tN_(\"git pack-objects --stdout [options...] [< ref-list | < object-list]\"),\n@@ -57,6 +58,12 @@ static struct progress *progress_state;\n static int pack_compression_level = Z_DEFAULT_COMPRESSION;\n static int pack_compression_seen;\n \n+static struct packed_git *reuse_packfile;\n+static uint32_t reuse_packfile_objects;\n+static off_t reuse_packfile_offset;\n+\n+static int use_bitmap_index = 1;\n+\n static unsigned long delta_cache_size = 0;\n static unsigned long max_delta_cache_size = 256 * 1024 * 1024;\n static unsigned long cache_max_small_delta_size = 1000;\n@@ -678,6 +685,46 @@ static struct object_entry **compute_write_order(void)\n \treturn wo;\n }\n \n+static off_t write_reused_pack(struct sha1file *f)\n+{\n+\tunsigned char buffer[8192];\n+\toff_t to_write;\n+\tint fd;\n+\n+\tif (!is_pack_valid(reuse_packfile))\n+\t\tdie(\"packfile is invalid: %s\", reuse_packfile->pack_name);\n+\n+\tfd = git_open_noatime(reuse_packfile->pack_name);\n+\tif (fd < 0)\n+\t\tdie_errno(\"unable to open packfile for reuse: %s\",\n+\t\t\t  reuse_packfile->pack_name);\n+\n+\tif (lseek(fd, sizeof(struct pack_header), SEEK_SET) == -1)\n+\t\tdie_errno(\"unable to seek in reused packfile\");\n+\n+\tif (reuse_packfile_offset < 0)\n+\t\treuse_packfile_offset = reuse_packfile->pack_size - 20;\n+\n+\tto_write = reuse_packfile_offset - sizeof(struct pack_header);\n+\n+\twhile (to_write) {\n+\t\tint read_pack = xread(fd, buffer, sizeof(buffer));\n+\n+\t\tif (read_pack <= 0)\n+\t\t\tdie_errno(\"unable to read from reused packfile\");\n+\n+\t\tif (read_pack > to_write)\n+\t\t\tread_pack = to_write;\n+\n+\t\tsha1write(f, buffer, read_pack);\n+\t\tto_write -= read_pack;\n+\t}\n+\n+\tclose(fd);\n+\twritten += reuse_packfile_objects;\n+\treturn reuse_packfile_offset - sizeof(struct pack_header);\n+}\n+\n static void write_pack_file(void)\n {\n \tuint32_t i = 0, j;\n@@ -704,6 +751,15 @@ static void write_pack_file(void)\n \t\toffset = write_pack_header(f, nr_remaining);\n \t\tif (!offset)\n \t\t\tdie_errno(\"unable to write pack header\");\n+\n+\t\tif (reuse_packfile) {\n+\t\t\toff_t packfile_size;\n+\t\t\tassert(pack_to_stdout);\n+\n+\t\t\tpackfile_size = write_reused_pack(f);\n+\t\t\toffset += packfile_size;\n+\t\t}\n+\n \t\tnr_written = 0;\n \t\tfor (; i < to_pack.nr_objects; i++) {\n \t\t\tstruct object_entry *e = write_order[i];\n@@ -923,6 +979,22 @@ static int add_object_entry(const unsigned char *sha1, enum object_type type,\n \treturn 1;\n }\n \n+static int add_object_entry_from_bitmap(const unsigned char *sha1,\n+\t\t\t\t\tenum object_type type,\n+\t\t\t\t\tint flags, uint32_t name_hash,\n+\t\t\t\t\tstruct packed_git *pack, off_t offset)\n+{\n+\tuint32_t index_pos;\n+\n+\tif (have_duplicate_entry(sha1, 0, &index_pos))\n+\t\treturn 0;\n+\n+\tcreate_object_entry(sha1, type, name_hash, 0, 0, index_pos, pack, offset);\n+\n+\tdisplay_progress(progress_state, to_pack.nr_objects);\n+\treturn 1;\n+}\n+\n struct pbase_tree_cache {\n \tunsigned char sha1[20];\n \tint ref;\n@@ -2085,6 +2157,10 @@ static int git_pack_config(const char *k, const char *v, void *cb)\n \t\tcache_max_small_delta_size = git_config_int(k, v);\n \t\treturn 0;\n \t}\n+\tif (!strcmp(k, \"pack.usebitmaps\")) {\n+\t\tuse_bitmap_index = git_config_bool(k, v);\n+\t\treturn 0;\n+\t}\n \tif (!strcmp(k, \"pack.threads\")) {\n \t\tdelta_search_threads = git_config_int(k, v);\n \t\tif (delta_search_threads < 0)\n@@ -2293,6 +2369,29 @@ static void loosen_unused_packed_objects(struct rev_info *revs)\n \t}\n }\n \n+static int get_object_list_from_bitmap(struct rev_info *revs)\n+{\n+\tif (prepare_bitmap_walk(revs) < 0)\n+\t\treturn -1;\n+\n+\tif (!reuse_partial_packfile_from_bitmap(\n+\t\t\t&reuse_packfile,\n+\t\t\t&reuse_packfile_objects,\n+\t\t\t&reuse_packfile_offset)) {\n+\t\tassert(reuse_packfile_objects);\n+\t\tnr_result += reuse_packfile_objects;\n+\n+\t\tif (progress) {\n+\t\t\tfprintf(stderr, \"Reusing existing pack: %d, done.\\n\",\n+\t\t\t\treuse_packfile_objects);\n+\t\t\tfflush(stderr);\n+\t\t}\n+\t}\n+\n+\ttraverse_bitmap_commit_list(&add_object_entry_from_bitmap);\n+\treturn 0;\n+}\n+\n static void get_object_list(int ac, const char **av)\n {\n \tstruct rev_info revs;\n@@ -2320,6 +2419,9 @@ static void get_object_list(int ac, const char **av)\n \t\t\tdie(\"bad revision '%s'\", line);\n \t}\n \n+\tif (use_bitmap_index && !get_object_list_from_bitmap(&revs))\n+\t\treturn;\n+\n \tif (prepare_revision_walk(&revs))\n \t\tdie(\"revision walk setup failed\");\n \tmark_edges_uninteresting(&revs, show_edge);\n@@ -2449,6 +2551,8 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)\n \t\t\t    N_(\"pack compression level\")),\n \t\tOPT_SET_INT(0, \"keep-true-parents\", &grafts_replace_parents,\n \t\t\t    N_(\"do not hide commits by grafts\"), 0),\n+\t\tOPT_BOOL(0, \"use-bitmap-index\", &use_bitmap_index,\n+\t\t\t N_(\"use a bitmap index if available to speed up counting objects\")),\n \t\tOPT_END(),\n \t};\n \n@@ -2515,6 +2619,9 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)\n \tif (keep_unreachable && unpack_unreachable)\n \t\tdie(\"--keep-unreachable and --unpack-unreachable are incompatible.\");\n \n+\tif (!use_internal_rev_list || !pack_to_stdout || is_repository_shallow())\n+\t\tuse_bitmap_index = 0;\n+\n \tif (progress && all_progress_implied)\n \t\tprogress = 2;\n \n-- \n1.8.5.1.399.g900e7cd\n"},{"id":"232319","messageId":"20131221140012.GM21145@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20131221135651.GA20818@sigill.intra.peff.net","subject":"[PATCH v4 13/23] rev-list: add bitmap mode to speed up object lists","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-12-21T14:00:12Z","receivedAt":"2013-12-21T14:00:12Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"From: Vicent Marti <tanoku@gmail.com>\n\nThe bitmap reachability index used to speed up the counting objects\nphase during `pack-objects` can also be used to optimize a normal\nrev-list if the only thing required are the SHA1s of the objects during\nthe list (i.e., not the path names at which trees and blobs were found).\n\nCalling `git rev-list --objects --use-bitmap-index [committish]` will\nperform an object iteration based on a bitmap result instead of actually\nwalking the object graph.\n\nThese are some example timings for `torvalds/linux` (warm cache,\nbest-of-five):\n\n    $ time git rev-list --objects master > /dev/null\n\n    real    0m34.191s\n    user    0m33.904s\n    sys     0m0.268s\n\n    $ time git rev-list --objects --use-bitmap-index master > /dev/null\n\n    real    0m1.041s\n    user    0m0.976s\n    sys     0m0.064s\n\nLikewise, using `git rev-list --count --use-bitmap-index` will speed up\nthe counting operation by building the resulting bitmap and performing a\nfast popcount (number of bits set on the bitmap) on the result.\n\nHere are some sample timings of different ways to count commits in\n`torvalds/linux`:\n\n    $ time git rev-list master | wc -l\n        399882\n\n        real    0m6.524s\n        user    0m6.060s\n        sys     0m3.284s\n\n    $ time git rev-list --count master\n        399882\n\n        real    0m4.318s\n        user    0m4.236s\n        sys     0m0.076s\n\n    $ time git rev-list --use-bitmap-index --count master\n        399882\n\n        real    0m0.217s\n        user    0m0.176s\n        sys     0m0.040s\n\nThis also respects negative refs, so you can use it to count\na slice of history:\n\n        $ time git rev-list --count v3.0..master\n        144843\n\n        real    0m1.971s\n        user    0m1.932s\n        sys     0m0.036s\n\n        $ time git rev-list --use-bitmap-index --count v3.0..master\n        real    0m0.280s\n        user    0m0.220s\n        sys     0m0.056s\n\nThough note that the closer the endpoints, the less it helps. In the\ntraversal case, we have fewer commits to cross, so we take less time.\nBut the bitmap time is dominated by generating the pack revindex, which\nis constant with respect to the refs given.\n\nNote that you cannot yet get a fast --left-right count of a symmetric\ndifference (e.g., \"--count --left-right master...topic\"). The slow part\nof that walk actually happens during the merge-base determination when\nwe parse \"master...topic\". Even though a count does not actually need to\nknow the real merge base (it only needs to take the symmetric difference\nof the bitmaps), the revision code would require some refactoring to\nhandle this case.\n\nAdditionally, a `--test-bitmap` flag has been added that will perform\nthe same rev-list manually (i.e. using a normal revwalk) and using\nbitmaps, and verify that the results are the same. This can be used to\nexercise the bitmap code, and also to verify that the contents of the\n.bitmap file are sane.\n\nSigned-off-by: Vicent Marti <tanoku@gmail.com>\nSigned-off-by: Jeff King <peff@peff.net>\n---\n Documentation/git-rev-list.txt     |  1 +\n Documentation/rev-list-options.txt |  8 ++++++++\n builtin/rev-list.c                 | 39 ++++++++++++++++++++++++++++++++++++++\n 3 files changed, 48 insertions(+)\n\ndiff --git a/Documentation/git-rev-list.txt b/Documentation/git-rev-list.txt\nindex 045b37b..7a1585d 100644\n--- a/Documentation/git-rev-list.txt\n+++ b/Documentation/git-rev-list.txt\n@@ -55,6 +55,7 @@ SYNOPSIS\n \t     [ \\--reverse ]\n \t     [ \\--walk-reflogs ]\n \t     [ \\--no-walk ] [ \\--do-walk ]\n+\t     [ \\--use-bitmap-index ]\n \t     <commit>... [ \\-- <paths>... ]\n \n DESCRIPTION\ndiff --git a/Documentation/rev-list-options.txt b/Documentation/rev-list-options.txt\nindex 5bdfb42..c236b85 100644\n--- a/Documentation/rev-list-options.txt\n+++ b/Documentation/rev-list-options.txt\n@@ -274,6 +274,14 @@ See also linkgit:git-reflog[1].\n \tOutput excluded boundary commits. Boundary commits are\n \tprefixed with `-`.\n \n+ifdef::git-rev-list[]\n+--use-bitmap-index::\n+\n+\tTry to speed up the traversal using the pack bitmap index (if\n+\tone is available). Note that when traversing with `--objects`,\n+\ttrees and blobs will not have their associated path printed.\n+endif::git-rev-list[]\n+\n --\n \n History Simplification\ndiff --git a/builtin/rev-list.c b/builtin/rev-list.c\nindex 4fc1616..5209255 100644\n--- a/builtin/rev-list.c\n+++ b/builtin/rev-list.c\n@@ -3,6 +3,8 @@\n #include \"diff.h\"\n #include \"revision.h\"\n #include \"list-objects.h\"\n+#include \"pack.h\"\n+#include \"pack-bitmap.h\"\n #include \"builtin.h\"\n #include \"log-tree.h\"\n #include \"graph.h\"\n@@ -257,6 +259,18 @@ static int show_bisect_vars(struct rev_list_info *info, int reaches, int all)\n \treturn 0;\n }\n \n+static int show_object_fast(\n+\tconst unsigned char *sha1,\n+\tenum object_type type,\n+\tint exclude,\n+\tuint32_t name_hash,\n+\tstruct packed_git *found_pack,\n+\toff_t found_offset)\n+{\n+\tfprintf(stdout, \"%s\\n\", sha1_to_hex(sha1));\n+\treturn 1;\n+}\n+\n int cmd_rev_list(int argc, const char **argv, const char *prefix)\n {\n \tstruct rev_info revs;\n@@ -265,6 +279,7 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)\n \tint bisect_list = 0;\n \tint bisect_show_vars = 0;\n \tint bisect_find_all = 0;\n+\tint use_bitmap_index = 0;\n \n \tgit_config(git_default_config, NULL);\n \tinit_revisions(&revs, prefix);\n@@ -306,6 +321,14 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)\n \t\t\tbisect_show_vars = 1;\n \t\t\tcontinue;\n \t\t}\n+\t\tif (!strcmp(arg, \"--use-bitmap-index\")) {\n+\t\t\tuse_bitmap_index = 1;\n+\t\t\tcontinue;\n+\t\t}\n+\t\tif (!strcmp(arg, \"--test-bitmap\")) {\n+\t\t\ttest_bitmap_walk(&revs);\n+\t\t\treturn 0;\n+\t\t}\n \t\tusage(rev_list_usage);\n \n \t}\n@@ -333,6 +356,22 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)\n \tif (bisect_list)\n \t\trevs.limited = 1;\n \n+\tif (use_bitmap_index) {\n+\t\tif (revs.count && !revs.left_right && !revs.cherry_mark) {\n+\t\t\tuint32_t commit_count;\n+\t\t\tif (!prepare_bitmap_walk(&revs)) {\n+\t\t\t\tcount_bitmap_commit_list(&commit_count, NULL, NULL, NULL);\n+\t\t\t\tprintf(\"%d\\n\", commit_count);\n+\t\t\t\treturn 0;\n+\t\t\t}\n+\t\t} else if (revs.tag_objects && revs.tree_objects && revs.blob_objects) {\n+\t\t\tif (!prepare_bitmap_walk(&revs)) {\n+\t\t\t\ttraverse_bitmap_commit_list(&show_object_fast);\n+\t\t\t\treturn 0;\n+\t\t\t}\n+\t\t}\n+\t}\n+\n \tif (prepare_revision_walk(&revs))\n \t\tdie(\"revision walk setup failed\");\n \tif (revs.tree_objects)\n-- \n1.8.5.1.399.g900e7cd\n"},{"id":"232328","messageId":"20131221140015.GN21145@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20131221135651.GA20818@sigill.intra.peff.net","subject":"[PATCH v4 14/23] pack-objects: implement bitmap writing","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-12-21T14:00:16Z","receivedAt":"2013-12-21T14:00:16Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"From: Vicent Marti <tanoku@gmail.com>\n\nThis commit extends more the functionality of `pack-objects` by allowing\nit to write out a `.bitmap` index next to any written packs, together\nwith the `.idx` index that currently gets written.\n\nIf bitmap writing is enabled for a given repository (either by calling\n`pack-objects` with the `--write-bitmap-index` flag or by having\n`pack.writebitmaps` set to `true` in the config) and pack-objects is\nwriting a packfile that would normally be indexed (i.e. not piping to\nstdout), we will attempt to write the corresponding bitmap index for the\npackfile.\n\nBitmap index writing happens after the packfile and its index has been\nsuccessfully written to disk (`finish_tmp_packfile`). The process is\nperformed in several steps:\n\n    1. `bitmap_writer_set_checksum`: this call stores the partial\n       checksum for the packfile being written; the checksum will be\n       written in the resulting bitmap index to verify its integrity\n\n    2. `bitmap_writer_build_type_index`: this call uses the array of\n       `struct object_entry` that has just been sorted when writing out\n       the actual packfile index to disk to generate 4 type-index bitmaps\n       (one for each object type).\n\n       These bitmaps have their nth bit set if the given object is of\n       the bitmap's type. E.g. the nth bit of the Commits bitmap will be\n       1 if the nth object in the packfile index is a commit.\n\n       This is a very cheap operation because the bitmap writing code has\n       access to the metadata stored in the `struct object_entry` array,\n       and hence the real type for each object in the packfile.\n\n    3. `bitmap_writer_reuse_bitmaps`: if there exists an existing bitmap\n       index for one of the packfiles we're trying to repack, this call\n       will efficiently rebuild the existing bitmaps so they can be\n       reused on the new index. All the existing bitmaps will be stored\n       in a `reuse` hash table, and the commit selection phase will\n       prioritize these when selecting, as they can be written directly\n       to the new index without having to perform a revision walk to\n       fill the bitmap. This can greatly speed up the repack of a\n       repository that already has bitmaps.\n\n    4. `bitmap_writer_select_commits`: if bitmap writing is enabled for\n       a given `pack-objects` run, the sequence of commits generated\n       during the Counting Objects phase will be stored in an array.\n\n       We then use that array to build up the list of selected commits.\n       Writing a bitmap in the index for each object in the repository\n       would be cost-prohibitive, so we use a simple heuristic to pick\n       the commits that will be indexed with bitmaps.\n\n       The current heuristics are a simplified version of JGit's\n       original implementation. We select a higher density of commits\n       depending on their age: the 100 most recent commits are always\n       selected, after that we pick 1 commit of each 100, and the gap\n       increases as the commits grow older. On top of that, we make sure\n       that every single branch that has not been merged (all the tips\n       that would be required from a clone) gets their own bitmap, and\n       when selecting commits between a gap, we tend to prioritize the\n       commit with the most parents.\n\n       Do note that there is no right/wrong way to perform commit\n       selection; different selection algorithms will result in\n       different commits being selected, but there's no such thing as\n       \"missing a commit\". The bitmap walker algorithm implemented in\n       `prepare_bitmap_walk` is able to adapt to missing bitmaps by\n       performing manual walks that complete the bitmap: the ideal\n       selection algorithm, however, would select the commits that are\n       more likely to be used as roots for a walk in the future (e.g.\n       the tips of each branch, and so on) to ensure a bitmap for them\n       is always available.\n\n    5. `bitmap_writer_build`: this is the computationally expensive part\n       of bitmap generation. Based on the list of commits that were\n       selected in the previous step, we perform several incremental\n       walks to generate the bitmap for each commit.\n\n       The walks begin from the oldest commit, and are built up\n       incrementally for each branch. E.g. consider this dag where A, B,\n       C, D, E, F are the selected commits, and a, b, c, e are a chunk\n       of simplified history that will not receive bitmaps.\n\n            A---a---B--b--C--c--D\n                     \\\n                      E--e--F\n\n       We start by building the bitmap for A, using A as the root for a\n       revision walk and marking all the objects that are reachable\n       until the walk is over. Once this bitmap is stored, we reuse the\n       bitmap walker to perform the walk for B, assuming that once we\n       reach A again, the walk will be terminated because A has already\n       been SEEN on the previous walk.\n\n       This process is repeated for C, and D, but when we try to\n       generate the bitmaps for E, we can reuse neither the current walk\n       nor the bitmap we have generated so far.\n\n       What we do now is resetting both the walk and clearing the\n       bitmap, and performing the walk from scratch using E as the\n       origin. This new walk, however, does not need to be completed.\n       Once we hit B, we can lookup the bitmap we have already stored\n       for that commit and OR it with the existing bitmap we've composed\n       so far, allowing us to limit the walk early.\n\n       After all the bitmaps have been generated, another iteration\n       through the list of commits is performed to find the best XOR\n       offsets for compression before writing them to disk. Because of\n       the incremental nature of these bitmaps, XORing one of them with\n       its predecesor results in a minimal \"bitmap delta\" most of the\n       time. We can write this delta to the on-disk bitmap index, and\n       then re-compose the original bitmaps by XORing them again when\n       loaded.\n\n       This is a phase very similar to pack-object's `find_delta` (using\n       bitmaps instead of objects, of course), except the heuristics\n       have been greatly simplified: we only check the 10 bitmaps before\n       any given one to find best compressing one. This gives good\n       results in practice, because there is locality in the ordering of\n       the objects (and therefore bitmaps) in the packfile.\n\n     6. `bitmap_writer_finish`: the last step in the process is\n\tserializing to disk all the bitmap data that has been generated\n\tin the two previous steps.\n\n\tThe bitmap is written to a tmp file and then moved atomically to\n\tits final destination, using the same process as\n\t`pack-write.c:write_idx_file`.\n\nSigned-off-by: Vicent Marti <tanoku@gmail.com>\nSigned-off-by: Jeff King <peff@peff.net>\n---\n Documentation/config.txt |   8 +\n Makefile                 |   1 +\n builtin/pack-objects.c   |  53 +++++\n pack-bitmap-write.c      | 535 +++++++++++++++++++++++++++++++++++++++++++++++\n pack-bitmap.c            |  92 ++++++++\n pack-bitmap.h            |  19 ++\n pack-objects.h           |   1 +\n pack-write.c             |   2 +\n 8 files changed, 711 insertions(+)\n create mode 100644 pack-bitmap-write.c\n\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex a981369..4b0c368 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -1864,6 +1864,14 @@ pack.useBitmaps::\n \ttrue. You should not generally need to turn this off unless\n \tyou are debugging pack bitmaps.\n \n+pack.writebitmaps::\n+\tWhen true, git will write a bitmap index when packing all\n+\tobjects to disk (e.g., when `git repack -a` is run).  This\n+\tindex can speed up the \"counting objects\" phase of subsequent\n+\tpacks created for clones and fetches, at the cost of some disk\n+\tspace and extra time spent on the initial repack.  Defaults to\n+\tfalse.\n+\n pager.<cmd>::\n \tIf the value is boolean, turns on or off pagination of the\n \toutput of a particular Git subcommand when writing to a tty.\ndiff --git a/Makefile b/Makefile\nindex b983d78..555d44c 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -839,6 +839,7 @@ LIB_OBJS += notes-merge.o\n LIB_OBJS += notes-utils.o\n LIB_OBJS += object.o\n LIB_OBJS += pack-bitmap.o\n+LIB_OBJS += pack-bitmap-write.o\n LIB_OBJS += pack-check.o\n LIB_OBJS += pack-objects.o\n LIB_OBJS += pack-revindex.o\ndiff --git a/builtin/pack-objects.c b/builtin/pack-objects.c\nindex 030d894..fd6ae01 100644\n--- a/builtin/pack-objects.c\n+++ b/builtin/pack-objects.c\n@@ -63,6 +63,7 @@ static uint32_t reuse_packfile_objects;\n static off_t reuse_packfile_offset;\n \n static int use_bitmap_index = 1;\n+static int write_bitmap_index;\n \n static unsigned long delta_cache_size = 0;\n static unsigned long max_delta_cache_size = 256 * 1024 * 1024;\n@@ -76,6 +77,24 @@ static unsigned long window_memory_limit = 0;\n static uint32_t written, written_delta;\n static uint32_t reused, reused_delta;\n \n+/*\n+ * Indexed commits\n+ */\n+static struct commit **indexed_commits;\n+static unsigned int indexed_commits_nr;\n+static unsigned int indexed_commits_alloc;\n+\n+static void index_commit_for_bitmap(struct commit *commit)\n+{\n+\tif (indexed_commits_nr >= indexed_commits_alloc) {\n+\t\tindexed_commits_alloc = (indexed_commits_alloc + 32) * 2;\n+\t\tindexed_commits = xrealloc(indexed_commits,\n+\t\t\tindexed_commits_alloc * sizeof(struct commit *));\n+\t}\n+\n+\tindexed_commits[indexed_commits_nr++] = commit;\n+}\n+\n static void *get_delta(struct object_entry *entry)\n {\n \tunsigned long size, base_size, delta_size;\n@@ -812,9 +831,30 @@ static void write_pack_file(void)\n \t\t\tif (sizeof(tmpname) <= strlen(base_name) + 50)\n \t\t\t\tdie(\"pack base name '%s' too long\", base_name);\n \t\t\tsnprintf(tmpname, sizeof(tmpname), \"%s-\", base_name);\n+\n+\t\t\tif (write_bitmap_index) {\n+\t\t\t\tbitmap_writer_set_checksum(sha1);\n+\t\t\t\tbitmap_writer_build_type_index(written_list, nr_written);\n+\t\t\t}\n+\n \t\t\tfinish_tmp_packfile(tmpname, pack_tmp_name,\n \t\t\t\t\t    written_list, nr_written,\n \t\t\t\t\t    &pack_idx_opts, sha1);\n+\n+\t\t\tif (write_bitmap_index) {\n+\t\t\t\tchar *end_of_name_prefix = strrchr(tmpname, 0);\n+\t\t\t\tsprintf(end_of_name_prefix, \"%s.bitmap\", sha1_to_hex(sha1));\n+\n+\t\t\t\tstop_progress(&progress_state);\n+\n+\t\t\t\tbitmap_writer_show_progress(progress);\n+\t\t\t\tbitmap_writer_reuse_bitmaps(&to_pack);\n+\t\t\t\tbitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1);\n+\t\t\t\tbitmap_writer_build(&to_pack);\n+\t\t\t\tbitmap_writer_finish(written_list, nr_written, tmpname);\n+\t\t\t\twrite_bitmap_index = 0;\n+\t\t\t}\n+\n \t\t\tfree(pack_tmp_name);\n \t\t\tputs(sha1_to_hex(sha1));\n \t\t}\n@@ -2157,6 +2197,10 @@ static int git_pack_config(const char *k, const char *v, void *cb)\n \t\tcache_max_small_delta_size = git_config_int(k, v);\n \t\treturn 0;\n \t}\n+\tif (!strcmp(k, \"pack.writebitmaps\")) {\n+\t\twrite_bitmap_index = git_config_bool(k, v);\n+\t\treturn 0;\n+\t}\n \tif (!strcmp(k, \"pack.usebitmaps\")) {\n \t\tuse_bitmap_index = git_config_bool(k, v);\n \t\treturn 0;\n@@ -2219,6 +2263,9 @@ static void show_commit(struct commit *commit, void *data)\n {\n \tadd_object_entry(commit->object.sha1, OBJ_COMMIT, NULL, 0);\n \tcommit->object.flags |= OBJECT_ADDED;\n+\n+\tif (write_bitmap_index)\n+\t\tindex_commit_for_bitmap(commit);\n }\n \n static void show_object(struct object *obj,\n@@ -2411,6 +2458,7 @@ static void get_object_list(int ac, const char **av)\n \t\tif (*line == '-') {\n \t\t\tif (!strcmp(line, \"--not\")) {\n \t\t\t\tflags ^= UNINTERESTING;\n+\t\t\t\twrite_bitmap_index = 0;\n \t\t\t\tcontinue;\n \t\t\t}\n \t\t\tdie(\"not a rev '%s'\", line);\n@@ -2553,6 +2601,8 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)\n \t\t\t    N_(\"do not hide commits by grafts\"), 0),\n \t\tOPT_BOOL(0, \"use-bitmap-index\", &use_bitmap_index,\n \t\t\t N_(\"use a bitmap index if available to speed up counting objects\")),\n+\t\tOPT_BOOL(0, \"write-bitmap-index\", &write_bitmap_index,\n+\t\t\t N_(\"write a bitmap index together with the pack index\")),\n \t\tOPT_END(),\n \t};\n \n@@ -2622,6 +2672,9 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)\n \tif (!use_internal_rev_list || !pack_to_stdout || is_repository_shallow())\n \t\tuse_bitmap_index = 0;\n \n+\tif (pack_to_stdout || !rev_list_all)\n+\t\twrite_bitmap_index = 0;\n+\n \tif (progress && all_progress_implied)\n \t\tprogress = 2;\n \ndiff --git a/pack-bitmap-write.c b/pack-bitmap-write.c\nnew file mode 100644\nindex 0000000..954a74d\n--- /dev/null\n+++ b/pack-bitmap-write.c\n@@ -0,0 +1,535 @@\n+#include \"cache.h\"\n+#include \"commit.h\"\n+#include \"tag.h\"\n+#include \"diff.h\"\n+#include \"revision.h\"\n+#include \"list-objects.h\"\n+#include \"progress.h\"\n+#include \"pack-revindex.h\"\n+#include \"pack.h\"\n+#include \"pack-bitmap.h\"\n+#include \"sha1-lookup.h\"\n+#include \"pack-objects.h\"\n+\n+struct bitmapped_commit {\n+\tstruct commit *commit;\n+\tstruct ewah_bitmap *bitmap;\n+\tstruct ewah_bitmap *write_as;\n+\tint flags;\n+\tint xor_offset;\n+\tuint32_t commit_pos;\n+};\n+\n+struct bitmap_writer {\n+\tstruct ewah_bitmap *commits;\n+\tstruct ewah_bitmap *trees;\n+\tstruct ewah_bitmap *blobs;\n+\tstruct ewah_bitmap *tags;\n+\n+\tkhash_sha1 *bitmaps;\n+\tkhash_sha1 *reused;\n+\tstruct packing_data *to_pack;\n+\n+\tstruct bitmapped_commit *selected;\n+\tunsigned int selected_nr, selected_alloc;\n+\n+\tstruct progress *progress;\n+\tint show_progress;\n+\tunsigned char pack_checksum[20];\n+};\n+\n+static struct bitmap_writer writer;\n+\n+void bitmap_writer_show_progress(int show)\n+{\n+\twriter.show_progress = show;\n+}\n+\n+/**\n+ * Build the initial type index for the packfile\n+ */\n+void bitmap_writer_build_type_index(struct pack_idx_entry **index,\n+\t\t\t\t    uint32_t index_nr)\n+{\n+\tuint32_t i;\n+\n+\twriter.commits = ewah_new();\n+\twriter.trees = ewah_new();\n+\twriter.blobs = ewah_new();\n+\twriter.tags = ewah_new();\n+\n+\tfor (i = 0; i < index_nr; ++i) {\n+\t\tstruct object_entry *entry = (struct object_entry *)index[i];\n+\t\tenum object_type real_type;\n+\n+\t\tentry->in_pack_pos = i;\n+\n+\t\tswitch (entry->type) {\n+\t\tcase OBJ_COMMIT:\n+\t\tcase OBJ_TREE:\n+\t\tcase OBJ_BLOB:\n+\t\tcase OBJ_TAG:\n+\t\t\treal_type = entry->type;\n+\t\t\tbreak;\n+\n+\t\tdefault:\n+\t\t\treal_type = sha1_object_info(entry->idx.sha1, NULL);\n+\t\t\tbreak;\n+\t\t}\n+\n+\t\tswitch (real_type) {\n+\t\tcase OBJ_COMMIT:\n+\t\t\tewah_set(writer.commits, i);\n+\t\t\tbreak;\n+\n+\t\tcase OBJ_TREE:\n+\t\t\tewah_set(writer.trees, i);\n+\t\t\tbreak;\n+\n+\t\tcase OBJ_BLOB:\n+\t\t\tewah_set(writer.blobs, i);\n+\t\t\tbreak;\n+\n+\t\tcase OBJ_TAG:\n+\t\t\tewah_set(writer.tags, i);\n+\t\t\tbreak;\n+\n+\t\tdefault:\n+\t\t\tdie(\"Missing type information for %s (%d/%d)\",\n+\t\t\t    sha1_to_hex(entry->idx.sha1), real_type, entry->type);\n+\t\t}\n+\t}\n+}\n+\n+/**\n+ * Compute the actual bitmaps\n+ */\n+static struct object **seen_objects;\n+static unsigned int seen_objects_nr, seen_objects_alloc;\n+\n+static inline void push_bitmapped_commit(struct commit *commit, struct ewah_bitmap *reused)\n+{\n+\tif (writer.selected_nr >= writer.selected_alloc) {\n+\t\twriter.selected_alloc = (writer.selected_alloc + 32) * 2;\n+\t\twriter.selected = xrealloc(writer.selected,\n+\t\t\t\t\t   writer.selected_alloc * sizeof(struct bitmapped_commit));\n+\t}\n+\n+\twriter.selected[writer.selected_nr].commit = commit;\n+\twriter.selected[writer.selected_nr].bitmap = reused;\n+\twriter.selected[writer.selected_nr].flags = 0;\n+\n+\twriter.selected_nr++;\n+}\n+\n+static inline void mark_as_seen(struct object *object)\n+{\n+\tALLOC_GROW(seen_objects, seen_objects_nr + 1, seen_objects_alloc);\n+\tseen_objects[seen_objects_nr++] = object;\n+}\n+\n+static inline void reset_all_seen(void)\n+{\n+\tunsigned int i;\n+\tfor (i = 0; i < seen_objects_nr; ++i) {\n+\t\tseen_objects[i]->flags &= ~(SEEN | ADDED | SHOWN);\n+\t}\n+\tseen_objects_nr = 0;\n+}\n+\n+static uint32_t find_object_pos(const unsigned char *sha1)\n+{\n+\tstruct object_entry *entry = packlist_find(writer.to_pack, sha1, NULL);\n+\n+\tif (!entry) {\n+\t\tdie(\"Failed to write bitmap index. Packfile doesn't have full closure \"\n+\t\t\t\"(object %s is missing)\", sha1_to_hex(sha1));\n+\t}\n+\n+\treturn entry->in_pack_pos;\n+}\n+\n+static void show_object(struct object *object, const struct name_path *path,\n+\t\t\tconst char *last, void *data)\n+{\n+\tstruct bitmap *base = data;\n+\tbitmap_set(base, find_object_pos(object->sha1));\n+\tmark_as_seen(object);\n+}\n+\n+static void show_commit(struct commit *commit, void *data)\n+{\n+\tmark_as_seen((struct object *)commit);\n+}\n+\n+static int\n+add_to_include_set(struct bitmap *base, struct commit *commit)\n+{\n+\tkhiter_t hash_pos;\n+\tuint32_t bitmap_pos = find_object_pos(commit->object.sha1);\n+\n+\tif (bitmap_get(base, bitmap_pos))\n+\t\treturn 0;\n+\n+\thash_pos = kh_get_sha1(writer.bitmaps, commit->object.sha1);\n+\tif (hash_pos < kh_end(writer.bitmaps)) {\n+\t\tstruct bitmapped_commit *bc = kh_value(writer.bitmaps, hash_pos);\n+\t\tbitmap_or_ewah(base, bc->bitmap);\n+\t\treturn 0;\n+\t}\n+\n+\tbitmap_set(base, bitmap_pos);\n+\treturn 1;\n+}\n+\n+static int\n+should_include(struct commit *commit, void *_data)\n+{\n+\tstruct bitmap *base = _data;\n+\n+\tif (!add_to_include_set(base, commit)) {\n+\t\tstruct commit_list *parent = commit->parents;\n+\n+\t\tmark_as_seen((struct object *)commit);\n+\n+\t\twhile (parent) {\n+\t\t\tparent->item->object.flags |= SEEN;\n+\t\t\tmark_as_seen((struct object *)parent->item);\n+\t\t\tparent = parent->next;\n+\t\t}\n+\n+\t\treturn 0;\n+\t}\n+\n+\treturn 1;\n+}\n+\n+static void compute_xor_offsets(void)\n+{\n+\tstatic const int MAX_XOR_OFFSET_SEARCH = 10;\n+\n+\tint i, next = 0;\n+\n+\twhile (next < writer.selected_nr) {\n+\t\tstruct bitmapped_commit *stored = &writer.selected[next];\n+\n+\t\tint best_offset = 0;\n+\t\tstruct ewah_bitmap *best_bitmap = stored->bitmap;\n+\t\tstruct ewah_bitmap *test_xor;\n+\n+\t\tfor (i = 1; i <= MAX_XOR_OFFSET_SEARCH; ++i) {\n+\t\t\tint curr = next - i;\n+\n+\t\t\tif (curr < 0)\n+\t\t\t\tbreak;\n+\n+\t\t\ttest_xor = ewah_pool_new();\n+\t\t\tewah_xor(writer.selected[curr].bitmap, stored->bitmap, test_xor);\n+\n+\t\t\tif (test_xor->buffer_size < best_bitmap->buffer_size) {\n+\t\t\t\tif (best_bitmap != stored->bitmap)\n+\t\t\t\t\tewah_pool_free(best_bitmap);\n+\n+\t\t\t\tbest_bitmap = test_xor;\n+\t\t\t\tbest_offset = i;\n+\t\t\t} else {\n+\t\t\t\tewah_pool_free(test_xor);\n+\t\t\t}\n+\t\t}\n+\n+\t\tstored->xor_offset = best_offset;\n+\t\tstored->write_as = best_bitmap;\n+\n+\t\tnext++;\n+\t}\n+}\n+\n+void bitmap_writer_build(struct packing_data *to_pack)\n+{\n+\tstatic const double REUSE_BITMAP_THRESHOLD = 0.2;\n+\n+\tint i, reuse_after, need_reset;\n+\tstruct bitmap *base = bitmap_new();\n+\tstruct rev_info revs;\n+\n+\twriter.bitmaps = kh_init_sha1();\n+\twriter.to_pack = to_pack;\n+\n+\tif (writer.show_progress)\n+\t\twriter.progress = start_progress(\"Building bitmaps\", writer.selected_nr);\n+\n+\tinit_revisions(&revs, NULL);\n+\trevs.tag_objects = 1;\n+\trevs.tree_objects = 1;\n+\trevs.blob_objects = 1;\n+\trevs.no_walk = 0;\n+\n+\trevs.include_check = should_include;\n+\treset_revision_walk();\n+\n+\treuse_after = writer.selected_nr * REUSE_BITMAP_THRESHOLD;\n+\tneed_reset = 0;\n+\n+\tfor (i = writer.selected_nr - 1; i >= 0; --i) {\n+\t\tstruct bitmapped_commit *stored;\n+\t\tstruct object *object;\n+\n+\t\tkhiter_t hash_pos;\n+\t\tint hash_ret;\n+\n+\t\tstored = &writer.selected[i];\n+\t\tobject = (struct object *)stored->commit;\n+\n+\t\tif (stored->bitmap == NULL) {\n+\t\t\tif (i < writer.selected_nr - 1 &&\n+\t\t\t    (need_reset ||\n+\t\t\t     !in_merge_bases(writer.selected[i + 1].commit,\n+\t\t\t\t\t     stored->commit))) {\n+\t\t\t    bitmap_reset(base);\n+\t\t\t    reset_all_seen();\n+\t\t\t}\n+\n+\t\t\tadd_pending_object(&revs, object, \"\");\n+\t\t\trevs.include_check_data = base;\n+\n+\t\t\tif (prepare_revision_walk(&revs))\n+\t\t\t\tdie(\"revision walk setup failed\");\n+\n+\t\t\ttraverse_commit_list(&revs, show_commit, show_object, base);\n+\n+\t\t\trevs.pending.nr = 0;\n+\t\t\trevs.pending.alloc = 0;\n+\t\t\trevs.pending.objects = NULL;\n+\n+\t\t\tstored->bitmap = bitmap_to_ewah(base);\n+\t\t\tneed_reset = 0;\n+\t\t} else\n+\t\t\tneed_reset = 1;\n+\n+\t\tif (i >= reuse_after)\n+\t\t\tstored->flags |= BITMAP_FLAG_REUSE;\n+\n+\t\thash_pos = kh_put_sha1(writer.bitmaps, object->sha1, &hash_ret);\n+\t\tif (hash_ret == 0)\n+\t\t\tdie(\"Duplicate entry when writing index: %s\",\n+\t\t\t    sha1_to_hex(object->sha1));\n+\n+\t\tkh_value(writer.bitmaps, hash_pos) = stored;\n+\t\tdisplay_progress(writer.progress, writer.selected_nr - i);\n+\t}\n+\n+\tbitmap_free(base);\n+\tstop_progress(&writer.progress);\n+\n+\tcompute_xor_offsets();\n+}\n+\n+/**\n+ * Select the commits that will be bitmapped\n+ */\n+static inline unsigned int next_commit_index(unsigned int idx)\n+{\n+\tstatic const unsigned int MIN_COMMITS = 100;\n+\tstatic const unsigned int MAX_COMMITS = 5000;\n+\n+\tstatic const unsigned int MUST_REGION = 100;\n+\tstatic const unsigned int MIN_REGION = 20000;\n+\n+\tunsigned int offset, next;\n+\n+\tif (idx <= MUST_REGION)\n+\t\treturn 0;\n+\n+\tif (idx <= MIN_REGION) {\n+\t\toffset = idx - MUST_REGION;\n+\t\treturn (offset < MIN_COMMITS) ? offset : MIN_COMMITS;\n+\t}\n+\n+\toffset = idx - MIN_REGION;\n+\tnext = (offset < MAX_COMMITS) ? offset : MAX_COMMITS;\n+\n+\treturn (next > MIN_COMMITS) ? next : MIN_COMMITS;\n+}\n+\n+static int date_compare(const void *_a, const void *_b)\n+{\n+\tstruct commit *a = *(struct commit **)_a;\n+\tstruct commit *b = *(struct commit **)_b;\n+\treturn (long)b->date - (long)a->date;\n+}\n+\n+void bitmap_writer_reuse_bitmaps(struct packing_data *to_pack)\n+{\n+\tif (prepare_bitmap_git() < 0)\n+\t\treturn;\n+\n+\twriter.reused = kh_init_sha1();\n+\trebuild_existing_bitmaps(to_pack, writer.reused, writer.show_progress);\n+}\n+\n+static struct ewah_bitmap *find_reused_bitmap(const unsigned char *sha1)\n+{\n+\tkhiter_t hash_pos;\n+\n+\tif (!writer.reused)\n+\t\treturn NULL;\n+\n+\thash_pos = kh_get_sha1(writer.reused, sha1);\n+\tif (hash_pos >= kh_end(writer.reused))\n+\t\treturn NULL;\n+\n+\treturn kh_value(writer.reused, hash_pos);\n+}\n+\n+void bitmap_writer_select_commits(struct commit **indexed_commits,\n+\t\t\t\t  unsigned int indexed_commits_nr,\n+\t\t\t\t  int max_bitmaps)\n+{\n+\tunsigned int i = 0, j, next;\n+\n+\tqsort(indexed_commits, indexed_commits_nr, sizeof(indexed_commits[0]),\n+\t      date_compare);\n+\n+\tif (writer.show_progress)\n+\t\twriter.progress = start_progress(\"Selecting bitmap commits\", 0);\n+\n+\tif (indexed_commits_nr < 100) {\n+\t\tfor (i = 0; i < indexed_commits_nr; ++i)\n+\t\t\tpush_bitmapped_commit(indexed_commits[i], NULL);\n+\t\treturn;\n+\t}\n+\n+\tfor (;;) {\n+\t\tstruct ewah_bitmap *reused_bitmap = NULL;\n+\t\tstruct commit *chosen = NULL;\n+\n+\t\tnext = next_commit_index(i);\n+\n+\t\tif (i + next >= indexed_commits_nr)\n+\t\t\tbreak;\n+\n+\t\tif (max_bitmaps > 0 && writer.selected_nr >= max_bitmaps) {\n+\t\t\twriter.selected_nr = max_bitmaps;\n+\t\t\tbreak;\n+\t\t}\n+\n+\t\tif (next == 0) {\n+\t\t\tchosen = indexed_commits[i];\n+\t\t\treused_bitmap = find_reused_bitmap(chosen->object.sha1);\n+\t\t} else {\n+\t\t\tchosen = indexed_commits[i + next];\n+\n+\t\t\tfor (j = 0; j <= next; ++j) {\n+\t\t\t\tstruct commit *cm = indexed_commits[i + j];\n+\n+\t\t\t\treused_bitmap = find_reused_bitmap(cm->object.sha1);\n+\t\t\t\tif (reused_bitmap || (cm->object.flags & NEEDS_BITMAP) != 0) {\n+\t\t\t\t\tchosen = cm;\n+\t\t\t\t\tbreak;\n+\t\t\t\t}\n+\n+\t\t\t\tif (cm->parents && cm->parents->next)\n+\t\t\t\t\tchosen = cm;\n+\t\t\t}\n+\t\t}\n+\n+\t\tpush_bitmapped_commit(chosen, reused_bitmap);\n+\n+\t\ti += next + 1;\n+\t\tdisplay_progress(writer.progress, i);\n+\t}\n+\n+\tstop_progress(&writer.progress);\n+}\n+\n+\n+static int sha1write_ewah_helper(void *f, const void *buf, size_t len)\n+{\n+\t/* sha1write will die on error */\n+\tsha1write(f, buf, len);\n+\treturn len;\n+}\n+\n+/**\n+ * Write the bitmap index to disk\n+ */\n+static inline void dump_bitmap(struct sha1file *f, struct ewah_bitmap *bitmap)\n+{\n+\tif (ewah_serialize_to(bitmap, sha1write_ewah_helper, f) < 0)\n+\t\tdie(\"Failed to write bitmap index\");\n+}\n+\n+static const unsigned char *sha1_access(size_t pos, void *table)\n+{\n+\tstruct pack_idx_entry **index = table;\n+\treturn index[pos]->sha1;\n+}\n+\n+static void write_selected_commits_v1(struct sha1file *f,\n+\t\t\t\t      struct pack_idx_entry **index,\n+\t\t\t\t      uint32_t index_nr)\n+{\n+\tint i;\n+\n+\tfor (i = 0; i < writer.selected_nr; ++i) {\n+\t\tstruct bitmapped_commit *stored = &writer.selected[i];\n+\t\tstruct bitmap_disk_entry on_disk;\n+\n+\t\tint commit_pos =\n+\t\t\tsha1_pos(stored->commit->object.sha1, index, index_nr, sha1_access);\n+\n+\t\tif (commit_pos < 0)\n+\t\t\tdie(\"BUG: trying to write commit not in index\");\n+\n+\t\ton_disk.object_pos = htonl(commit_pos);\n+\t\ton_disk.xor_offset = stored->xor_offset;\n+\t\ton_disk.flags = stored->flags;\n+\n+\t\tsha1write(f, &on_disk, sizeof(on_disk));\n+\t\tdump_bitmap(f, stored->write_as);\n+\t}\n+}\n+\n+void bitmap_writer_set_checksum(unsigned char *sha1)\n+{\n+\thashcpy(writer.pack_checksum, sha1);\n+}\n+\n+void bitmap_writer_finish(struct pack_idx_entry **index,\n+\t\t\t  uint32_t index_nr,\n+\t\t\t  const char *filename)\n+{\n+\tstatic char tmp_file[PATH_MAX];\n+\tstatic uint16_t default_version = 1;\n+\tstatic uint16_t flags = BITMAP_OPT_FULL_DAG;\n+\tstruct sha1file *f;\n+\n+\tstruct bitmap_disk_header header;\n+\n+\tint fd = odb_mkstemp(tmp_file, sizeof(tmp_file), \"pack/tmp_bitmap_XXXXXX\");\n+\n+\tif (fd < 0)\n+\t\tdie_errno(\"unable to create '%s'\", tmp_file);\n+\tf = sha1fd(fd, tmp_file);\n+\n+\tmemcpy(header.magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE));\n+\theader.version = htons(default_version);\n+\theader.options = htons(flags);\n+\theader.entry_count = htonl(writer.selected_nr);\n+\tmemcpy(header.checksum, writer.pack_checksum, 20);\n+\n+\tsha1write(f, &header, sizeof(header));\n+\tdump_bitmap(f, writer.commits);\n+\tdump_bitmap(f, writer.trees);\n+\tdump_bitmap(f, writer.blobs);\n+\tdump_bitmap(f, writer.tags);\n+\twrite_selected_commits_v1(f, index, index_nr);\n+\n+\tsha1close(f, NULL, CSUM_FSYNC);\n+\n+\tif (adjust_shared_perm(tmp_file))\n+\t\tdie_errno(\"unable to make temporary bitmap file readable\");\n+\n+\tif (rename(tmp_file, filename))\n+\t\tdie_errno(\"unable to rename temporary bitmap file to '%s'\", filename);\n+}\ndiff --git a/pack-bitmap.c b/pack-bitmap.c\nindex 33e7482..82090a6 100644\n--- a/pack-bitmap.c\n+++ b/pack-bitmap.c\n@@ -968,3 +968,95 @@ void test_bitmap_walk(struct rev_info *revs)\n \telse\n \t\tfprintf(stderr, \"Mismatch!\\n\");\n }\n+\n+static int rebuild_bitmap(uint32_t *reposition,\n+\t\t\t  struct ewah_bitmap *source,\n+\t\t\t  struct bitmap *dest)\n+{\n+\tuint32_t pos = 0;\n+\tstruct ewah_iterator it;\n+\teword_t word;\n+\n+\tewah_iterator_init(&it, source);\n+\n+\twhile (ewah_iterator_next(&word, &it)) {\n+\t\tuint32_t offset, bit_pos;\n+\n+\t\tfor (offset = 0; offset < BITS_IN_WORD; ++offset) {\n+\t\t\tif ((word >> offset) == 0)\n+\t\t\t\tbreak;\n+\n+\t\t\toffset += ewah_bit_ctz64(word >> offset);\n+\n+\t\t\tbit_pos = reposition[pos + offset];\n+\t\t\tif (bit_pos > 0)\n+\t\t\t\tbitmap_set(dest, bit_pos - 1);\n+\t\t\telse /* can't reuse, we don't have the object */\n+\t\t\t\treturn -1;\n+\t\t}\n+\n+\t\tpos += BITS_IN_WORD;\n+\t}\n+\treturn 0;\n+}\n+\n+int rebuild_existing_bitmaps(struct packing_data *mapping,\n+\t\t\t     khash_sha1 *reused_bitmaps,\n+\t\t\t     int show_progress)\n+{\n+\tuint32_t i, num_objects;\n+\tuint32_t *reposition;\n+\tstruct bitmap *rebuild;\n+\tstruct stored_bitmap *stored;\n+\tstruct progress *progress = NULL;\n+\n+\tkhiter_t hash_pos;\n+\tint hash_ret;\n+\n+\tif (prepare_bitmap_git() < 0)\n+\t\treturn -1;\n+\n+\tnum_objects = bitmap_git.pack->num_objects;\n+\treposition = xcalloc(num_objects, sizeof(uint32_t));\n+\n+\tfor (i = 0; i < num_objects; ++i) {\n+\t\tconst unsigned char *sha1;\n+\t\tstruct revindex_entry *entry;\n+\t\tstruct object_entry *oe;\n+\n+\t\tentry = &bitmap_git.reverse_index->revindex[i];\n+\t\tsha1 = nth_packed_object_sha1(bitmap_git.pack, entry->nr);\n+\t\toe = packlist_find(mapping, sha1, NULL);\n+\n+\t\tif (oe)\n+\t\t\treposition[i] = oe->in_pack_pos + 1;\n+\t}\n+\n+\trebuild = bitmap_new();\n+\ti = 0;\n+\n+\tif (show_progress)\n+\t\tprogress = start_progress(\"Reusing bitmaps\", 0);\n+\n+\tkh_foreach_value(bitmap_git.bitmaps, stored, {\n+\t\tif (stored->flags & BITMAP_FLAG_REUSE) {\n+\t\t\tif (!rebuild_bitmap(reposition,\n+\t\t\t\t\t    lookup_stored_bitmap(stored),\n+\t\t\t\t\t    rebuild)) {\n+\t\t\t\thash_pos = kh_put_sha1(reused_bitmaps,\n+\t\t\t\t\t\t       stored->sha1,\n+\t\t\t\t\t\t       &hash_ret);\n+\t\t\t\tkh_value(reused_bitmaps, hash_pos) =\n+\t\t\t\t\tbitmap_to_ewah(rebuild);\n+\t\t\t}\n+\t\t\tbitmap_reset(rebuild);\n+\t\t\tdisplay_progress(progress, ++i);\n+\t\t}\n+\t});\n+\n+\tstop_progress(&progress);\n+\n+\tfree(reposition);\n+\tbitmap_free(rebuild);\n+\treturn 0;\n+}\ndiff --git a/pack-bitmap.h b/pack-bitmap.h\nindex b4510d5..09acf02 100644\n--- a/pack-bitmap.h\n+++ b/pack-bitmap.h\n@@ -3,6 +3,7 @@\n \n #include \"ewah/ewok.h\"\n #include \"khash.h\"\n+#include \"pack-objects.h\"\n \n struct bitmap_disk_entry {\n \tuint32_t object_pos;\n@@ -20,10 +21,16 @@ struct bitmap_disk_header {\n \n static const char BITMAP_IDX_SIGNATURE[] = {'B', 'I', 'T', 'M'};\n \n+#define NEEDS_BITMAP (1u<<22)\n+\n enum pack_bitmap_opts {\n \tBITMAP_OPT_FULL_DAG = 1\n };\n \n+enum pack_bitmap_flags {\n+\tBITMAP_FLAG_REUSE = 0x1\n+};\n+\n typedef int (*show_reachable_fn)(\n \tconst unsigned char *sha1,\n \tenum object_type type,\n@@ -39,5 +46,17 @@ void test_bitmap_walk(struct rev_info *revs);\n char *pack_bitmap_filename(struct packed_git *p);\n int prepare_bitmap_walk(struct rev_info *revs);\n int reuse_partial_packfile_from_bitmap(struct packed_git **packfile, uint32_t *entries, off_t *up_to);\n+int rebuild_existing_bitmaps(struct packing_data *mapping, khash_sha1 *reused_bitmaps, int show_progress);\n+\n+void bitmap_writer_show_progress(int show);\n+void bitmap_writer_set_checksum(unsigned char *sha1);\n+void bitmap_writer_build_type_index(struct pack_idx_entry **index, uint32_t index_nr);\n+void bitmap_writer_reuse_bitmaps(struct packing_data *to_pack);\n+void bitmap_writer_select_commits(struct commit **indexed_commits,\n+\t\tunsigned int indexed_commits_nr, int max_bitmaps);\n+void bitmap_writer_build(struct packing_data *to_pack);\n+void bitmap_writer_finish(struct pack_idx_entry **index,\n+\t\t\t  uint32_t index_nr,\n+\t\t\t  const char *filename);\n \n #endif\ndiff --git a/pack-objects.h b/pack-objects.h\nindex 90ad0a8..d1b98b3 100644\n--- a/pack-objects.h\n+++ b/pack-objects.h\n@@ -17,6 +17,7 @@ struct object_entry {\n \tenum object_type type;\n \tenum object_type in_pack_type;\t/* could be delta */\n \tuint32_t hash;\t\t\t/* name hint hash */\n+\tunsigned int in_pack_pos;\n \tunsigned char in_pack_header_size;\n \tunsigned preferred_base:1; /*\n \t\t\t\t    * we do not pack this, but is available\ndiff --git a/pack-write.c b/pack-write.c\nindex ca9e63b..6203d37 100644\n--- a/pack-write.c\n+++ b/pack-write.c\n@@ -371,5 +371,7 @@ void finish_tmp_packfile(char *name_buffer,\n \tif (rename(idx_tmp_name, name_buffer))\n \t\tdie_errno(\"unable to rename temporary index file\");\n \n+\t*end_of_name_prefix = '\\0';\n+\n \tfree((void *)idx_tmp_name);\n }\n-- \n1.8.5.1.399.g900e7cd\n"},{"id":"232320","messageId":"20131221140019.GO21145@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20131221135651.GA20818@sigill.intra.peff.net","subject":"[PATCH v4 15/23] repack: stop using magic number for ARRAY_SIZE(exts)","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-12-21T14:00:19Z","receivedAt":"2013-12-21T14:00:19Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"We have a static array of extensions, but hardcode the size\nof the array in our loops. Let's pull out this magic number,\nwhich will make it easier to change.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\n builtin/repack.c | 8 ++++----\n 1 file changed, 4 insertions(+), 4 deletions(-)\n\ndiff --git a/builtin/repack.c b/builtin/repack.c\nindex a0ff5c7..2e88975 100644\n--- a/builtin/repack.c\n+++ b/builtin/repack.c\n@@ -115,7 +115,7 @@ static void remove_redundant_pack(const char *dir_name, const char *base_name)\n \n int cmd_repack(int argc, const char **argv, const char *prefix)\n {\n-\tconst char *exts[2] = {\".pack\", \".idx\"};\n+\tconst char *exts[] = {\".pack\", \".idx\"};\n \tstruct child_process cmd;\n \tstruct string_list_item *item;\n \tstruct argv_array cmd_args = ARGV_ARRAY_INIT;\n@@ -258,7 +258,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)\n \t */\n \tfailed = 0;\n \tfor_each_string_list_item(item, &names) {\n-\t\tfor (ext = 0; ext < 2; ext++) {\n+\t\tfor (ext = 0; ext < ARRAY_SIZE(exts); ext++) {\n \t\t\tchar *fname, *fname_old;\n \t\t\tfname = mkpathdup(\"%s/%s%s\", packdir,\n \t\t\t\t\t\titem->string, exts[ext]);\n@@ -315,7 +315,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)\n \n \t/* Now the ones with the same name are out of the way... */\n \tfor_each_string_list_item(item, &names) {\n-\t\tfor (ext = 0; ext < 2; ext++) {\n+\t\tfor (ext = 0; ext < ARRAY_SIZE(exts); ext++) {\n \t\t\tchar *fname, *fname_old;\n \t\t\tstruct stat statbuffer;\n \t\t\tfname = mkpathdup(\"%s/pack-%s%s\",\n@@ -335,7 +335,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)\n \n \t/* Remove the \"old-\" files */\n \tfor_each_string_list_item(item, &names) {\n-\t\tfor (ext = 0; ext < 2; ext++) {\n+\t\tfor (ext = 0; ext < ARRAY_SIZE(exts); ext++) {\n \t\t\tchar *fname;\n \t\t\tfname = mkpath(\"%s/old-pack-%s%s\",\n \t\t\t\t\tpackdir,\n-- \n1.8.5.1.399.g900e7cd\n"},{"id":"232318","messageId":"20131221140023.GP21145@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20131221135651.GA20818@sigill.intra.peff.net","subject":"[PATCH v4 16/23] repack: turn exts array into array-of-struct","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-12-21T14:00:23Z","receivedAt":"2013-12-21T14:00:23Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"This is slightly more verbose, but will let us annotate the\nextensions with further options in future commits.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\n builtin/repack.c | 17 +++++++++++------\n 1 file changed, 11 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/repack.c b/builtin/repack.c\nindex 2e88975..a176de2 100644\n--- a/builtin/repack.c\n+++ b/builtin/repack.c\n@@ -115,7 +115,12 @@ static void remove_redundant_pack(const char *dir_name, const char *base_name)\n \n int cmd_repack(int argc, const char **argv, const char *prefix)\n {\n-\tconst char *exts[] = {\".pack\", \".idx\"};\n+\tstruct {\n+\t\tconst char *name;\n+\t} exts[] = {\n+\t\t{\".pack\"},\n+\t\t{\".idx\"},\n+\t};\n \tstruct child_process cmd;\n \tstruct string_list_item *item;\n \tstruct argv_array cmd_args = ARGV_ARRAY_INIT;\n@@ -261,14 +266,14 @@ int cmd_repack(int argc, const char **argv, const char *prefix)\n \t\tfor (ext = 0; ext < ARRAY_SIZE(exts); ext++) {\n \t\t\tchar *fname, *fname_old;\n \t\t\tfname = mkpathdup(\"%s/%s%s\", packdir,\n-\t\t\t\t\t\titem->string, exts[ext]);\n+\t\t\t\t\t\titem->string, exts[ext].name);\n \t\t\tif (!file_exists(fname)) {\n \t\t\t\tfree(fname);\n \t\t\t\tcontinue;\n \t\t\t}\n \n \t\t\tfname_old = mkpath(\"%s/old-%s%s\", packdir,\n-\t\t\t\t\t\titem->string, exts[ext]);\n+\t\t\t\t\t\titem->string, exts[ext].name);\n \t\t\tif (file_exists(fname_old))\n \t\t\t\tif (unlink(fname_old))\n \t\t\t\t\tfailed = 1;\n@@ -319,9 +324,9 @@ int cmd_repack(int argc, const char **argv, const char *prefix)\n \t\t\tchar *fname, *fname_old;\n \t\t\tstruct stat statbuffer;\n \t\t\tfname = mkpathdup(\"%s/pack-%s%s\",\n-\t\t\t\t\tpackdir, item->string, exts[ext]);\n+\t\t\t\t\tpackdir, item->string, exts[ext].name);\n \t\t\tfname_old = mkpathdup(\"%s-%s%s\",\n-\t\t\t\t\tpacktmp, item->string, exts[ext]);\n+\t\t\t\t\tpacktmp, item->string, exts[ext].name);\n \t\t\tif (!stat(fname_old, &statbuffer)) {\n \t\t\t\tstatbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);\n \t\t\t\tchmod(fname_old, statbuffer.st_mode);\n@@ -340,7 +345,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)\n \t\t\tfname = mkpath(\"%s/old-pack-%s%s\",\n \t\t\t\t\tpackdir,\n \t\t\t\t\titem->string,\n-\t\t\t\t\texts[ext]);\n+\t\t\t\t\texts[ext].name);\n \t\t\tif (remove_path(fname))\n \t\t\t\twarning(_(\"removing '%s' failed\"), fname);\n \t\t}\n-- \n1.8.5.1.399.g900e7cd\n"},{"id":"232322","messageId":"20131221140027.GQ21145@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20131221135651.GA20818@sigill.intra.peff.net","subject":"[PATCH v4 17/23] repack: handle optional files created by pack-objects","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-12-21T14:00:27Z","receivedAt":"2013-12-21T14:00:27Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"We ask pack-objects to pack to a set of temporary files, and\nthen rename them into place. Some files that pack-objects\ncreates may be optional (like a .bitmap file), in which case\nwe would not want to call rename(). We already call stat()\nand make the chmod optional if the file cannot be accessed.\nWe could simply skip the rename step in this case, but that\nwould be a minor regression in noticing problems with\nnon-optional files (like the .pack and .idx files).\n\nInstead, we can now annotate extensions as optional, and\nskip them if they don't exist (and otherwise rely on\nrename() to barf).\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\n builtin/repack.c | 9 +++++++--\n 1 file changed, 7 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/repack.c b/builtin/repack.c\nindex a176de2..8b7dfd0 100644\n--- a/builtin/repack.c\n+++ b/builtin/repack.c\n@@ -117,6 +117,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)\n {\n \tstruct {\n \t\tconst char *name;\n+\t\tunsigned optional:1;\n \t} exts[] = {\n \t\t{\".pack\"},\n \t\t{\".idx\"},\n@@ -323,6 +324,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)\n \t\tfor (ext = 0; ext < ARRAY_SIZE(exts); ext++) {\n \t\t\tchar *fname, *fname_old;\n \t\t\tstruct stat statbuffer;\n+\t\t\tint exists = 0;\n \t\t\tfname = mkpathdup(\"%s/pack-%s%s\",\n \t\t\t\t\tpackdir, item->string, exts[ext].name);\n \t\t\tfname_old = mkpathdup(\"%s-%s%s\",\n@@ -330,9 +332,12 @@ int cmd_repack(int argc, const char **argv, const char *prefix)\n \t\t\tif (!stat(fname_old, &statbuffer)) {\n \t\t\t\tstatbuffer.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);\n \t\t\t\tchmod(fname_old, statbuffer.st_mode);\n+\t\t\t\texists = 1;\n+\t\t\t}\n+\t\t\tif (exists || !exts[ext].optional) {\n+\t\t\t\tif (rename(fname_old, fname))\n+\t\t\t\t\tdie_errno(_(\"renaming '%s' failed\"), fname_old);\n \t\t\t}\n-\t\t\tif (rename(fname_old, fname))\n-\t\t\t\tdie_errno(_(\"renaming '%s' failed\"), fname_old);\n \t\t\tfree(fname);\n \t\t\tfree(fname_old);\n \t\t}\n-- \n1.8.5.1.399.g900e7cd\n"},{"id":"232321","messageId":"20131221140031.GR21145@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20131221135651.GA20818@sigill.intra.peff.net","subject":"[PATCH v4 18/23] repack: consider bitmaps when performing repacks","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-12-21T14:00:31Z","receivedAt":"2013-12-21T14:00:31Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"From: Vicent Marti <tanoku@gmail.com>\n\nSince `pack-objects` will write a `.bitmap` file next to the `.pack` and\n`.idx` files, this commit teaches `git-repack` to consider the new\nbitmap indexes (if they exist) when performing repack operations.\n\nThis implies moving old bitmap indexes out of the way if we are\nrepacking a repository that already has them, and moving the newly\ngenerated bitmap indexes into the `objects/pack` directory, next to\ntheir corresponding packfiles.\n\nSince `git repack` is now capable of handling these `.bitmap` files,\na normal `git gc` run on a repository that has `pack.writebitmaps` set\nto true in its config file will generate bitmap indexes as part of the\ngarbage collection process.\n\nAlternatively, `git repack` can be called with the `-b` switch to\nexplicitly generate bitmap indexes if you are experimenting\nand don't want them on all the time.\n\nSigned-off-by: Vicent Marti <tanoku@gmail.com>\nSigned-off-by: Jeff King <peff@peff.net>\n---\n Documentation/git-repack.txt | 9 ++++++++-\n builtin/repack.c             | 9 ++++++++-\n 2 files changed, 16 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/git-repack.txt b/Documentation/git-repack.txt\nindex 4c1aff6..dad186c 100644\n--- a/Documentation/git-repack.txt\n+++ b/Documentation/git-repack.txt\n@@ -9,7 +9,7 @@ git-repack - Pack unpacked objects in a repository\n SYNOPSIS\n --------\n [verse]\n-'git repack' [-a] [-A] [-d] [-f] [-F] [-l] [-n] [-q] [--window=<n>] [--depth=<n>]\n+'git repack' [-a] [-A] [-d] [-f] [-F] [-l] [-n] [-q] [-b] [--window=<n>] [--depth=<n>]\n \n DESCRIPTION\n -----------\n@@ -110,6 +110,13 @@ other objects in that pack they already have locally.\n \tThe default is unlimited, unless the config variable\n \t`pack.packSizeLimit` is set.\n \n+-b::\n+--write-bitmap-index::\n+\tWrite a reachability bitmap index as part of the repack. This\n+\tonly makes sense when used with `-a` or `-A`, as the bitmaps\n+\tmust be able to refer to all reachable objects. This option\n+\toverrides the setting of `pack.writebitmaps`.\n+\n \n Configuration\n -------------\ndiff --git a/builtin/repack.c b/builtin/repack.c\nindex 8b7dfd0..239f278 100644\n--- a/builtin/repack.c\n+++ b/builtin/repack.c\n@@ -94,7 +94,7 @@ static void get_non_kept_pack_filenames(struct string_list *fname_list)\n \n static void remove_redundant_pack(const char *dir_name, const char *base_name)\n {\n-\tconst char *exts[] = {\".pack\", \".idx\", \".keep\"};\n+\tconst char *exts[] = {\".pack\", \".idx\", \".keep\", \".bitmap\"};\n \tint i;\n \tstruct strbuf buf = STRBUF_INIT;\n \tsize_t plen;\n@@ -121,6 +121,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)\n \t} exts[] = {\n \t\t{\".pack\"},\n \t\t{\".idx\"},\n+\t\t{\".bitmap\", 1},\n \t};\n \tstruct child_process cmd;\n \tstruct string_list_item *item;\n@@ -143,6 +144,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)\n \tint no_update_server_info = 0;\n \tint quiet = 0;\n \tint local = 0;\n+\tint write_bitmap = -1;\n \n \tstruct option builtin_repack_options[] = {\n \t\tOPT_BIT('a', NULL, &pack_everything,\n@@ -161,6 +163,8 @@ int cmd_repack(int argc, const char **argv, const char *prefix)\n \t\tOPT__QUIET(&quiet, N_(\"be quiet\")),\n \t\tOPT_BOOL('l', \"local\", &local,\n \t\t\t\tN_(\"pass --local to git-pack-objects\")),\n+\t\tOPT_BOOL('b', \"write-bitmap-index\", &write_bitmap,\n+\t\t\t\tN_(\"write bitmap index\")),\n \t\tOPT_STRING(0, \"unpack-unreachable\", &unpack_unreachable, N_(\"approxidate\"),\n \t\t\t\tN_(\"with -A, do not loosen objects older than this\")),\n \t\tOPT_INTEGER(0, \"window\", &window,\n@@ -202,6 +206,9 @@ int cmd_repack(int argc, const char **argv, const char *prefix)\n \t\targv_array_pushf(&cmd_args, \"--no-reuse-delta\");\n \tif (no_reuse_object)\n \t\targv_array_pushf(&cmd_args, \"--no-reuse-object\");\n+\tif (write_bitmap >= 0)\n+\t\targv_array_pushf(&cmd_args, \"--%swrite-bitmap-index\",\n+\t\t\t\t write_bitmap ? \"\" : \"no-\");\n \n \tif (pack_everything & ALL_INTO_ONE) {\n \t\tget_non_kept_pack_filenames(&existing_packs);\n-- \n1.8.5.1.399.g900e7cd\n"},{"id":"232324","messageId":"20131221140034.GS21145@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20131221135651.GA20818@sigill.intra.peff.net","subject":"[PATCH v4 19/23] count-objects: recognize .bitmap in garbage-checking","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-12-21T14:00:34Z","receivedAt":"2013-12-21T14:00:34Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n\nCount-objects will report any \"garbage\" files in the packs\ndirectory, including files whose extensions it does not\nknow (case 1), and files whose matching \".pack\" file is\nmissing (case 2).  Without having learned about \".bitmap\"\nfiles, the current code reports all such files as garbage\n(case 1), even if their pack exists. Instead, they should be\ntreated as case 2.\n\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Jeff King <peff@peff.net>\n---\n sha1_file.c | 1 +\n 1 file changed, 1 insertion(+)\n\ndiff --git a/sha1_file.c b/sha1_file.c\nindex 4714bd8..1294962 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -1194,6 +1194,7 @@ static void prepare_packed_git_one(char *objdir, int local)\n \n \t\tif (has_extension(de->d_name, \".idx\") ||\n \t\t    has_extension(de->d_name, \".pack\") ||\n+\t\t    has_extension(de->d_name, \".bitmap\") ||\n \t\t    has_extension(de->d_name, \".keep\"))\n \t\t\tstring_list_append(&garbage, path);\n \t\telse\n-- \n1.8.5.1.399.g900e7cd\n"},{"id":"232325","messageId":"20131221140038.GT21145@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20131221135651.GA20818@sigill.intra.peff.net","subject":"[PATCH v4 20/23] t: add basic bitmap functionality tests","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-12-21T14:00:38Z","receivedAt":"2013-12-21T14:00:38Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"Now that we can read and write bitmaps, we can exercise them\nwith some basic functionality tests. These tests aren't\nparticularly useful for seeing the benefit, as the test\nrepo is too small for it to make a difference. However, we\ncan at least check that using bitmaps does not break anything.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\n t/t5310-pack-bitmaps.sh | 138 ++++++++++++++++++++++++++++++++++++++++++++++++\n 1 file changed, 138 insertions(+)\n create mode 100755 t/t5310-pack-bitmaps.sh\n\ndiff --git a/t/t5310-pack-bitmaps.sh b/t/t5310-pack-bitmaps.sh\nnew file mode 100755\nindex 0000000..d2b0c45\n--- /dev/null\n+++ b/t/t5310-pack-bitmaps.sh\n@@ -0,0 +1,138 @@\n+#!/bin/sh\n+\n+test_description='exercise basic bitmap functionality'\n+. ./test-lib.sh\n+\n+test_expect_success 'setup repo with moderate-sized history' '\n+\tfor i in $(test_seq 1 10); do\n+\t\ttest_commit $i\n+\tdone &&\n+\tgit checkout -b other HEAD~5 &&\n+\tfor i in $(test_seq 1 10); do\n+\t\ttest_commit side-$i\n+\tdone &&\n+\tgit checkout master &&\n+\tblob=$(echo tagged-blob | git hash-object -w --stdin) &&\n+\tgit tag tagged-blob $blob &&\n+\tgit config pack.writebitmaps true\n+'\n+\n+test_expect_success 'full repack creates bitmaps' '\n+\tgit repack -ad &&\n+\tls .git/objects/pack/ | grep bitmap >output &&\n+\ttest_line_count = 1 output\n+'\n+\n+test_expect_success 'rev-list --test-bitmap verifies bitmaps' '\n+\tgit rev-list --test-bitmap HEAD\n+'\n+\n+rev_list_tests() {\n+\tstate=$1\n+\n+\ttest_expect_success \"counting commits via bitmap ($state)\" '\n+\t\tgit rev-list --count HEAD >expect &&\n+\t\tgit rev-list --use-bitmap-index --count HEAD >actual &&\n+\t\ttest_cmp expect actual\n+\t'\n+\n+\ttest_expect_success \"counting partial commits via bitmap ($state)\" '\n+\t\tgit rev-list --count HEAD~5..HEAD >expect &&\n+\t\tgit rev-list --use-bitmap-index --count HEAD~5..HEAD >actual &&\n+\t\ttest_cmp expect actual\n+\t'\n+\n+\ttest_expect_success \"counting non-linear history ($state)\" '\n+\t\tgit rev-list --count other...master >expect &&\n+\t\tgit rev-list --use-bitmap-index --count other...master >actual &&\n+\t\ttest_cmp expect actual\n+\t'\n+\n+\ttest_expect_success \"enumerate --objects ($state)\" '\n+\t\tgit rev-list --objects --use-bitmap-index HEAD >tmp &&\n+\t\tcut -d\" \" -f1 <tmp >tmp2 &&\n+\t\tsort <tmp2 >actual &&\n+\t\tgit rev-list --objects HEAD >tmp &&\n+\t\tcut -d\" \" -f1 <tmp >tmp2 &&\n+\t\tsort <tmp2 >expect &&\n+\t\ttest_cmp expect actual\n+\t'\n+\n+\ttest_expect_success \"bitmap --objects handles non-commit objects ($state)\" '\n+\t\tgit rev-list --objects --use-bitmap-index HEAD tagged-blob >actual &&\n+\t\tgrep $blob actual\n+\t'\n+}\n+\n+rev_list_tests 'full bitmap'\n+\n+test_expect_success 'clone from bitmapped repository' '\n+\tgit clone --no-local --bare . clone.git &&\n+\tgit rev-parse HEAD >expect &&\n+\tgit --git-dir=clone.git rev-parse HEAD >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'setup further non-bitmapped commits' '\n+\tfor i in $(test_seq 1 10); do\n+\t\ttest_commit further-$i\n+\tdone\n+'\n+\n+rev_list_tests 'partial bitmap'\n+\n+test_expect_success 'fetch (partial bitmap)' '\n+\tgit --git-dir=clone.git fetch origin master:master &&\n+\tgit rev-parse HEAD >expect &&\n+\tgit --git-dir=clone.git rev-parse HEAD >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'incremental repack cannot create bitmaps' '\n+\ttest_commit more-1 &&\n+\ttest_must_fail git repack -d\n+'\n+\n+test_expect_success 'incremental repack can disable bitmaps' '\n+\ttest_commit more-2 &&\n+\tgit repack -d --no-write-bitmap-index\n+'\n+\n+test_expect_success 'full repack, reusing previous bitmaps' '\n+\tgit repack -ad &&\n+\tls .git/objects/pack/ | grep bitmap >output &&\n+\ttest_line_count = 1 output\n+'\n+\n+test_expect_success 'fetch (full bitmap)' '\n+\tgit --git-dir=clone.git fetch origin master:master &&\n+\tgit rev-parse HEAD >expect &&\n+\tgit --git-dir=clone.git rev-parse HEAD >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_lazy_prereq JGIT '\n+\ttype jgit\n+'\n+\n+test_expect_success JGIT 'we can read jgit bitmaps' '\n+\tgit clone . compat-jgit &&\n+\t(\n+\t\tcd compat-jgit &&\n+\t\trm -f .git/objects/pack/*.bitmap &&\n+\t\tjgit gc &&\n+\t\tgit rev-list --test-bitmap HEAD\n+\t)\n+'\n+\n+test_expect_success JGIT 'jgit can read our bitmaps' '\n+\tgit clone . compat-us &&\n+\t(\n+\t\tcd compat-us &&\n+\t\tgit repack -adb &&\n+\t\t# jgit gc will barf if it does not like our bitmaps\n+\t\tjgit gc\n+\t)\n+'\n+\n+test_done\n-- \n1.8.5.1.399.g900e7cd\n"},{"id":"232326","messageId":"20131221140042.GU21145@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20131221135651.GA20818@sigill.intra.peff.net","subject":"[PATCH v4 21/23] t/perf: add tests for pack bitmaps","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-12-21T14:00:42Z","receivedAt":"2013-12-21T14:00:42Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"This adds a few basic perf tests for the pack bitmap code to\nshow off its improvements. The tests are:\n\n  1. How long does it take to do a repack (it gets slower\n     with bitmaps, since we have to do extra work)?\n\n  2. How long does it take to do a clone (it gets faster\n     with bitmaps)?\n\n  3. How does a small fetch perform when we've just\n     repacked?\n\n  4. How does a clone perform when we haven't repacked since\n     a week of pushes?\n\nHere are results against linux.git:\n\nTest                      origin/master       this tree\n-----------------------------------------------------------------------\n5310.2: repack to disk    33.64(32.64+2.04)   67.67(66.75+1.84) +101.2%\n5310.3: simulated clone   30.49(29.47+2.05)   1.20(1.10+0.10) -96.1%\n5310.4: simulated fetch   3.49(6.79+0.06)     5.57(22.35+0.07) +59.6%\n5310.6: partial bitmap    36.70(43.87+1.81)   8.18(21.92+0.73) -77.7%\n\nYou can see that we do take longer to repack, but we do way\nbetter for further clones. A small fetch performs a bit\nworse, as we spend way more time on delta compression (note\nthe heavy user CPU time, as we have 8 threads) due to the\nlack of name hashes for the bitmapped objects.\n\nThe final test shows how the bitmaps degrade over time\nbetween packs. There's still a significant speedup over the\nnon-bitmap case, but we don't do quite as well (we have to\nspend time accessing the \"new\" objects the old fashioned\nway, including delta compression).\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\n t/perf/p5310-pack-bitmaps.sh | 56 ++++++++++++++++++++++++++++++++++++++++++++\n 1 file changed, 56 insertions(+)\n create mode 100755 t/perf/p5310-pack-bitmaps.sh\n\ndiff --git a/t/perf/p5310-pack-bitmaps.sh b/t/perf/p5310-pack-bitmaps.sh\nnew file mode 100755\nindex 0000000..8c6ae45\n--- /dev/null\n+++ b/t/perf/p5310-pack-bitmaps.sh\n@@ -0,0 +1,56 @@\n+#!/bin/sh\n+\n+test_description='Tests pack performance using bitmaps'\n+. ./perf-lib.sh\n+\n+test_perf_large_repo\n+\n+# note that we do everything through config,\n+# since we want to be able to compare bitmap-aware\n+# git versus non-bitmap git\n+test_expect_success 'setup bitmap config' '\n+\tgit config pack.writebitmaps true\n+'\n+\n+test_perf 'repack to disk' '\n+\tgit repack -ad\n+'\n+\n+test_perf 'simulated clone' '\n+\tgit pack-objects --stdout --all </dev/null >/dev/null\n+'\n+\n+test_perf 'simulated fetch' '\n+\thave=$(git rev-list HEAD~100 -1) &&\n+\t{\n+\t\techo HEAD &&\n+\t\techo ^$have\n+\t} | git pack-objects --revs --stdout >/dev/null\n+'\n+\n+test_expect_success 'create partial bitmap state' '\n+\t# pick a commit to represent the repo tip in the past\n+\tcutoff=$(git rev-list HEAD~100 -1) &&\n+\torig_tip=$(git rev-parse HEAD) &&\n+\n+\t# now kill off all of the refs and pretend we had\n+\t# just the one tip\n+\trm -rf .git/logs .git/refs/* .git/packed-refs\n+\tgit update-ref HEAD $cutoff\n+\n+\t# and then repack, which will leave us with a nice\n+\t# big bitmap pack of the \"old\" history, and all of\n+\t# the new history will be loose, as if it had been pushed\n+\t# up incrementally and exploded via unpack-objects\n+\tgit repack -Ad\n+\n+\t# and now restore our original tip, as if the pushes\n+\t# had happened\n+\tgit update-ref HEAD $orig_tip\n+'\n+\n+test_perf 'partial bitmap' '\n+\tgit pack-objects --stdout --all </dev/null >/dev/null\n+'\n+\n+test_done\n-- \n1.8.5.1.399.g900e7cd\n"},{"id":"232327","messageId":"20131221140045.GV21145@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20131221135651.GA20818@sigill.intra.peff.net","subject":"[PATCH v4 22/23] pack-bitmap: implement optional name_hash cache","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-12-21T14:00:45Z","receivedAt":"2013-12-21T14:00:45Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"From: Vicent Marti <tanoku@gmail.com>\n\nWhen we use pack bitmaps rather than walking the object\ngraph, we end up with the list of objects to include in the\npackfile, but we do not know the path at which any tree or\nblob objects would be found.\n\nIn a recently packed repository, this is fine. A fetch would\nuse the paths only as a heuristic in the delta compression\nphase, and a fully packed repository should not need to do\nmuch delta compression.\n\nAs time passes, though, we may acquire more objects on top\nof our large bitmapped pack. If clients fetch frequently,\nthen they never even look at the bitmapped history, and all\nworks as usual. However, a client who has not fetched since\nthe last bitmap repack will have \"have\" tips in the\nbitmapped history, but \"want\" newer objects.\n\nThe bitmaps themselves degrade gracefully in this\ncircumstance. We manually walk the more recent bits of\nhistory, and then use bitmaps when we hit them.\n\nBut we would also like to perform delta compression between\nthe newer objects and the bitmapped objects (both to delta\nagainst what we know the user already has, but also between\n\"new\" and \"old\" objects that the user is fetching). The lack\nof pathnames makes our delta heuristics much less effective.\n\nThis patch adds an optional cache of the 32-bit name_hash\nvalues to the end of the bitmap file. If present, a reader\ncan use it to match bitmapped and non-bitmapped names during\ndelta compression.\n\nHere are perf results for p5310:\n\nTest                      origin/master       HEAD^                      HEAD\n-------------------------------------------------------------------------------------------------\n5310.2: repack to disk    36.81(37.82+1.43)   47.70(48.74+1.41) +29.6%   47.75(48.70+1.51) +29.7%\n5310.3: simulated clone   30.78(29.70+2.14)   1.08(0.97+0.10) -96.5%     1.07(0.94+0.12) -96.5%\n5310.4: simulated fetch   3.16(6.10+0.08)     3.54(10.65+0.06) +12.0%    1.70(3.07+0.06) -46.2%\n5310.6: partial bitmap    36.76(43.19+1.81)   6.71(11.25+0.76) -81.7%    4.08(6.26+0.46) -88.9%\n\nYou can see that the time spent on an incremental fetch goes\ndown, as our delta heuristics are able to do their work.\nAnd we save time on the partial bitmap clone for the same\nreason.\n\nSigned-off-by: Vicent Marti <tanoku@gmail.com>\nSigned-off-by: Jeff King <peff@peff.net>\n---\n Documentation/config.txt                  | 11 +++++++++++\n Documentation/technical/bitmap-format.txt | 33 +++++++++++++++++++++++++++++++\n builtin/pack-objects.c                    | 10 +++++++++-\n pack-bitmap-write.c                       | 21 ++++++++++++++++++--\n pack-bitmap.c                             | 11 +++++++++++\n pack-bitmap.h                             |  6 ++++--\n t/perf/p5310-pack-bitmaps.sh              |  3 ++-\n t/t5310-pack-bitmaps.sh                   |  3 ++-\n 8 files changed, 91 insertions(+), 7 deletions(-)\n\ndiff --git a/Documentation/config.txt b/Documentation/config.txt\nindex 4b0c368..499a3c4 100644\n--- a/Documentation/config.txt\n+++ b/Documentation/config.txt\n@@ -1872,6 +1872,17 @@ pack.writebitmaps::\n \tspace and extra time spent on the initial repack.  Defaults to\n \tfalse.\n \n+pack.writeBitmapHashCache::\n+\tWhen true, git will include a \"hash cache\" section in the bitmap\n+\tindex (if one is written). This cache can be used to feed git's\n+\tdelta heuristics, potentially leading to better deltas between\n+\tbitmapped and non-bitmapped objects (e.g., when serving a fetch\n+\tbetween an older, bitmapped pack and objects that have been\n+\tpushed since the last gc). The downside is that it consumes 4\n+\tbytes per object of disk space, and that JGit's bitmap\n+\timplementation does not understand it, causing it to complain if\n+\tGit and JGit are used on the same repository. Defaults to false.\n+\n pager.<cmd>::\n \tIf the value is boolean, turns on or off pagination of the\n \toutput of a particular Git subcommand when writing to a tty.\ndiff --git a/Documentation/technical/bitmap-format.txt b/Documentation/technical/bitmap-format.txt\nindex 7a86bd7..f8c18a0 100644\n--- a/Documentation/technical/bitmap-format.txt\n+++ b/Documentation/technical/bitmap-format.txt\n@@ -21,6 +21,12 @@ GIT bitmap v1 format\n \t\t\trequirement for the bitmap index format, also present in JGit,\n \t\t\tthat greatly reduces the complexity of the implementation.\n \n+\t\t\t- BITMAP_OPT_HASH_CACHE (0x4)\n+\t\t\tIf present, the end of the bitmap file contains\n+\t\t\t`N` 32-bit name-hash values, one per object in the\n+\t\t\tpack. The format and meaning of the name-hash is\n+\t\t\tdescribed below.\n+\n \t\t4-byte entry count (network byte order)\n \n \t\t\tThe total count of entries (bitmapped commits) in this bitmap index.\n@@ -129,3 +135,30 @@ The bitstream represented by the above chunk is then:\n The next word after `L_M` (if any) must again be a RLW, for the next\n chunk.  For efficient appending to the bitstream, the EWAH stores a\n pointer to the last RLW in the stream.\n+\n+\n+== Appendix B: Optional Bitmap Sections\n+\n+These sections may or may not be present in the `.bitmap` file; their\n+presence is indicated by the header flags section described above.\n+\n+Name-hash cache\n+---------------\n+\n+If the BITMAP_OPT_HASH_CACHE flag is set, the end of the bitmap contains\n+a cache of 32-bit values, one per object in the pack. The value at\n+position `i` is the hash of the pathname at which the `i`th object\n+(counting in index order) in the pack can be found.  This can be fed\n+into the delta heuristics to compare objects with similar pathnames.\n+\n+The hash algorithm used is:\n+\n+    hash = 0;\n+    while ((c = *name++))\n+\t    if (!isspace(c))\n+\t\t    hash = (hash >> 2) + (c << 24);\n+\n+Note that this hashing scheme is tied to the BITMAP_OPT_HASH_CACHE flag.\n+If implementations want to choose a different hashing scheme, they are\n+free to do so, but MUST allocate a new header flag (because comparing\n+hashes made under two different schemes would be pointless).\ndiff --git a/builtin/pack-objects.c b/builtin/pack-objects.c\nindex fd6ae01..fd74197 100644\n--- a/builtin/pack-objects.c\n+++ b/builtin/pack-objects.c\n@@ -64,6 +64,7 @@ static off_t reuse_packfile_offset;\n \n static int use_bitmap_index = 1;\n static int write_bitmap_index;\n+static uint16_t write_bitmap_options;\n \n static unsigned long delta_cache_size = 0;\n static unsigned long max_delta_cache_size = 256 * 1024 * 1024;\n@@ -851,7 +852,8 @@ static void write_pack_file(void)\n \t\t\t\tbitmap_writer_reuse_bitmaps(&to_pack);\n \t\t\t\tbitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1);\n \t\t\t\tbitmap_writer_build(&to_pack);\n-\t\t\t\tbitmap_writer_finish(written_list, nr_written, tmpname);\n+\t\t\t\tbitmap_writer_finish(written_list, nr_written,\n+\t\t\t\t\t\t     tmpname, write_bitmap_options);\n \t\t\t\twrite_bitmap_index = 0;\n \t\t\t}\n \n@@ -2201,6 +2203,12 @@ static int git_pack_config(const char *k, const char *v, void *cb)\n \t\twrite_bitmap_index = git_config_bool(k, v);\n \t\treturn 0;\n \t}\n+\tif (!strcmp(k, \"pack.writebitmaphashcache\")) {\n+\t\tif (git_config_bool(k, v))\n+\t\t\twrite_bitmap_options |= BITMAP_OPT_HASH_CACHE;\n+\t\telse\n+\t\t\twrite_bitmap_options &= ~BITMAP_OPT_HASH_CACHE;\n+\t}\n \tif (!strcmp(k, \"pack.usebitmaps\")) {\n \t\tuse_bitmap_index = git_config_bool(k, v);\n \t\treturn 0;\ndiff --git a/pack-bitmap-write.c b/pack-bitmap-write.c\nindex 954a74d..1218bef 100644\n--- a/pack-bitmap-write.c\n+++ b/pack-bitmap-write.c\n@@ -490,6 +490,19 @@ static void write_selected_commits_v1(struct sha1file *f,\n \t}\n }\n \n+static void write_hash_cache(struct sha1file *f,\n+\t\t\t     struct pack_idx_entry **index,\n+\t\t\t     uint32_t index_nr)\n+{\n+\tuint32_t i;\n+\n+\tfor (i = 0; i < index_nr; ++i) {\n+\t\tstruct object_entry *entry = (struct object_entry *)index[i];\n+\t\tuint32_t hash_value = htonl(entry->hash);\n+\t\tsha1write(f, &hash_value, sizeof(hash_value));\n+\t}\n+}\n+\n void bitmap_writer_set_checksum(unsigned char *sha1)\n {\n \thashcpy(writer.pack_checksum, sha1);\n@@ -497,7 +510,8 @@ void bitmap_writer_set_checksum(unsigned char *sha1)\n \n void bitmap_writer_finish(struct pack_idx_entry **index,\n \t\t\t  uint32_t index_nr,\n-\t\t\t  const char *filename)\n+\t\t\t  const char *filename,\n+\t\t\t  uint16_t options)\n {\n \tstatic char tmp_file[PATH_MAX];\n \tstatic uint16_t default_version = 1;\n@@ -514,7 +528,7 @@ void bitmap_writer_finish(struct pack_idx_entry **index,\n \n \tmemcpy(header.magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE));\n \theader.version = htons(default_version);\n-\theader.options = htons(flags);\n+\theader.options = htons(flags | options);\n \theader.entry_count = htonl(writer.selected_nr);\n \tmemcpy(header.checksum, writer.pack_checksum, 20);\n \n@@ -525,6 +539,9 @@ void bitmap_writer_finish(struct pack_idx_entry **index,\n \tdump_bitmap(f, writer.tags);\n \twrite_selected_commits_v1(f, index, index_nr);\n \n+\tif (options & BITMAP_OPT_HASH_CACHE)\n+\t\twrite_hash_cache(f, index, index_nr);\n+\n \tsha1close(f, NULL, CSUM_FSYNC);\n \n \tif (adjust_shared_perm(tmp_file))\ndiff --git a/pack-bitmap.c b/pack-bitmap.c\nindex 82090a6..ae0b57b 100644\n--- a/pack-bitmap.c\n+++ b/pack-bitmap.c\n@@ -66,6 +66,9 @@ static struct bitmap_index {\n \t/* Number of bitmapped commits */\n \tuint32_t entry_count;\n \n+\t/* Name-hash cache (or NULL if not present). */\n+\tuint32_t *hashes;\n+\n \t/*\n \t * Extended index.\n \t *\n@@ -152,6 +155,11 @@ static int load_bitmap_header(struct bitmap_index *index)\n \t\tif ((flags & BITMAP_OPT_FULL_DAG) == 0)\n \t\t\treturn error(\"Unsupported options for bitmap index file \"\n \t\t\t\t\"(Git requires BITMAP_OPT_FULL_DAG)\");\n+\n+\t\tif (flags & BITMAP_OPT_HASH_CACHE) {\n+\t\t\tunsigned char *end = index->map + index->map_size - 20;\n+\t\t\tindex->hashes = ((uint32_t *)end) - index->pack->num_objects;\n+\t\t}\n \t}\n \n \tindex->entry_count = ntohl(header->entry_count);\n@@ -626,6 +634,9 @@ static void show_objects_for_type(\n \t\t\tentry = &bitmap_git.reverse_index->revindex[pos + offset];\n \t\t\tsha1 = nth_packed_object_sha1(bitmap_git.pack, entry->nr);\n \n+\t\t\tif (bitmap_git.hashes)\n+\t\t\t\thash = ntohl(bitmap_git.hashes[entry->nr]);\n+\n \t\t\tshow_reach(sha1, object_type, 0, hash, bitmap_git.pack, entry->offset);\n \t\t}\n \ndiff --git a/pack-bitmap.h b/pack-bitmap.h\nindex 09acf02..8b7f4e9 100644\n--- a/pack-bitmap.h\n+++ b/pack-bitmap.h\n@@ -24,7 +24,8 @@ static const char BITMAP_IDX_SIGNATURE[] = {'B', 'I', 'T', 'M'};\n #define NEEDS_BITMAP (1u<<22)\n \n enum pack_bitmap_opts {\n-\tBITMAP_OPT_FULL_DAG = 1\n+\tBITMAP_OPT_FULL_DAG = 1,\n+\tBITMAP_OPT_HASH_CACHE = 4,\n };\n \n enum pack_bitmap_flags {\n@@ -57,6 +58,7 @@ void bitmap_writer_select_commits(struct commit **indexed_commits,\n void bitmap_writer_build(struct packing_data *to_pack);\n void bitmap_writer_finish(struct pack_idx_entry **index,\n \t\t\t  uint32_t index_nr,\n-\t\t\t  const char *filename);\n+\t\t\t  const char *filename,\n+\t\t\t  uint16_t options);\n \n #endif\ndiff --git a/t/perf/p5310-pack-bitmaps.sh b/t/perf/p5310-pack-bitmaps.sh\nindex 8c6ae45..685d46f 100755\n--- a/t/perf/p5310-pack-bitmaps.sh\n+++ b/t/perf/p5310-pack-bitmaps.sh\n@@ -9,7 +9,8 @@ test_perf_large_repo\n # since we want to be able to compare bitmap-aware\n # git versus non-bitmap git\n test_expect_success 'setup bitmap config' '\n-\tgit config pack.writebitmaps true\n+\tgit config pack.writebitmaps true &&\n+\tgit config pack.writebitmaphashcache true\n '\n \n test_perf 'repack to disk' '\ndiff --git a/t/t5310-pack-bitmaps.sh b/t/t5310-pack-bitmaps.sh\nindex d2b0c45..d3a3afa 100755\n--- a/t/t5310-pack-bitmaps.sh\n+++ b/t/t5310-pack-bitmaps.sh\n@@ -14,7 +14,8 @@ test_expect_success 'setup repo with moderate-sized history' '\n \tgit checkout master &&\n \tblob=$(echo tagged-blob | git hash-object -w --stdin) &&\n \tgit tag tagged-blob $blob &&\n-\tgit config pack.writebitmaps true\n+\tgit config pack.writebitmaps true &&\n+\tgit config pack.writebitmaphashcache true\n '\n \n test_expect_success 'full repack creates bitmaps' '\n-- \n1.8.5.1.399.g900e7cd\n"},{"id":"232323","messageId":"20131221140052.GW21145@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20131221135651.GA20818@sigill.intra.peff.net","subject":"[PATCH v4 23/23] compat/mingw.h: Fix the MinGW and msvc builds","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-12-21T14:00:52Z","receivedAt":"2013-12-21T14:00:52Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"From: Ramsay Jones <ramsay@ramsay1.demon.co.uk>\n\nSigned-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Jeff King <peff@peff.net>\n---\n compat/mingw.h | 1 +\n 1 file changed, 1 insertion(+)\n\ndiff --git a/compat/mingw.h b/compat/mingw.h\nindex 92cd728..8828ede 100644\n--- a/compat/mingw.h\n+++ b/compat/mingw.h\n@@ -345,6 +345,7 @@ static inline char *mingw_find_last_dir_sep(const char *path)\n #define PATH_SEP ';'\n #define PRIuMAX \"I64u\"\n #define PRId64 \"I64d\"\n+#define PRIx64 \"I64x\"\n \n void mingw_open_html(const char *path);\n #define open_html mingw_open_html\n-- \n1.8.5.1.399.g900e7cd\n"},{"id":"232329","messageId":"20131221140346.GA21359@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20131221135651.GA20818@sigill.intra.peff.net","subject":"Re: [PATCH v4 0/22] pack bitmaps","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-12-21T14:03:47Z","receivedAt":"2013-12-21T14:03:47Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Sat, Dec 21, 2013 at 08:56:51AM -0500, Jeff King wrote:\n\n> Interdiff is below.\n> \n>   [01/23]: sha1write: make buffer const-correct\n>   [02/23]: revindex: Export new APIs\n>   [03/23]: pack-objects: Refactor the packing list\n>   [04/23]: pack-objects: factor out name_hash\n>   [05/23]: revision: allow setting custom limiter function\n>   [06/23]: sha1_file: export `git_open_noatime`\n>   [07/23]: compat: add endianness helpers\n>   [08/23]: ewah: compressed bitmap implementation\n>   [09/23]: documentation: add documentation for the bitmap format\n\nBy the way, the patches are identical up through 09/23. I think the\nfirst one is already merged into another topic, too, so it may be worth\nbuilding on that instead of re-applying.\n\n-Peff\n"},{"id":"232330","messageId":"20131221140544.GB21359@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20131221135651.GA20818@sigill.intra.peff.net","subject":"Re: [PATCH v4 0/22] pack bitmaps","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-12-21T14:05:44Z","receivedAt":"2013-12-21T14:05:44Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Sat, Dec 21, 2013 at 08:56:51AM -0500, Jeff King wrote:\n\n> The changes from v3 are:\n> \n>  - reworked add_object_entry refactoring (see patch 11, which is new,\n>    and patch 12 which builds on it in a more natural way)\n> \n>  - better error/die reporting from write_reused_pack\n> \n>  - added Ramsay's PRIx64 compat fix\n> \n>  - fixed a user-after-free in the warning message of open_pack_bitmap_1\n> \n>  - minor typo/thinko fixes from Thomas in docs and tests\n\nOne thing explicitly _not_ here is ripping out khash in favor of\nKarsten's hash system. That is still on the table, but I'd much rather\ndo it on top if we are going to.\n\n-Peff\n"},{"id":"232337","messageId":"87d2kqm4rw.fsf@thomasrast.ch","threadId":"35562","inReplyTo":"20131221135651.GA20818@sigill.intra.peff.net","subject":"Re: [PATCH v4 0/22] pack bitmaps","fromName":"Thomas Rast","fromEmail":"tr@thomasrast.ch","sentAt":"2013-12-21T18:34:59Z","receivedAt":"2013-12-21T18:34:59Z","isPatch":true,"sender":{"key":"tr@thomasrast.ch","avatar":"https://avatars.githubusercontent.com/u/153510?v=4"},"body":"Jeff King <peff@peff.net> writes:\n\n> Here's the v4 re-roll of the pack bitmap series.\n>\n> The changes from v3 are:\n>\n>  - reworked add_object_entry refactoring (see patch 11, which is new,\n>    and patch 12 which builds on it in a more natural way)\n\nThis now looks like this (pasting because it is hard to see in the diffs):\n\n  static int add_object_entry(const unsigned char *sha1, enum object_type type,\n                              const char *name, int exclude)\n  {\n          struct packed_git *found_pack;\n          off_t found_offset;\n          uint32_t index_pos;\n\n          if (have_duplicate_entry(sha1, exclude, &index_pos))\n                  return 0;\n\n          if (!want_object_in_pack(sha1, exclude, &found_pack, &found_offset))\n                  return 0;\n\n          create_object_entry(sha1, type, pack_name_hash(name),\n                              exclude, name && no_try_delta(name),\n                              index_pos, found_pack, found_offset);\n\n          display_progress(progress_state, to_pack.nr_objects);\n          return 1;\n  }\n\n  static int add_object_entry_from_bitmap(const unsigned char *sha1,\n                                          enum object_type type,\n                                          int flags, uint32_t name_hash,\n                                          struct packed_git *pack, off_t offset)\n  {\n          uint32_t index_pos;\n\n          if (have_duplicate_entry(sha1, 0, &index_pos))\n                  return 0;\n\n          create_object_entry(sha1, type, name_hash, 0, 0, index_pos, pack, offset);\n\n          display_progress(progress_state, to_pack.nr_objects);\n          return 1;\n  }\n\n\nMuch nicer.  Thanks for going the extra mile!\n\n-- \nThomas Rast\ntr@thomasrast.ch\n"},{"id":"232350","messageId":"CAP8UFD3fnkGZPd_m42om-divjDAihZxcDuUR5nCQzCgGe2fPDQ@mail.gmail.com","threadId":"35562","inReplyTo":"20131221135926.GA21145@sigill.intra.peff.net","subject":"Re: [PATCH v4 01/23] sha1write: make buffer const-correct","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2013-12-22T09:06:36Z","receivedAt":"2013-12-22T09:06:36Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Sat, Dec 21, 2013 at 2:59 PM, Jeff King <peff@peff.net> wrote:\n> We are passed a \"void *\" and write it out without ever\n\ns/are passed/pass/\n\nCheers,\nChristian.\n"},{"id":"232388","messageId":"CABPQNSa+mtVoMiN_mxVfYW_=JMxO-0Odv5uLnGhknNhDq1yWrw@mail.gmail.com","threadId":"35562","inReplyTo":"20131221140052.GW21145@sigill.intra.peff.net","subject":"Re: [PATCH v4 23/23] compat/mingw.h: Fix the MinGW and msvc builds","fromName":"Erik Faye-Lund","fromEmail":"kusmabite@gmail.com","sentAt":"2013-12-25T22:08:57Z","receivedAt":"2013-12-25T22:08:57Z","isPatch":true,"sender":{"key":"kusmabite@gmail.com","avatar":"https://avatars.githubusercontent.com/u/47073?v=4"},"body":"On Sat, Dec 21, 2013 at 3:00 PM, Jeff King <peff@peff.net> wrote:\n> From: Ramsay Jones <ramsay@ramsay1.demon.co.uk>\n>\n> Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>\n> Signed-off-by: Junio C Hamano <gitster@pobox.com>\n> Signed-off-by: Jeff King <peff@peff.net>\n> ---\n>  compat/mingw.h | 1 +\n>  1 file changed, 1 insertion(+)\n>\n> diff --git a/compat/mingw.h b/compat/mingw.h\n> index 92cd728..8828ede 100644\n> --- a/compat/mingw.h\n> +++ b/compat/mingw.h\n> @@ -345,6 +345,7 @@ static inline char *mingw_find_last_dir_sep(const char *path)\n>  #define PATH_SEP ';'\n>  #define PRIuMAX \"I64u\"\n>  #define PRId64 \"I64d\"\n> +#define PRIx64 \"I64x\"\n>\n\nPlease, move this before patch #8, and adjust the commit message.\n"},{"id":"232472","messageId":"20131228100050.GA24929@sigill.intra.peff.net","threadId":"35562","inReplyTo":"CABPQNSa+mtVoMiN_mxVfYW_=JMxO-0Odv5uLnGhknNhDq1yWrw@mail.gmail.com","subject":"Re: [PATCH v4 23/23] compat/mingw.h: Fix the MinGW and msvc builds","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2013-12-28T10:00:50Z","receivedAt":"2013-12-28T10:00:50Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Wed, Dec 25, 2013 at 11:08:57PM +0100, Erik Faye-Lund wrote:\n\n> On Sat, Dec 21, 2013 at 3:00 PM, Jeff King <peff@peff.net> wrote:\n> > From: Ramsay Jones <ramsay@ramsay1.demon.co.uk>\n> >\n> > Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>\n> > Signed-off-by: Junio C Hamano <gitster@pobox.com>\n> > Signed-off-by: Jeff King <peff@peff.net>\n> > ---\n> >  compat/mingw.h | 1 +\n> >  1 file changed, 1 insertion(+)\n> >\n> > diff --git a/compat/mingw.h b/compat/mingw.h\n> > index 92cd728..8828ede 100644\n> > --- a/compat/mingw.h\n> > +++ b/compat/mingw.h\n> > @@ -345,6 +345,7 @@ static inline char *mingw_find_last_dir_sep(const char *path)\n> >  #define PATH_SEP ';'\n> >  #define PRIuMAX \"I64u\"\n> >  #define PRId64 \"I64d\"\n> > +#define PRIx64 \"I64x\"\n> >\n> \n> Please, move this before patch #8, and adjust the commit message.\n\nYeah, that makes sense. Though I think we can do one better and simply\nremove the need for it entirely. The only use of PRIx64 is in a\ndebugging function that does not get called.\n\nHow about squashing the patch below into patch 8 (\"ewah: compressed\nbitmap implementation\"):\n\ndiff --git a/ewah/ewah_bitmap.c b/ewah/ewah_bitmap.c\nindex f104b87..9ced2da 100644\n--- a/ewah/ewah_bitmap.c\n+++ b/ewah/ewah_bitmap.c\n@@ -381,18 +381,6 @@ void ewah_iterator_init(struct ewah_iterator *it, struct ewah_bitmap *parent)\n \t\tread_new_rlw(it);\n }\n \n-void ewah_dump(struct ewah_bitmap *self)\n-{\n-\tsize_t i;\n-\tfprintf(stderr, \"%\"PRIuMAX\" bits | %\"PRIuMAX\" words | \",\n-\t\t(uintmax_t)self->bit_size, (uintmax_t)self->buffer_size);\n-\n-\tfor (i = 0; i < self->buffer_size; ++i)\n-\t\tfprintf(stderr, \"%016\"PRIx64\" \", (uint64_t)self->buffer[i]);\n-\n-\tfprintf(stderr, \"\\n\");\n-}\n-\n void ewah_not(struct ewah_bitmap *self)\n {\n \tsize_t pointer = 0;\ndiff --git a/ewah/ewok.h b/ewah/ewok.h\nindex 619afaa..43adeb5 100644\n--- a/ewah/ewok.h\n+++ b/ewah/ewok.h\n@@ -193,8 +193,6 @@ void ewah_and(\n \tstruct ewah_bitmap *ewah_j,\n \tstruct ewah_bitmap *out);\n \n-void ewah_dump(struct ewah_bitmap *self);\n-\n /**\n  * Direct word access\n  */\n"},{"id":"232473","messageId":"CAFFjANQpdh5Ti2JCKD1Q-gZQFUjzX5y=Nhn1t5uieTg=xSzGwQ@mail.gmail.com","threadId":"35562","inReplyTo":"20131228100050.GA24929@sigill.intra.peff.net","subject":"Re: [PATCH v4 23/23] compat/mingw.h: Fix the MinGW and msvc builds","fromName":"Vicent Martí","fromEmail":"tanoku@gmail.com","sentAt":"2013-12-28T10:06:41Z","receivedAt":"2013-12-28T10:06:41Z","isPatch":true,"sender":{"key":"tanoku@gmail.com","avatar":"https://gravatar.com/avatar/271386991cb4c2b8f1e1ed1d059f3422cc3485de7a598f65043f70be021d095b?d=mp&s=160"},"body":"Sounds good. We don't really need the dump anyway.\n\nOn Sat, Dec 28, 2013 at 11:00 AM, Jeff King <peff@peff.net> wrote:\n> On Wed, Dec 25, 2013 at 11:08:57PM +0100, Erik Faye-Lund wrote:\n>\n>> On Sat, Dec 21, 2013 at 3:00 PM, Jeff King <peff@peff.net> wrote:\n>> > From: Ramsay Jones <ramsay@ramsay1.demon.co.uk>\n>> >\n>> > Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>\n>> > Signed-off-by: Junio C Hamano <gitster@pobox.com>\n>> > Signed-off-by: Jeff King <peff@peff.net>\n>> > ---\n>> >  compat/mingw.h | 1 +\n>> >  1 file changed, 1 insertion(+)\n>> >\n>> > diff --git a/compat/mingw.h b/compat/mingw.h\n>> > index 92cd728..8828ede 100644\n>> > --- a/compat/mingw.h\n>> > +++ b/compat/mingw.h\n>> > @@ -345,6 +345,7 @@ static inline char *mingw_find_last_dir_sep(const char *path)\n>> >  #define PATH_SEP ';'\n>> >  #define PRIuMAX \"I64u\"\n>> >  #define PRId64 \"I64d\"\n>> > +#define PRIx64 \"I64x\"\n>> >\n>>\n>> Please, move this before patch #8, and adjust the commit message.\n>\n> Yeah, that makes sense. Though I think we can do one better and simply\n> remove the need for it entirely. The only use of PRIx64 is in a\n> debugging function that does not get called.\n>\n> How about squashing the patch below into patch 8 (\"ewah: compressed\n> bitmap implementation\"):\n>\n> diff --git a/ewah/ewah_bitmap.c b/ewah/ewah_bitmap.c\n> index f104b87..9ced2da 100644\n> --- a/ewah/ewah_bitmap.c\n> +++ b/ewah/ewah_bitmap.c\n> @@ -381,18 +381,6 @@ void ewah_iterator_init(struct ewah_iterator *it, struct ewah_bitmap *parent)\n>                 read_new_rlw(it);\n>  }\n>\n> -void ewah_dump(struct ewah_bitmap *self)\n> -{\n> -       size_t i;\n> -       fprintf(stderr, \"%\"PRIuMAX\" bits | %\"PRIuMAX\" words | \",\n> -               (uintmax_t)self->bit_size, (uintmax_t)self->buffer_size);\n> -\n> -       for (i = 0; i < self->buffer_size; ++i)\n> -               fprintf(stderr, \"%016\"PRIx64\" \", (uint64_t)self->buffer[i]);\n> -\n> -       fprintf(stderr, \"\\n\");\n> -}\n> -\n>  void ewah_not(struct ewah_bitmap *self)\n>  {\n>         size_t pointer = 0;\n> diff --git a/ewah/ewok.h b/ewah/ewok.h\n> index 619afaa..43adeb5 100644\n> --- a/ewah/ewok.h\n> +++ b/ewah/ewok.h\n> @@ -193,8 +193,6 @@ void ewah_and(\n>         struct ewah_bitmap *ewah_j,\n>         struct ewah_bitmap *out);\n>\n> -void ewah_dump(struct ewah_bitmap *self);\n> -\n>  /**\n>   * Direct word access\n>   */\n> --\n> To unsubscribe from this list: send the line \"unsubscribe git\" in\n> the body of a message to majordomo@vger.kernel.org\n> More majordomo info at  http://vger.kernel.org/majordomo-info.html\n"},{"id":"232490","messageId":"52BEF50F.6010803@ramsay1.demon.co.uk","threadId":"35562","inReplyTo":"20131228100050.GA24929@sigill.intra.peff.net","subject":"Re: [PATCH v4 23/23] compat/mingw.h: Fix the MinGW and msvc builds","fromName":"Ramsay Jones","fromEmail":"ramsay@ramsay1.demon.co.uk","sentAt":"2013-12-28T15:58:07Z","receivedAt":"2013-12-28T15:58:07Z","isPatch":true,"sender":{"key":"ramsay@ramsayjones.plus.com","avatar":"https://avatars.githubusercontent.com/u/33702710?v=4"},"body":"On 28/12/13 10:00, Jeff King wrote:\n> On Wed, Dec 25, 2013 at 11:08:57PM +0100, Erik Faye-Lund wrote:\n> \n>> On Sat, Dec 21, 2013 at 3:00 PM, Jeff King <peff@peff.net> wrote:\n>>> From: Ramsay Jones <ramsay@ramsay1.demon.co.uk>\n>>>\n>>> Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>\n>>> Signed-off-by: Junio C Hamano <gitster@pobox.com>\n>>> Signed-off-by: Jeff King <peff@peff.net>\n>>> ---\n>>>  compat/mingw.h | 1 +\n>>>  1 file changed, 1 insertion(+)\n>>>\n>>> diff --git a/compat/mingw.h b/compat/mingw.h\n>>> index 92cd728..8828ede 100644\n>>> --- a/compat/mingw.h\n>>> +++ b/compat/mingw.h\n>>> @@ -345,6 +345,7 @@ static inline char *mingw_find_last_dir_sep(const char *path)\n>>>  #define PATH_SEP ';'\n>>>  #define PRIuMAX \"I64u\"\n>>>  #define PRId64 \"I64d\"\n>>> +#define PRIx64 \"I64x\"\n>>>\n>>\n>> Please, move this before patch #8, and adjust the commit message.\n> \n> Yeah, that makes sense. Though I think we can do one better and simply\n> remove the need for it entirely. The only use of PRIx64 is in a\n> debugging function that does not get called.\n> \n> How about squashing the patch below into patch 8 (\"ewah: compressed\n> bitmap implementation\"):\n> \n> diff --git a/ewah/ewah_bitmap.c b/ewah/ewah_bitmap.c\n> index f104b87..9ced2da 100644\n> --- a/ewah/ewah_bitmap.c\n> +++ b/ewah/ewah_bitmap.c\n> @@ -381,18 +381,6 @@ void ewah_iterator_init(struct ewah_iterator *it, struct ewah_bitmap *parent)\n>  \t\tread_new_rlw(it);\n>  }\n>  \n> -void ewah_dump(struct ewah_bitmap *self)\n> -{\n> -\tsize_t i;\n> -\tfprintf(stderr, \"%\"PRIuMAX\" bits | %\"PRIuMAX\" words | \",\n> -\t\t(uintmax_t)self->bit_size, (uintmax_t)self->buffer_size);\n> -\n> -\tfor (i = 0; i < self->buffer_size; ++i)\n> -\t\tfprintf(stderr, \"%016\"PRIx64\" \", (uint64_t)self->buffer[i]);\n> -\n> -\tfprintf(stderr, \"\\n\");\n> -}\n> -\n>  void ewah_not(struct ewah_bitmap *self)\n>  {\n>  \tsize_t pointer = 0;\n> diff --git a/ewah/ewok.h b/ewah/ewok.h\n> index 619afaa..43adeb5 100644\n> --- a/ewah/ewok.h\n> +++ b/ewah/ewok.h\n> @@ -193,8 +193,6 @@ void ewah_and(\n>  \tstruct ewah_bitmap *ewah_j,\n>  \tstruct ewah_bitmap *out);\n>  \n> -void ewah_dump(struct ewah_bitmap *self);\n> -\n>  /**\n>   * Direct word access\n>   */\n\nI'm always in favour of removing unused (or unwanted) code! :-D\n\nATB,\nRamsay Jones\n"},{"id":"233579","messageId":"20140123020536.GP18964@google.com","threadId":"35562","inReplyTo":"20131221135953.GH21145@sigill.intra.peff.net","subject":"Re: [PATCH v4 08/23] ewah: compressed bitmap implementation","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-01-23T02:05:36Z","receivedAt":"2014-01-23T02:05:36Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Hi,\n\nJeff King wrote:\n\n> EWAH is a word-aligned compressed variant of a bitset (i.e. a data\n> structure that acts as a 0-indexed boolean array for many entries).\n\nI suspect that for some callers it's not word-aligned.\n\nWithout the following squashed in, commits 212f2ffb and later fail t5310\non some machines[1].\n\nOn ARMv5:\n\n\texpecting success: \n\t\tgit rev-list --test-bitmap HEAD\n\n\t*** Error in `/«PKGBUILDDIR»/git': realloc(): invalid pointer: 0x008728b0 ***\n\tAborted\n\tnot ok 3 - rev-list --test-bitmap verifies bitmaps\n\nOn sparc:\n\n\texpecting success: \n\t\tgit rev-list --test-bitmap HEAD\n\n\tBus error\n\tnot ok 3 - rev-list --test-bitmap verifies bitmaps\n\nHopefully it's possible to get the alignment right in the caller\nand tweak the signature to require that instead of using unaligned\nreads like this.  There's still something wrong after this patch ---\nthe new result is a NULL pointer dereference in t5310.7 \"enumerate\n--objects (full bitmap)\".\n\n  (gdb) run\n  Starting program: /home/jrnieder/src/git/git rev-list --objects --use-bitmap-index HEAD\n  [Thread debugging using libthread_db enabled]\n  Using host libthread_db library \"/lib/sparc-linux-gnu/libthread_db.so.1\".\n  537ea4d3eb79c95f602873b1167c480006d2ac2d\n[...]\n  ec635144f60048986bc560c5576355344005e6e7\n\n  Program received signal SIGSEGV, Segmentation fault.\n  0x001321c0 in sha1_to_hex (sha1=0x0) at hex.c:68\n  68                      unsigned int val = *sha1++;\n  (gdb) bt\n  #0  0x001321c0 in sha1_to_hex (sha1=0x0) at hex.c:68\n  #1  0x000b839c in show_object_fast (sha1=0x0, type=OBJ_TREE, exclude=0, name_hash=0, found_pack=0x2b8480, found_offset=4338) at builtin/rev-list.c:270\n  #2  0x00158abc in show_objects_for_type (objects=0x2b2498, type_filter=0x2b0fb0, object_type=OBJ_TREE, show_reach=0xb834c <show_object_fast>) at pack-bitmap.c:640\n  #3  0x001592d0 in traverse_bitmap_commit_list (show_reachable=0xb834c <show_object_fast>) at pack-bitmap.c:818\n  #4  0x000b894c in cmd_rev_list (argc=2, argv=0xffffd688, prefix=0x0) at builtin/rev-list.c:369\n  #5  0x00014024 in run_builtin (p=0x256e38 <commands+1020>, argc=4, argv=0xffffd688) at git.c:314\n  #6  0x00014330 in handle_builtin (argc=4, argv=0xffffd688) at git.c:487\n  #7  0x000144a8 in run_argv (argcp=0xffffd5ec, argv=0xffffd5a0) at git.c:533\n  #8  0x000146fc in main (argc=4, av=0xffffd684) at git.c:616\n  (gdb) frame 2\n  #2  0x00158abc in show_objects_for_type (objects=0x2b2498, type_filter=0x2b0fb0, object_type=OBJ_TREE, show_reach=0xb834c <show_object_fast>) at pack-bitmap.c:640\n  640                             show_reach(sha1, object_type, 0, hash, bitmap_git.pack, entry->offset);\n  (gdb) p entry->nr\n  $1 = 4294967295\n\nLine numbers are in the context of 8e6341d9.  Ideas?\n\n[1] ARMv5 and sparc:\nhttps://buildd.debian.org/status/logs.php?pkg=git&suite=experimental\n\ndiff --git a/ewah/ewah_io.c b/ewah/ewah_io.c\nindex aed0da6..696a8ec 100644\n--- a/ewah/ewah_io.c\n+++ b/ewah/ewah_io.c\n@@ -110,25 +110,38 @@ int ewah_serialize(struct ewah_bitmap *self, int fd)\n \treturn ewah_serialize_to(self, write_helper, (void *)(intptr_t)fd);\n }\n \n+#define get_be32(p) ( \\\n+\t(*((unsigned char *)(p) + 0) << 24) | \\\n+\t(*((unsigned char *)(p) + 1) << 16) | \\\n+\t(*((unsigned char *)(p) + 2) <<  8) | \\\n+\t(*((unsigned char *)(p) + 3) <<  0) )\n+\n+#define get_be64(p) ( \\\n+\t((uint64_t) get_be32(p) << 32) | \\\n+\tget_be32((unsigned char *)(p) + 4) )\n+\n int ewah_read_mmap(struct ewah_bitmap *self, void *map, size_t len)\n {\n-\tuint32_t *read32 = map;\n-\teword_t *read64;\n+\tunsigned char *p = map;\n \tsize_t i;\n \n-\tself->bit_size = ntohl(*read32++);\n-\tself->buffer_size = self->alloc_size = ntohl(*read32++);\n+\tself->bit_size = get_be32(p);\n+\tp += 4;\n+\tself->buffer_size = self->alloc_size = get_be32(p);\n+\tp += 4;\n \tself->buffer = ewah_realloc(self->buffer,\n \t\tself->alloc_size * sizeof(eword_t));\n \n \tif (!self->buffer)\n \t\treturn -1;\n \n-\tfor (i = 0, read64 = (void *)read32; i < self->buffer_size; ++i)\n-\t\tself->buffer[i] = ntohll(*read64++);\n+\tfor (i = 0; i < self->buffer_size; ++i) {\n+\t\tself->buffer[i] = get_be64(p);\n+\t\tp += 8;\n+\t}\n \n-\tread32 = (void *)read64;\n-\tself->rlw = self->buffer + ntohl(*read32++);\n+\tself->rlw = self->buffer + get_be32(p);\n+\tp += 4;\n \n \treturn (3 * 4) + (self->buffer_size * 8);\n }\n"},{"id":"233607","messageId":"20140123183320.GA22995@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20140123020536.GP18964@google.com","subject":"Re: [PATCH v4 08/23] ewah: compressed bitmap implementation","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2014-01-23T18:33:20Z","receivedAt":"2014-01-23T18:33:20Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Wed, Jan 22, 2014 at 06:05:36PM -0800, Jonathan Nieder wrote:\n\n> Jeff King wrote:\n> \n> > EWAH is a word-aligned compressed variant of a bitset (i.e. a data\n> > structure that acts as a 0-indexed boolean array for many entries).\n> \n> I suspect that for some callers it's not word-aligned.\n\nYes, the mmap'd buffers aren't necessarily word-aligned. I don't think\nwe can fix that easily without changing the on-disk format (which comes\nfrom JGit anyway). However, since we are memcpying the bulk of the data\ninto a newly allocated buffer (which must be aligned), we can do that\nfirst, and then fix the endian-ness in place.\n\nThe only SPARC machine I have access to is running Solaris, but after\nsome slight wrestling with the BYTE_ORDER macros, I managed to get it to\ncompile and reproduced the bus error.\n\nHere's a patch series (on top of jk/pack-bitmap, naturally) that lets\nt5310 pass there. I assume the ARM problem is the same, though seeing\nthe failure in realloc() is unexpected. Can you try it on both your\nplatforms with these patches?\n\n  [1/2]: compat: move unaligned helpers to bswap.h\n  [2/2]: ewah: support platforms that require aligned reads\n\n> Hopefully it's possible to get the alignment right in the caller\n> and tweak the signature to require that instead of using unaligned\n> reads like this.  There's still something wrong after this patch ---\n> the new result is a NULL pointer dereference in t5310.7 \"enumerate\n> --objects (full bitmap)\".\n\nAfter my patches, t5310 runs fine for me. I didn't try your patch, but\nmine are similar. Let me know if you still see the problem (there may\nsimply be a bug in yours, but I didn't see it).\n\n-Peff\n"},{"id":"233608","messageId":"20140123183522.GA26447@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20140123183320.GA22995@sigill.intra.peff.net","subject":"[PATCH 1/2] compat: move unaligned helpers to bswap.h","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2014-01-23T18:35:23Z","receivedAt":"2014-01-23T18:35:23Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"From: Vicent Marti <tanoku@gmail.com>\n\nCommit d60c49c (read-cache.c: allow unaligned mapping of the\nindex file, 2012-04-03) introduced helpers to access\nunaligned data. Let's factor them out to make them more\nwidely available.\n\nWhile we're at it, we'll give the helpers more readable\nnames, add a helper for the \"ntohll\" form, and add the\nappropriate Makefile knob.\n\nSigned-off-by: Vicent Marti <tanoku@gmail.com>\nSigned-off-by: Jeff King <peff@peff.net>\n---\n Makefile       |  7 +++++++\n compat/bswap.h | 28 ++++++++++++++++++++++++++++\n read-cache.c   | 44 ++++++++++++--------------------------------\n 3 files changed, 47 insertions(+), 32 deletions(-)\n\ndiff --git a/Makefile b/Makefile\nindex 4136c4f..5711c0e 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -342,6 +342,9 @@ all::\n # Define DEFAULT_HELP_FORMAT to \"man\", \"info\" or \"html\"\n # (defaults to \"man\") if you want to have a different default when\n # \"git help\" is called without a parameter specifying the format.\n+#\n+# Define NEEDS_ALIGNED_ACCESS if your platform cannot handle unaligned\n+# access to integers in mmap'd files.\n \n GIT-VERSION-FILE: FORCE\n \t@$(SHELL_PATH) ./GIT-VERSION-GEN\n@@ -1505,6 +1508,10 @@ ifneq (,$(XDL_FAST_HASH))\n \tBASIC_CFLAGS += -DXDL_FAST_HASH\n endif\n \n+ifdef NEEDS_ALIGNED_ACCESS\n+\tBASIC_CFLAGS += -DNEEDS_ALIGNED_ACCESS\n+endif\n+\n ifeq ($(TCLTK_PATH),)\n NO_TCLTK = NoThanks\n endif\ndiff --git a/compat/bswap.h b/compat/bswap.h\nindex c18a78e..80abc54 100644\n--- a/compat/bswap.h\n+++ b/compat/bswap.h\n@@ -122,3 +122,31 @@ static inline uint64_t git_bswap64(uint64_t x)\n #endif\n \n #endif\n+\n+#ifndef NEEDS_ALIGNED_ACCESS\n+#define align_ntohs(var) ntohs(var)\n+#define align_ntohl(var) ntohl(var)\n+#define align_ntohll(var) ntohll(var)\n+#else\n+static inline uint16_t ntohs_force_align(void *p)\n+{\n+\tuint16_t x;\n+\tmemcpy(&x, p, sizeof(x));\n+\treturn ntohs(x);\n+}\n+static inline uint32_t ntohl_force_align(void *p)\n+{\n+\tuint32_t x;\n+\tmemcpy(&x, p, sizeof(x));\n+\treturn ntohl(x);\n+}\n+static inline uint64_t ntohll_force_align(void *p)\n+{\n+\tuint64_t x;\n+\tmemcpy(&x, p, sizeof(x));\n+\treturn ntohll(x);\n+}\n+#define align_ntohs(var) ntohs_force_align(&(var))\n+#define align_ntohl(var) ntohl_force_align(&(var))\n+#define align_ntohll(var) ntohll_force_align(&(var))\n+#endif\ndiff --git a/read-cache.c b/read-cache.c\nindex 33dd676..fa53504 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -1313,26 +1313,6 @@ int read_index(struct index_state *istate)\n \treturn read_index_from(istate, get_index_file());\n }\n \n-#ifndef NEEDS_ALIGNED_ACCESS\n-#define ntoh_s(var) ntohs(var)\n-#define ntoh_l(var) ntohl(var)\n-#else\n-static inline uint16_t ntoh_s_force_align(void *p)\n-{\n-\tuint16_t x;\n-\tmemcpy(&x, p, sizeof(x));\n-\treturn ntohs(x);\n-}\n-static inline uint32_t ntoh_l_force_align(void *p)\n-{\n-\tuint32_t x;\n-\tmemcpy(&x, p, sizeof(x));\n-\treturn ntohl(x);\n-}\n-#define ntoh_s(var) ntoh_s_force_align(&(var))\n-#define ntoh_l(var) ntoh_l_force_align(&(var))\n-#endif\n-\n static struct cache_entry *cache_entry_from_ondisk(struct ondisk_cache_entry *ondisk,\n \t\t\t\t\t\t   unsigned int flags,\n \t\t\t\t\t\t   const char *name,\n@@ -1340,16 +1320,16 @@ static struct cache_entry *cache_entry_from_ondisk(struct ondisk_cache_entry *on\n {\n \tstruct cache_entry *ce = xmalloc(cache_entry_size(len));\n \n-\tce->ce_stat_data.sd_ctime.sec = ntoh_l(ondisk->ctime.sec);\n-\tce->ce_stat_data.sd_mtime.sec = ntoh_l(ondisk->mtime.sec);\n-\tce->ce_stat_data.sd_ctime.nsec = ntoh_l(ondisk->ctime.nsec);\n-\tce->ce_stat_data.sd_mtime.nsec = ntoh_l(ondisk->mtime.nsec);\n-\tce->ce_stat_data.sd_dev   = ntoh_l(ondisk->dev);\n-\tce->ce_stat_data.sd_ino   = ntoh_l(ondisk->ino);\n-\tce->ce_mode  = ntoh_l(ondisk->mode);\n-\tce->ce_stat_data.sd_uid   = ntoh_l(ondisk->uid);\n-\tce->ce_stat_data.sd_gid   = ntoh_l(ondisk->gid);\n-\tce->ce_stat_data.sd_size  = ntoh_l(ondisk->size);\n+\tce->ce_stat_data.sd_ctime.sec = align_ntohl(ondisk->ctime.sec);\n+\tce->ce_stat_data.sd_mtime.sec = align_ntohl(ondisk->mtime.sec);\n+\tce->ce_stat_data.sd_ctime.nsec = align_ntohl(ondisk->ctime.nsec);\n+\tce->ce_stat_data.sd_mtime.nsec = align_ntohl(ondisk->mtime.nsec);\n+\tce->ce_stat_data.sd_dev   = align_ntohl(ondisk->dev);\n+\tce->ce_stat_data.sd_ino   = align_ntohl(ondisk->ino);\n+\tce->ce_mode  = align_ntohl(ondisk->mode);\n+\tce->ce_stat_data.sd_uid   = align_ntohl(ondisk->uid);\n+\tce->ce_stat_data.sd_gid   = align_ntohl(ondisk->gid);\n+\tce->ce_stat_data.sd_size  = align_ntohl(ondisk->size);\n \tce->ce_flags = flags & ~CE_NAMEMASK;\n \tce->ce_namelen = len;\n \thashcpy(ce->sha1, ondisk->sha1);\n@@ -1389,14 +1369,14 @@ static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk,\n \tunsigned int flags;\n \n \t/* On-disk flags are just 16 bits */\n-\tflags = ntoh_s(ondisk->flags);\n+\tflags = align_ntohs(ondisk->flags);\n \tlen = flags & CE_NAMEMASK;\n \n \tif (flags & CE_EXTENDED) {\n \t\tstruct ondisk_cache_entry_extended *ondisk2;\n \t\tint extended_flags;\n \t\tondisk2 = (struct ondisk_cache_entry_extended *)ondisk;\n-\t\textended_flags = ntoh_s(ondisk2->flags2) << 16;\n+\t\textended_flags = align_ntohs(ondisk2->flags2) << 16;\n \t\t/* We do not yet understand any bit out of CE_EXTENDED_FLAGS */\n \t\tif (extended_flags & ~CE_EXTENDED_FLAGS)\n \t\t\tdie(\"Unknown index entry format %08x\", extended_flags);\n-- \n1.8.5.2.500.g8060133\n"},{"id":"233609","messageId":"20140123183543.GB26447@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20140123183320.GA22995@sigill.intra.peff.net","subject":"[PATCH 2/2] ewah: support platforms that require aligned reads","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2014-01-23T18:35:43Z","receivedAt":"2014-01-23T18:35:43Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"From: Vicent Marti <tanoku@gmail.com>\n\nThe caller may hand us an unaligned buffer (e.g., because it\nis an mmap of a file with many ewah bitmaps). On some\nplatforms (like SPARC) this can cause a bus error. We can\nfix it with a combination of force-align macros and moving\nthe data into an aligned buffer (which we would do anyway,\nbut we can move it before fixing the endianness).\n\nSigned-off-by: Vicent Marti <tanoku@gmail.com>\nSigned-off-by: Jeff King <peff@peff.net>\n---\n ewah/ewah_io.c | 33 ++++++++++++++++++++++++---------\n 1 file changed, 24 insertions(+), 9 deletions(-)\n\ndiff --git a/ewah/ewah_io.c b/ewah/ewah_io.c\nindex aed0da6..1948ba5 100644\n--- a/ewah/ewah_io.c\n+++ b/ewah/ewah_io.c\n@@ -112,23 +112,38 @@ int ewah_serialize(struct ewah_bitmap *self, int fd)\n \n int ewah_read_mmap(struct ewah_bitmap *self, void *map, size_t len)\n {\n-\tuint32_t *read32 = map;\n-\teword_t *read64;\n-\tsize_t i;\n+\tuint8_t *ptr = map;\n+\n+\tself->bit_size = align_ntohl(*(uint32_t *)ptr);\n+\tptr += sizeof(uint32_t);\n+\n+\tself->buffer_size = self->alloc_size = align_ntohl(*(uint32_t *)ptr);\n+\tptr += sizeof(uint32_t);\n \n-\tself->bit_size = ntohl(*read32++);\n-\tself->buffer_size = self->alloc_size = ntohl(*read32++);\n \tself->buffer = ewah_realloc(self->buffer,\n \t\tself->alloc_size * sizeof(eword_t));\n \n \tif (!self->buffer)\n \t\treturn -1;\n \n-\tfor (i = 0, read64 = (void *)read32; i < self->buffer_size; ++i)\n-\t\tself->buffer[i] = ntohll(*read64++);\n+\t/*\n+\t * Copy the raw data for the bitmap as a whole chunk;\n+\t * if we're in a little-endian platform, we'll perform\n+\t * the endianness conversion in a separate pass to ensure\n+\t * we're loading 8-byte aligned words.\n+\t */\n+\tmemcpy(self->buffer, ptr, self->buffer_size * sizeof(uint64_t));\n+\tptr += self->buffer_size * sizeof(uint64_t);\n+\n+#if __BYTE_ORDER != __BIG_ENDIAN\n+\t{\n+\t\tsize_t i;\n+\t\tfor (i = 0; i < self->buffer_size; ++i)\n+\t\t\tself->buffer[i] = ntohll(self->buffer[i]);\n+\t}\n+#endif\n \n-\tread32 = (void *)read64;\n-\tself->rlw = self->buffer + ntohl(*read32++);\n+\tself->rlw = self->buffer + align_ntohl(*(uint32_t *)ptr);\n \n \treturn (3 * 4) + (self->buffer_size * 8);\n }\n-- \n1.8.5.2.500.g8060133\n"},{"id":"233611","messageId":"20140123194118.GT18964@google.com","threadId":"35562","inReplyTo":"20140123183522.GA26447@sigill.intra.peff.net","subject":"Re: [PATCH 1/2] compat: move unaligned helpers to bswap.h","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-01-23T19:41:18Z","receivedAt":"2014-01-23T19:41:18Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Jeff King wrote:\n\n> Commit d60c49c (read-cache.c: allow unaligned mapping of the\n> index file, 2012-04-03) introduced helpers to access\n> unaligned data. Let's factor them out to make them more\n> widely available.\n>\n> While we're at it, we'll give the helpers more readable\n> names, add a helper for the \"ntohll\" form, and add the\n> appropriate Makefile knob.\n\nWeird.  Why wasn't git broken on the relevant platforms before (given\nthat no one has been setting NEEDS_ALIGNED_ACCESS for them)?\n\nPuzzled,\nJonathan\n"},{"id":"233613","messageId":"20140123194401.GA31412@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20140123194118.GT18964@google.com","subject":"Re: [PATCH 1/2] compat: move unaligned helpers to bswap.h","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2014-01-23T19:44:01Z","receivedAt":"2014-01-23T19:44:01Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, Jan 23, 2014 at 11:41:18AM -0800, Jonathan Nieder wrote:\n\n> Jeff King wrote:\n> \n> > Commit d60c49c (read-cache.c: allow unaligned mapping of the\n> > index file, 2012-04-03) introduced helpers to access\n> > unaligned data. Let's factor them out to make them more\n> > widely available.\n> >\n> > While we're at it, we'll give the helpers more readable\n> > names, add a helper for the \"ntohll\" form, and add the\n> > appropriate Makefile knob.\n> \n> Weird.  Why wasn't git broken on the relevant platforms before (given\n> that no one has been setting NEEDS_ALIGNED_ACCESS for them)?\n\nBecause most of our data structures support aligned access. Thomas\nmentioned this as a potential issue earlier, and I said in a re-roll\ncover letter:\n\n  I did not include the NEEDS_ALIGNED_ACCESS patch. I note that we do\n  not even have a Makefile knob for this, and the code in read-cache.c\n  has probably never actually been used. Are there real systems that\n  have a problem? The read-cache code was in support of the index v4\n  experiment, which did away with the 8-byte padding. So it could be\n  that we simply don't see it, because everything is currently aligned.\n\nI think it was a bug waiting to surface if index v4 ever got wide use.\n\n-Peff\n"},{"id":"233614","messageId":"20140123195206.GU18964@google.com","threadId":"35562","inReplyTo":"20140123183320.GA22995@sigill.intra.peff.net","subject":"Re: [PATCH v4 08/23] ewah: compressed bitmap implementation","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-01-23T19:52:06Z","receivedAt":"2014-01-23T19:52:06Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Jeff King wrote:\n\n> Here's a patch series (on top of jk/pack-bitmap, naturally) that lets\n> t5310 pass there. I assume the ARM problem is the same, though seeing\n> the failure in realloc() is unexpected. Can you try it on both your\n> platforms with these patches?\n\nThanks.  Trying it out now.\n\n[...]\n>> Hopefully it's possible to get the alignment right in the caller\n>> and tweak the signature to require that instead of using unaligned\n>> reads like this.  There's still something wrong after this patch ---\n>> the new result is a NULL pointer dereference in t5310.7 \"enumerate\n>> --objects (full bitmap)\".\n>\n> After my patches, t5310 runs fine for me. I didn't try your patch, but\n> mine are similar. Let me know if you still see the problem (there may\n> simply be a bug in yours, but I didn't see it).\n\nI had left out a cast to unsigned, producing an overflow.\n\nMy main worry about the patches is that they will probably run into\nan analagous problem to the one that v1.7.12-rc0~1^2~2 (block-sha1:\navoid pointer conversion that violates alignment constraints,\n2012-07-22) solved.  By casting the pointer to (uint32_t *) we are\ntelling the compiler it is 32-bit aligned (C99 section 6.3.2.3).\n\nThanks,\nJonathan\n"},{"id":"233618","messageId":"20140123195643.GV18964@google.com","threadId":"35562","inReplyTo":"20140123194401.GA31412@sigill.intra.peff.net","subject":"Re: [PATCH 1/2] compat: move unaligned helpers to bswap.h","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-01-23T19:56:43Z","receivedAt":"2014-01-23T19:56:43Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Jeff King wrote:\n\n> I think it was a bug waiting to surface if index v4 ever got wide use.\n\nAh, ok.\n\nIn that case I think git-compat-util.h should include something like\nwhat block-sha1/sha1.c has:\n\n\t#if !defined(__i386__) && !defined(__x86_64__) && \\\n\t    !defined(_M_IX86) && !defined(_M_X64) && \\\n\t    !defined(__ppc__) && !defined(__ppc64__) && \\\n\t    !defined(__powerpc__) && !defined(__powerpc64__) && \\\n\t    !defined(__s390__) && !defined(__s390x__)\n\t#define NEEDS_ALIGNED_ACCESS\n\t#endif\n\nOtherwise we are relying on the person building to know their own\narchitecture intimately, which shouldn't be necessary.\n\nMeanwhile, as mentioned in the other message, I suspect the\nNEEDS_ALIGNED_ACCESS code path is broken for aggressive compilers\nanyway.  Looking more.\n\nThanks,\nJonathan\n"},{"id":"233620","messageId":"20140123200311.GA31920@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20140123195206.GU18964@google.com","subject":"Re: [PATCH v4 08/23] ewah: compressed bitmap implementation","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2014-01-23T20:03:11Z","receivedAt":"2014-01-23T20:03:11Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, Jan 23, 2014 at 11:52:06AM -0800, Jonathan Nieder wrote:\n\n> > After my patches, t5310 runs fine for me. I didn't try your patch, but\n> > mine are similar. Let me know if you still see the problem (there may\n> > simply be a bug in yours, but I didn't see it).\n> \n> I had left out a cast to unsigned, producing an overflow.\n> \n> My main worry about the patches is that they will probably run into\n> an analagous problem to the one that v1.7.12-rc0~1^2~2 (block-sha1:\n> avoid pointer conversion that violates alignment constraints,\n> 2012-07-22) solved.  By casting the pointer to (uint32_t *) we are\n> telling the compiler it is 32-bit aligned (C99 section 6.3.2.3).\n\nYeah, maybe. We go via memcpy, which takes a \"void *\", so that part is\ngood. However, the new code looks like:\n\n  foo = align_ntohl(*(uint32_t *)ptr);\n\nI think this probably works in practice because align_ntohl is inlined,\nand any sane compiler will never actually load the variable. If we\nchange the signature of align_ntohl, we can do this:\n\n  uint32_t align_ntohl(void *ptr)\n  {\n          uint32_t x;\n          memcpy(x, ptr, sizeof(x));\n          return ntohl(x);\n  }\n\n  ...\n\n  foo = align_ntohl(ptr);\n\nThe memcpy solution is taken from read-cache.c, but as we noted, it\nprobably hasn't been used a lot. The blk_sha1 get_be may be faster, as\nit converts as it reads. However, the bulk of the data is copied via\na single memcpy and then modified in place. I don't know if that would\nbe faster or not (for a big-endian system it probably is, since we can\nomit the modification loop entirely).\n\n-Peff\n"},{"id":"233621","messageId":"20140123200450.GB31920@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20140123195643.GV18964@google.com","subject":"Re: [PATCH 1/2] compat: move unaligned helpers to bswap.h","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2014-01-23T20:04:50Z","receivedAt":"2014-01-23T20:04:50Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, Jan 23, 2014 at 11:56:43AM -0800, Jonathan Nieder wrote:\n\n> Jeff King wrote:\n> \n> > I think it was a bug waiting to surface if index v4 ever got wide use.\n> \n> Ah, ok.\n> \n> In that case I think git-compat-util.h should include something like\n> what block-sha1/sha1.c has:\n> \n> \t#if !defined(__i386__) && !defined(__x86_64__) && \\\n> \t    !defined(_M_IX86) && !defined(_M_X64) && \\\n> \t    !defined(__ppc__) && !defined(__ppc64__) && \\\n> \t    !defined(__powerpc__) && !defined(__powerpc64__) && \\\n> \t    !defined(__s390__) && !defined(__s390x__)\n> \t#define NEEDS_ALIGNED_ACCESS\n> \t#endif\n> \n> Otherwise we are relying on the person building to know their own\n> architecture intimately, which shouldn't be necessary.\n\nYeah, I agree it would be nice to autodetect. I just didn't know what\nthe right set of platforms was, and assumed people would tweak the\nMakefile knob as appropriate (though it is probably much easier to do so\nwithin the compiler, where we have the right architecture variables\nset).\n\n-Peff\n"},{"id":"233622","messageId":"20140123200804.GW18964@google.com","threadId":"35562","inReplyTo":"20140123200450.GB31920@sigill.intra.peff.net","subject":"Re: [PATCH 1/2] compat: move unaligned helpers to bswap.h","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-01-23T20:08:04Z","receivedAt":"2014-01-23T20:08:04Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Jeff King wrote:\n> On Thu, Jan 23, 2014 at 11:56:43AM -0800, Jonathan Nieder wrote:\n\n>> In that case I think git-compat-util.h should include something like\n>> what block-sha1/sha1.c has:\n>> \n>> \t#if !defined(__i386__) && !defined(__x86_64__) && \\\n>> \t    !defined(_M_IX86) && !defined(_M_X64) && \\\n>> \t    !defined(__ppc__) && !defined(__ppc64__) && \\\n>> \t    !defined(__powerpc__) && !defined(__powerpc64__) && \\\n>> \t    !defined(__s390__) && !defined(__s390x__)\n>> \t#define NEEDS_ALIGNED_ACCESS\n>> \t#endif\n>>\n>> Otherwise we are relying on the person building to know their own\n>> architecture intimately, which shouldn't be necessary.\n>\n> Yeah, I agree it would be nice to autodetect.\n\nThe nice thing is that false positives are harmless, modulo slowing\ndown git a little if the compiler doesn't figure out how to optimize\nthe NEEDS_ALIGNED_ACCESS codepath when on an unlisted platform that\ndoesn't, in fact, need aligned access.\n\nIn other words, it would work out of the box for everybody.\n"},{"id":"233623","messageId":"20140123200911.GB32229@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20140123200804.GW18964@google.com","subject":"Re: [PATCH 1/2] compat: move unaligned helpers to bswap.h","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2014-01-23T20:09:11Z","receivedAt":"2014-01-23T20:09:11Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, Jan 23, 2014 at 12:08:04PM -0800, Jonathan Nieder wrote:\n\n> Jeff King wrote:\n> > On Thu, Jan 23, 2014 at 11:56:43AM -0800, Jonathan Nieder wrote:\n> \n> >> In that case I think git-compat-util.h should include something like\n> >> what block-sha1/sha1.c has:\n> >> \n> >> \t#if !defined(__i386__) && !defined(__x86_64__) && \\\n> >> \t    !defined(_M_IX86) && !defined(_M_X64) && \\\n> >> \t    !defined(__ppc__) && !defined(__ppc64__) && \\\n> >> \t    !defined(__powerpc__) && !defined(__powerpc64__) && \\\n> >> \t    !defined(__s390__) && !defined(__s390x__)\n> >> \t#define NEEDS_ALIGNED_ACCESS\n> >> \t#endif\n> >>\n> >> Otherwise we are relying on the person building to know their own\n> >> architecture intimately, which shouldn't be necessary.\n> >\n> > Yeah, I agree it would be nice to autodetect.\n> \n> The nice thing is that false positives are harmless, modulo slowing\n> down git a little if the compiler doesn't figure out how to optimize\n> the NEEDS_ALIGNED_ACCESS codepath when on an unlisted platform that\n> doesn't, in fact, need aligned access.\n\nOK, I'll refactor the knob.\n\n-Peff\n"},{"id":"233624","messageId":"20140123201223.GX18964@google.com","threadId":"35562","inReplyTo":"20140123200311.GA31920@sigill.intra.peff.net","subject":"Re: [PATCH v4 08/23] ewah: compressed bitmap implementation","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-01-23T20:12:23Z","receivedAt":"2014-01-23T20:12:23Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Jeff King wrote:\n> On Thu, Jan 23, 2014 at 11:52:06AM -0800, Jonathan Nieder wrote:\n\n>> My main worry about the patches is that they will probably run into\n>> an analagous problem to the one that v1.7.12-rc0~1^2~2\n[...]\n> I think this probably works in practice because align_ntohl is inlined,\n> and any sane compiler will never actually load the variable.\n\nI don't think that's safe to rely on.  The example named above didn't\npose any problems except on one platform.  All the relevant functions\nwere static and easy to inline.  GCC just followed the standard\nliterally and chose to break by reading one word at a time, just like\nin this case it could break e.g. by copying one word at a time in\n__builtin_memcpy (which seems perfectly reasonable to me ---\noptimization involves a lot of constraint solving, and if you can't\ntrust your constraints then there's not much you can do).\n"},{"id":"233625","messageId":"20140123201344.GA32580@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20140123201223.GX18964@google.com","subject":"Re: [PATCH v4 08/23] ewah: compressed bitmap implementation","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2014-01-23T20:13:45Z","receivedAt":"2014-01-23T20:13:45Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, Jan 23, 2014 at 12:12:23PM -0800, Jonathan Nieder wrote:\n\n> Jeff King wrote:\n> > On Thu, Jan 23, 2014 at 11:52:06AM -0800, Jonathan Nieder wrote:\n> \n> >> My main worry about the patches is that they will probably run into\n> >> an analagous problem to the one that v1.7.12-rc0~1^2~2\n> [...]\n> > I think this probably works in practice because align_ntohl is inlined,\n> > and any sane compiler will never actually load the variable.\n> \n> I don't think that's safe to rely on.  The example named above didn't\n> pose any problems except on one platform.  All the relevant functions\n> were static and easy to inline.  GCC just followed the standard\n> literally and chose to break by reading one word at a time, just like\n> in this case it could break e.g. by copying one word at a time in\n> __builtin_memcpy (which seems perfectly reasonable to me ---\n> optimization involves a lot of constraint solving, and if you can't\n> trust your constraints then there's not much you can do).\n\nI wasn't disagreeing with you. I was guessing at why it did not fail out\nof the box when I tested it.  What do you think of the alternative I\nposted?\n\n-Peff\n"},{"id":"233626","messageId":"CAJo=hJtQG_u4=SjPAgU8h4Wew9LjaXUxnHqTT3Q9E1=_5LJ6Sw@mail.gmail.com","threadId":"35562","inReplyTo":"20140123183320.GA22995@sigill.intra.peff.net","subject":"Re: [PATCH v4 08/23] ewah: compressed bitmap implementation","fromName":"Shawn Pearce","fromEmail":"spearce@spearce.org","sentAt":"2014-01-23T20:14:03Z","receivedAt":"2014-01-23T20:14:03Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"On Thu, Jan 23, 2014 at 10:33 AM, Jeff King <peff@peff.net> wrote:\n> On Wed, Jan 22, 2014 at 06:05:36PM -0800, Jonathan Nieder wrote:\n>\n>> Jeff King wrote:\n>>\n>> > EWAH is a word-aligned compressed variant of a bitset (i.e. a data\n>> > structure that acts as a 0-indexed boolean array for many entries).\n>>\n>> I suspect that for some callers it's not word-aligned.\n>\n> Yes, the mmap'd buffers aren't necessarily word-aligned. I don't think\n> we can fix that easily without changing the on-disk format (which comes\n> from JGit anyway).\n\nOuch, sorry about that. JGit doesn't mmap the file so we didn't think\nabout the impact of words not being aligned. I should have caught\nthat, but I didn't.\n"},{"id":"233627","messageId":"20140123201818.GY18964@google.com","threadId":"35562","inReplyTo":"20140123183320.GA22995@sigill.intra.peff.net","subject":"Re: [PATCH v4 08/23] ewah: compressed bitmap implementation","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-01-23T20:18:18Z","receivedAt":"2014-01-23T20:18:18Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Jeff King wrote:\n\n>   [1/2]: compat: move unaligned helpers to bswap.h\n>   [2/2]: ewah: support platforms that require aligned reads\n\nAfter setting NEEDS_ALIGNED_ACCESS,\nTested-by: Jonathan Nieder <jrnieder@gmail.com> # ARMv5\n"},{"id":"233628","messageId":"20140123202342.GZ18964@google.com","threadId":"35562","inReplyTo":"20140123200311.GA31920@sigill.intra.peff.net","subject":"Re: [PATCH v4 08/23] ewah: compressed bitmap implementation","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-01-23T20:23:42Z","receivedAt":"2014-01-23T20:23:42Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Jeff King wrote:\n\n>                                                              If we\n> change the signature of align_ntohl, we can do this:\n>\n>   uint32_t align_ntohl(void *ptr)\n>   {\n>           uint32_t x;\n>           memcpy(x, ptr, sizeof(x));\n>           return ntohl(x);\n>   }\n>\n>   ...\n>\n>   foo = align_ntohl(ptr);\n>\n> The memcpy solution is taken from read-cache.c, but as we noted, it\n> probably hasn't been used a lot. The blk_sha1 get_be may be faster, as\n> it converts as it reads.\n\nI doubt there's much difference either way, especially after an\noptimizer gets its hands on it.  According to [1] ARM has no fast\nbyte swap instruction so with -O0 the byte-at-a-time implementation is\nprobably faster there.  I can try a performance test if you like.\n\nJonathan\n\n[1] http://thread.gmane.org/gmane.comp.version-control.git/125737\n"},{"id":"233629","messageId":"20140123202645.GA329@sigill.intra.peff.net","threadId":"35562","inReplyTo":"CAJo=hJtQG_u4=SjPAgU8h4Wew9LjaXUxnHqTT3Q9E1=_5LJ6Sw@mail.gmail.com","subject":"Re: [PATCH v4 08/23] ewah: compressed bitmap implementation","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2014-01-23T20:26:45Z","receivedAt":"2014-01-23T20:26:45Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, Jan 23, 2014 at 12:14:03PM -0800, Shawn Pearce wrote:\n\n> > Yes, the mmap'd buffers aren't necessarily word-aligned. I don't think\n> > we can fix that easily without changing the on-disk format (which comes\n> > from JGit anyway).\n> \n> Ouch, sorry about that. JGit doesn't mmap the file so we didn't think\n> about the impact of words not being aligned. I should have caught\n> that, but I didn't.\n\nLooking over the format, I think the only thing preventing 4-byte\nalignment is the 1-byte XOR-offset and 1-byte flags field for each\nbitmap. If we ever have a v2, we could pad the sum of those out to 4\nbytes. Is 4-byte alignment enough? We do treat the actual data as 64-bit\nintegers. I wonder if that would have problems on Sparc64, for example.\n\n-Peff\n"},{"id":"233630","messageId":"20140123202940.GB329@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20140123202342.GZ18964@google.com","subject":"Re: [PATCH v4 08/23] ewah: compressed bitmap implementation","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2014-01-23T20:29:40Z","receivedAt":"2014-01-23T20:29:40Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, Jan 23, 2014 at 12:23:42PM -0800, Jonathan Nieder wrote:\n\n> > The memcpy solution is taken from read-cache.c, but as we noted, it\n> > probably hasn't been used a lot. The blk_sha1 get_be may be faster, as\n> > it converts as it reads.\n> \n> I doubt there's much difference either way, especially after an\n> optimizer gets its hands on it.  According to [1] ARM has no fast\n> byte swap instruction so with -O0 the byte-at-a-time implementation is\n> probably faster there.  I can try a performance test if you like.\n\nIf you're curious and have time, go ahead and benchmark what I posted\nagainst what you posted (with your fix). But you'll probably need a big\nrepo like the kernel to notice anything.\n\nBut I don't mind that much if we just use the memcpy trick for now. It's\nnice and obvious, and we can always change it later if somebody has\nnumbers (I doubt it will be all that noticeable anyway; this isn't\nnearly as tight a loop as the BLK_SHA1 code).\n\n-Peff\n"},{"id":"233632","messageId":"20140123203830.GA4365@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20140123200311.GA31920@sigill.intra.peff.net","subject":"Re: [PATCH v4 08/23] ewah: compressed bitmap implementation","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2014-01-23T20:38:31Z","receivedAt":"2014-01-23T20:38:31Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, Jan 23, 2014 at 03:03:11PM -0500, Jeff King wrote:\n\n> > My main worry about the patches is that they will probably run into\n> > an analagous problem to the one that v1.7.12-rc0~1^2~2 (block-sha1:\n> > avoid pointer conversion that violates alignment constraints,\n> > 2012-07-22) solved.  By casting the pointer to (uint32_t *) we are\n> > telling the compiler it is 32-bit aligned (C99 section 6.3.2.3).\n> \n> Yeah, maybe. We go via memcpy, which takes a \"void *\", so that part is\n> good. However, the new code looks like:\n> \n>   foo = align_ntohl(*(uint32_t *)ptr);\n> \n> I think this probably works in practice because align_ntohl is inlined,\n> and any sane compiler will never actually load the variable. If we\n> change the signature of align_ntohl, we can do this:\n\nActually, it is a little trickier than that. We actually take the\naddress in the macro. So even without inlining, we end up casting to\nvoid. I still think this:\n\n>   uint32_t align_ntohl(void *ptr)\n>   {\n>           uint32_t x;\n>           memcpy(x, ptr, sizeof(x));\n>           return ntohl(x);\n>   }\n\nis a little more obvious, though. It does mean that everybody has to\npass a pointer, though, and on platforms where non-aligned reads are OK,\nwe do the cast ourselves. That means that:\n\n  foo = align_ntohl(&bar);\n\nwill not be able to do any type-checking for \"bar\" (say, when we are\npulling \"bar\" straight out of a packed struct). I don't know how much\nwe care.\n\n-Peff\n"},{"id":"233639","messageId":"20140123212036.GA21299@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20140123183320.GA22995@sigill.intra.peff.net","subject":"[PATCH v2 0/3] unaligned reads from .bitmap files","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2014-01-23T21:20:36Z","receivedAt":"2014-01-23T21:20:36Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, Jan 23, 2014 at 01:33:20PM -0500, Jeff King wrote:\n\n> Here's a patch series (on top of jk/pack-bitmap, naturally) that lets\n> t5310 pass there. I assume the ARM problem is the same, though seeing\n> the failure in realloc() is unexpected. Can you try it on both your\n> platforms with these patches?\n> \n>   [1/2]: compat: move unaligned helpers to bswap.h\n>   [2/2]: ewah: support platforms that require aligned reads\n\nHere it is again, fixing the issues we've discussed.\n\nInstead of building on the code in read-cache, it pulls the much more\nbattle-tested code from block-sha1, and refactors read-cache to use that\ninstead. So the fix now kicks in automatically, and in theory it is a\nslight bit faster (though I still doubt it would even be measurable in\nthis case).\n\n  [1/3]: block-sha1: factor out get_be and put_be wrappers\n  [2/3]: read-cache: use get_be32 instead of hand-rolled ntoh_l\n  [3/3]: ewah: support platforms that require aligned reads\n\n-Peff\n"},{"id":"233640","messageId":"20140123212308.GA21705@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20140123212036.GA21299@sigill.intra.peff.net","subject":"[PATCH 1/3] block-sha1: factor out get_be and put_be wrappers","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2014-01-23T21:23:09Z","receivedAt":"2014-01-23T21:23:09Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"The BLK_SHA1 code has optimized wrappers for doing endian\nconversions on memory that may not be aligned. Let's pull\nthem out so that we can use them elsewhere, especially the\ntime-tested list of platforms that prefer each strategy.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\nThese short names might not be descriptive enough now that they are\nglobals. However, they make sense to me. I'm open to suggestions if\nsomebody disagrees.\n\n block-sha1/sha1.c | 32 --------------------------------\n compat/bswap.h    | 32 ++++++++++++++++++++++++++++++++\n 2 files changed, 32 insertions(+), 32 deletions(-)\n\ndiff --git a/block-sha1/sha1.c b/block-sha1/sha1.c\nindex e1a1eb6..22b125c 100644\n--- a/block-sha1/sha1.c\n+++ b/block-sha1/sha1.c\n@@ -62,38 +62,6 @@\n   #define setW(x, val) (W(x) = (val))\n #endif\n \n-/*\n- * Performance might be improved if the CPU architecture is OK with\n- * unaligned 32-bit loads and a fast ntohl() is available.\n- * Otherwise fall back to byte loads and shifts which is portable,\n- * and is faster on architectures with memory alignment issues.\n- */\n-\n-#if defined(__i386__) || defined(__x86_64__) || \\\n-    defined(_M_IX86) || defined(_M_X64) || \\\n-    defined(__ppc__) || defined(__ppc64__) || \\\n-    defined(__powerpc__) || defined(__powerpc64__) || \\\n-    defined(__s390__) || defined(__s390x__)\n-\n-#define get_be32(p)\tntohl(*(unsigned int *)(p))\n-#define put_be32(p, v)\tdo { *(unsigned int *)(p) = htonl(v); } while (0)\n-\n-#else\n-\n-#define get_be32(p)\t( \\\n-\t(*((unsigned char *)(p) + 0) << 24) | \\\n-\t(*((unsigned char *)(p) + 1) << 16) | \\\n-\t(*((unsigned char *)(p) + 2) <<  8) | \\\n-\t(*((unsigned char *)(p) + 3) <<  0) )\n-#define put_be32(p, v)\tdo { \\\n-\tunsigned int __v = (v); \\\n-\t*((unsigned char *)(p) + 0) = __v >> 24; \\\n-\t*((unsigned char *)(p) + 1) = __v >> 16; \\\n-\t*((unsigned char *)(p) + 2) = __v >>  8; \\\n-\t*((unsigned char *)(p) + 3) = __v >>  0; } while (0)\n-\n-#endif\n-\n /* This \"rolls\" over the 512-bit array */\n #define W(x) (array[(x)&15])\n \ndiff --git a/compat/bswap.h b/compat/bswap.h\nindex c18a78e..7d17953 100644\n--- a/compat/bswap.h\n+++ b/compat/bswap.h\n@@ -122,3 +122,35 @@ static inline uint64_t git_bswap64(uint64_t x)\n #endif\n \n #endif\n+\n+/*\n+ * Performance might be improved if the CPU architecture is OK with\n+ * unaligned 32-bit loads and a fast ntohl() is available.\n+ * Otherwise fall back to byte loads and shifts which is portable,\n+ * and is faster on architectures with memory alignment issues.\n+ */\n+\n+#if defined(__i386__) || defined(__x86_64__) || \\\n+    defined(_M_IX86) || defined(_M_X64) || \\\n+    defined(__ppc__) || defined(__ppc64__) || \\\n+    defined(__powerpc__) || defined(__powerpc64__) || \\\n+    defined(__s390__) || defined(__s390x__)\n+\n+#define get_be32(p)\tntohl(*(unsigned int *)(p))\n+#define put_be32(p, v)\tdo { *(unsigned int *)(p) = htonl(v); } while (0)\n+\n+#else\n+\n+#define get_be32(p)\t( \\\n+\t(*((unsigned char *)(p) + 0) << 24) | \\\n+\t(*((unsigned char *)(p) + 1) << 16) | \\\n+\t(*((unsigned char *)(p) + 2) <<  8) | \\\n+\t(*((unsigned char *)(p) + 3) <<  0) )\n+#define put_be32(p, v)\tdo { \\\n+\tunsigned int __v = (v); \\\n+\t*((unsigned char *)(p) + 0) = __v >> 24; \\\n+\t*((unsigned char *)(p) + 1) = __v >> 16; \\\n+\t*((unsigned char *)(p) + 2) = __v >>  8; \\\n+\t*((unsigned char *)(p) + 3) = __v >>  0; } while (0)\n+\n+#endif\n-- \n1.8.5.2.500.g8060133\n"},{"id":"233643","messageId":"20140123212642.GB21705@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20140123212036.GA21299@sigill.intra.peff.net","subject":"[PATCH 2/3] read-cache: use get_be32 instead of hand-rolled ntoh_l","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2014-01-23T21:26:42Z","receivedAt":"2014-01-23T21:26:42Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"Commit d60c49c (read-cache.c: allow unaligned mapping of the\nindex file, 2012-04-03) introduced helpers to access\nunaligned data. However, we already have get_be32, which has\na few advantages:\n\n  1. It's already written, so we avoid duplication.\n\n  2. It's probably faster, since it does the endian\n     conversion and the alignment fix at the same time.\n\n  3. The get_be32 code is well-tested, having been in\n     block-sha1 for a long time. By contrast, our custom\n     helpers were probably almost never used, since the user\n     needed to manually define a macro to enable them.\n\nWe have to add a get_be16 implementation to the existing\nget_be32, but that is very simple to do.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\nThis _might_ still suffer from the issue fixed in 5f6a112 (block-sha1:\navoid pointer conversion that violates alignment constraints,\n2012-07-22), as we are taking the pointer of a uint32 in a struct. But\nif that is the case, then the original did, as well. It's not clear to\nme if the casting get_be32 does is sufficient, or if a sufficiently\nclever compiler might make assumptions based on the original pointer\ntype.\n\nI'm inclined to leave it for now, as we haven't made anything worse, and\nnobody has reported a problem.\n\n compat/bswap.h |  4 ++++\n read-cache.c   | 44 ++++++++++++--------------------------------\n 2 files changed, 16 insertions(+), 32 deletions(-)\n\ndiff --git a/compat/bswap.h b/compat/bswap.h\nindex 7d17953..120c6c1 100644\n--- a/compat/bswap.h\n+++ b/compat/bswap.h\n@@ -136,11 +136,15 @@ static inline uint64_t git_bswap64(uint64_t x)\n     defined(__powerpc__) || defined(__powerpc64__) || \\\n     defined(__s390__) || defined(__s390x__)\n \n+#define get_be16(p)\tntohs(*(unsigned short *)(p))\n #define get_be32(p)\tntohl(*(unsigned int *)(p))\n #define put_be32(p, v)\tdo { *(unsigned int *)(p) = htonl(v); } while (0)\n \n #else\n \n+#define get_be16(p)\t( \\\n+\t(*((unsigned char *)(p) + 0) << 8) | \\\n+\t(*((unsigned char *)(p) + 1) << 0) )\n #define get_be32(p)\t( \\\n \t(*((unsigned char *)(p) + 0) << 24) | \\\n \t(*((unsigned char *)(p) + 1) << 16) | \\\ndiff --git a/read-cache.c b/read-cache.c\nindex 33dd676..4221872 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -1313,26 +1313,6 @@ int read_index(struct index_state *istate)\n \treturn read_index_from(istate, get_index_file());\n }\n \n-#ifndef NEEDS_ALIGNED_ACCESS\n-#define ntoh_s(var) ntohs(var)\n-#define ntoh_l(var) ntohl(var)\n-#else\n-static inline uint16_t ntoh_s_force_align(void *p)\n-{\n-\tuint16_t x;\n-\tmemcpy(&x, p, sizeof(x));\n-\treturn ntohs(x);\n-}\n-static inline uint32_t ntoh_l_force_align(void *p)\n-{\n-\tuint32_t x;\n-\tmemcpy(&x, p, sizeof(x));\n-\treturn ntohl(x);\n-}\n-#define ntoh_s(var) ntoh_s_force_align(&(var))\n-#define ntoh_l(var) ntoh_l_force_align(&(var))\n-#endif\n-\n static struct cache_entry *cache_entry_from_ondisk(struct ondisk_cache_entry *ondisk,\n \t\t\t\t\t\t   unsigned int flags,\n \t\t\t\t\t\t   const char *name,\n@@ -1340,16 +1320,16 @@ static struct cache_entry *cache_entry_from_ondisk(struct ondisk_cache_entry *on\n {\n \tstruct cache_entry *ce = xmalloc(cache_entry_size(len));\n \n-\tce->ce_stat_data.sd_ctime.sec = ntoh_l(ondisk->ctime.sec);\n-\tce->ce_stat_data.sd_mtime.sec = ntoh_l(ondisk->mtime.sec);\n-\tce->ce_stat_data.sd_ctime.nsec = ntoh_l(ondisk->ctime.nsec);\n-\tce->ce_stat_data.sd_mtime.nsec = ntoh_l(ondisk->mtime.nsec);\n-\tce->ce_stat_data.sd_dev   = ntoh_l(ondisk->dev);\n-\tce->ce_stat_data.sd_ino   = ntoh_l(ondisk->ino);\n-\tce->ce_mode  = ntoh_l(ondisk->mode);\n-\tce->ce_stat_data.sd_uid   = ntoh_l(ondisk->uid);\n-\tce->ce_stat_data.sd_gid   = ntoh_l(ondisk->gid);\n-\tce->ce_stat_data.sd_size  = ntoh_l(ondisk->size);\n+\tce->ce_stat_data.sd_ctime.sec = get_be32(&ondisk->ctime.sec);\n+\tce->ce_stat_data.sd_mtime.sec = get_be32(&ondisk->mtime.sec);\n+\tce->ce_stat_data.sd_ctime.nsec = get_be32(&ondisk->ctime.nsec);\n+\tce->ce_stat_data.sd_mtime.nsec = get_be32(&ondisk->mtime.nsec);\n+\tce->ce_stat_data.sd_dev   = get_be32(&ondisk->dev);\n+\tce->ce_stat_data.sd_ino   = get_be32(&ondisk->ino);\n+\tce->ce_mode  = get_be32(&ondisk->mode);\n+\tce->ce_stat_data.sd_uid   = get_be32(&ondisk->uid);\n+\tce->ce_stat_data.sd_gid   = get_be32(&ondisk->gid);\n+\tce->ce_stat_data.sd_size  = get_be32(&ondisk->size);\n \tce->ce_flags = flags & ~CE_NAMEMASK;\n \tce->ce_namelen = len;\n \thashcpy(ce->sha1, ondisk->sha1);\n@@ -1389,14 +1369,14 @@ static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk,\n \tunsigned int flags;\n \n \t/* On-disk flags are just 16 bits */\n-\tflags = ntoh_s(ondisk->flags);\n+\tflags = get_be16(&ondisk->flags);\n \tlen = flags & CE_NAMEMASK;\n \n \tif (flags & CE_EXTENDED) {\n \t\tstruct ondisk_cache_entry_extended *ondisk2;\n \t\tint extended_flags;\n \t\tondisk2 = (struct ondisk_cache_entry_extended *)ondisk;\n-\t\textended_flags = ntoh_s(ondisk2->flags2) << 16;\n+\t\textended_flags = get_be16(&ondisk2->flags2) << 16;\n \t\t/* We do not yet understand any bit out of CE_EXTENDED_FLAGS */\n \t\tif (extended_flags & ~CE_EXTENDED_FLAGS)\n \t\t\tdie(\"Unknown index entry format %08x\", extended_flags);\n-- \n1.8.5.2.500.g8060133\n"},{"id":"233644","messageId":"20140123212752.GC21705@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20140123212036.GA21299@sigill.intra.peff.net","subject":"[PATCH 3/3] ewah: support platforms that require aligned reads","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2014-01-23T21:27:52Z","receivedAt":"2014-01-23T21:27:52Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"From: Vicent Marti <tanoku@gmail.com>\n\nThe caller may hand us an unaligned buffer (e.g., because it\nis an mmap of a file with many ewah bitmaps). On some\nplatforms (like SPARC) this can cause a bus error. We can\nfix it with a combination of get_be32 and moving the data\ninto an aligned buffer (which we would do anyway, but we can\nmove it before fixing the endianness).\n\nSigned-off-by: Vicent Marti <tanoku@gmail.com>\nSigned-off-by: Jeff King <peff@peff.net>\n---\nTested on the SPARC I have access to. Please double-check that it also\nworks fine on ARM.\n\n ewah/ewah_io.c | 33 ++++++++++++++++++++++++---------\n 1 file changed, 24 insertions(+), 9 deletions(-)\n\ndiff --git a/ewah/ewah_io.c b/ewah/ewah_io.c\nindex aed0da6..4a7fae6 100644\n--- a/ewah/ewah_io.c\n+++ b/ewah/ewah_io.c\n@@ -112,23 +112,38 @@ int ewah_serialize(struct ewah_bitmap *self, int fd)\n \n int ewah_read_mmap(struct ewah_bitmap *self, void *map, size_t len)\n {\n-\tuint32_t *read32 = map;\n-\teword_t *read64;\n-\tsize_t i;\n+\tuint8_t *ptr = map;\n+\n+\tself->bit_size = get_be32(ptr);\n+\tptr += sizeof(uint32_t);\n+\n+\tself->buffer_size = self->alloc_size = get_be32(ptr);\n+\tptr += sizeof(uint32_t);\n \n-\tself->bit_size = ntohl(*read32++);\n-\tself->buffer_size = self->alloc_size = ntohl(*read32++);\n \tself->buffer = ewah_realloc(self->buffer,\n \t\tself->alloc_size * sizeof(eword_t));\n \n \tif (!self->buffer)\n \t\treturn -1;\n \n-\tfor (i = 0, read64 = (void *)read32; i < self->buffer_size; ++i)\n-\t\tself->buffer[i] = ntohll(*read64++);\n+\t/*\n+\t * Copy the raw data for the bitmap as a whole chunk;\n+\t * if we're in a little-endian platform, we'll perform\n+\t * the endianness conversion in a separate pass to ensure\n+\t * we're loading 8-byte aligned words.\n+\t */\n+\tmemcpy(self->buffer, ptr, self->buffer_size * sizeof(uint64_t));\n+\tptr += self->buffer_size * sizeof(uint64_t);\n+\n+#if __BYTE_ORDER != __BIG_ENDIAN\n+\t{\n+\t\tsize_t i;\n+\t\tfor (i = 0; i < self->buffer_size; ++i)\n+\t\t\tself->buffer[i] = ntohll(self->buffer[i]);\n+\t}\n+#endif\n \n-\tread32 = (void *)read64;\n-\tself->rlw = self->buffer + ntohl(*read32++);\n+\tself->rlw = self->buffer + get_be32(ptr);\n \n \treturn (3 * 4) + (self->buffer_size * 8);\n }\n-- \n1.8.5.2.500.g8060133\n"},{"id":"233647","messageId":"20140123215325.GA28829@vauxhall.crustytoothpaste.net","threadId":"35562","inReplyTo":"20140123202645.GA329@sigill.intra.peff.net","subject":"Re: [PATCH v4 08/23] ewah: compressed bitmap implementation","fromName":"brian m. carlson","fromEmail":"sandals@crustytoothpaste.net","sentAt":"2014-01-23T21:53:26Z","receivedAt":"2014-01-23T21:53:26Z","isPatch":true,"sender":{"key":"sandals@crustytoothpaste.net","avatar":"https://avatars.githubusercontent.com/u/497054?v=4"},"body":"On Thu, Jan 23, 2014 at 03:26:45PM -0500, Jeff King wrote:\n> Looking over the format, I think the only thing preventing 4-byte\n> alignment is the 1-byte XOR-offset and 1-byte flags field for each\n> bitmap. If we ever have a v2, we could pad the sum of those out to 4\n> bytes. Is 4-byte alignment enough? We do treat the actual data as 64-bit\n> integers. I wonder if that would have problems on Sparc64, for example.\n\nYes, it will.  SPARC requires all loads be naturally aligned (4-byte to\nan address that's a multiple of 4, 8-byte to a multiple of 8, and so\non).  In general, architectures that do not support unaligned access\nrequire natural alignment for all quantities.\n\nAlso, even on architectures where the kernel can fix these alignment\nissues up, the cost of doing so is a two context switches (in and out of\nthe kernel), servicing the trap, two loads, some shifts and rotates, and\na kernel message, so many people disable alignment fixups.  I know it\nmade things extremely slow on Alpha.  ARM is even more fun since if you\ndon't take the trap, it loads the data rotated, so the load happens, it\njust silently returns the wrong data.\n\n-- \nbrian m. carlson / brian with sandals: Houston, Texas, US\n+1 832 623 2791 | http://www.crustytoothpaste.net/~bmc | My opinion only\nOpenPGP: RSA v4 4096b: 88AC E9B2 9196 305B A994 7552 F1BA 225C 0223 B187\n"},{"id":"233648","messageId":"20140123220742.GA29357@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20140123215325.GA28829@vauxhall.crustytoothpaste.net","subject":"Re: [PATCH v4 08/23] ewah: compressed bitmap implementation","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2014-01-23T22:07:43Z","receivedAt":"2014-01-23T22:07:43Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, Jan 23, 2014 at 09:53:26PM +0000, brian m. carlson wrote:\n\n> On Thu, Jan 23, 2014 at 03:26:45PM -0500, Jeff King wrote:\n> > Looking over the format, I think the only thing preventing 4-byte\n> > alignment is the 1-byte XOR-offset and 1-byte flags field for each\n> > bitmap. If we ever have a v2, we could pad the sum of those out to 4\n> > bytes. Is 4-byte alignment enough? We do treat the actual data as 64-bit\n> > integers. I wonder if that would have problems on Sparc64, for example.\n> \n> Yes, it will.  SPARC requires all loads be naturally aligned (4-byte to\n> an address that's a multiple of 4, 8-byte to a multiple of 8, and so\n> on).  In general, architectures that do not support unaligned access\n> require natural alignment for all quantities.\n\nIn that case, I think we cannot even blame Shawn. The ewah serialization\nformat itself (which JGit inherited from the javaewah library) has 8\nbytes of header and 4 bytes of trailer. So packed serialized ewahs\nwouldn't be 8-byte aligned (though of course he could have added his own\npadding to each when we have a sequence of them).\n\n-Peff\n"},{"id":"233649","messageId":"20140123221755.GA18964@google.com","threadId":"35562","inReplyTo":"20140123220742.GA29357@sigill.intra.peff.net","subject":"Re: [PATCH v4 08/23] ewah: compressed bitmap implementation","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-01-23T22:17:55Z","receivedAt":"2014-01-23T22:17:55Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Jeff King wrote:\n> On Thu, Jan 23, 2014 at 09:53:26PM +0000, brian m. carlson wrote:\n\n>> Yes, it will.  SPARC requires all loads be naturally aligned (4-byte to\n>> an address that's a multiple of 4, 8-byte to a multiple of 8, and so\n>> on).  In general, architectures that do not support unaligned access\n>> require natural alignment for all quantities.\n>\n> In that case, I think we cannot even blame Shawn. The ewah serialization\n> format itself (which JGit inherited from the javaewah library) has 8\n> bytes of header and 4 bytes of trailer. So packed serialized ewahs\n> wouldn't be 8-byte aligned\n\nI don't think that's a big issue.  A pair of 4-byte reads would not be\ntoo slow.\n\nEven on x86, aligned reads are supposed to be faster than unaligned\nreads (though I haven't looked at benchmarks recently).\n"},{"id":"233650","messageId":"20140123222632.GA2311@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20140123221755.GA18964@google.com","subject":"Re: [PATCH v4 08/23] ewah: compressed bitmap implementation","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2014-01-23T22:26:32Z","receivedAt":"2014-01-23T22:26:32Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, Jan 23, 2014 at 02:17:55PM -0800, Jonathan Nieder wrote:\n\n> Jeff King wrote:\n> > On Thu, Jan 23, 2014 at 09:53:26PM +0000, brian m. carlson wrote:\n> \n> >> Yes, it will.  SPARC requires all loads be naturally aligned (4-byte to\n> >> an address that's a multiple of 4, 8-byte to a multiple of 8, and so\n> >> on).  In general, architectures that do not support unaligned access\n> >> require natural alignment for all quantities.\n> >\n> > In that case, I think we cannot even blame Shawn. The ewah serialization\n> > format itself (which JGit inherited from the javaewah library) has 8\n> > bytes of header and 4 bytes of trailer. So packed serialized ewahs\n> > wouldn't be 8-byte aligned\n> \n> I don't think that's a big issue.  A pair of 4-byte reads would not be\n> too slow.\n\nThe header is actually two separate 4-byte values, so that's fine. But\nbetween the header and trailer are a series of 8-byte data values, and\nthat is what we need the 8-byte alignment for. So the _first_ ewah's\ndata is 8-byte aligned, but then it offsets the alignment with a single\n4-byte trailer. So the next ewah, if they are packed in a sequence, is\nwill have its data misaligned.\n\nYou could solve it by putting an empty 4-byte pad at the end of each\newah (and of course making sure the first one is 8-byte aligned).\n\nAnyway, this is all academic until we are designing bitmap v2, which I\ndo not plan on doing anytime soon.\n\n-Peff\n"},{"id":"233651","messageId":"20140123223318.GB18964@google.com","threadId":"35562","inReplyTo":"20140123222632.GA2311@sigill.intra.peff.net","subject":"Re: [PATCH v4 08/23] ewah: compressed bitmap implementation","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-01-23T22:33:18Z","receivedAt":"2014-01-23T22:33:18Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Jeff King wrote:\n> On Thu, Jan 23, 2014 at 02:17:55PM -0800, Jonathan Nieder wrote:\n\n>> I don't think that's a big issue.  A pair of 4-byte reads would not be\n>> too slow.\n>\n> The header is actually two separate 4-byte values, so that's fine. But\n> between the header and trailer are a series of 8-byte data values, and\n> that is what we need the 8-byte alignment for.\n\nSorry for the lack of clarity.  What I meant is that a 4-byte aligned\n8-byte value can be read using a pair of 4-byte reads, which is less\nof a performance issue than a completely unaligned value.\n\n[...]\n> Anyway, this is all academic until we are designing bitmap v2, which I\n> do not plan on doing anytime soon.\n\nSure, fair enough. :)\n\nJonathan\n"},{"id":"233654","messageId":"20140123231738.GC18964@google.com","threadId":"35562","inReplyTo":"20140123212036.GA21299@sigill.intra.peff.net","subject":"Re: [PATCH v2 0/3] unaligned reads from .bitmap files","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-01-23T23:17:38Z","receivedAt":"2014-01-23T23:17:38Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Jeff King wrote:\n\n> Here it is again, fixing the issues we've discussed.\n\nThanks!  Passes all tests.\n\nTested-by: Jonathan Nieder <jrnieder@gmail.com> # ARMv5\n"},{"id":"233655","messageId":"20140123231912.GD18964@google.com","threadId":"35562","inReplyTo":"20140123212308.GA21705@sigill.intra.peff.net","subject":"Re: [PATCH 1/3] block-sha1: factor out get_be and put_be wrappers","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-01-23T23:19:12Z","receivedAt":"2014-01-23T23:19:12Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Jeff King wrote:\n\n> These short names might not be descriptive enough now that they are\n> globals. However, they make sense to me.\n\nYeah, I think they're clear.  And they match the Linux kernel's\nget_unaligned_be32() / put_unaligned_be32().\n"},{"id":"233656","messageId":"20140123233416.GE18964@google.com","threadId":"35562","inReplyTo":"20140123212642.GB21705@sigill.intra.peff.net","subject":"Re: [PATCH 2/3] read-cache: use get_be32 instead of hand-rolled ntoh_l","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-01-23T23:34:16Z","receivedAt":"2014-01-23T23:34:16Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Jeff King wrote:\n\n> This _might_ still suffer from the issue fixed in 5f6a112 (block-sha1:\n> avoid pointer conversion that violates alignment constraints,\n> 2012-07-22), as we are taking the pointer of a uint32 in a struct.\n\nNo conversion, so no issue there.\n\nLine 1484 looks more problematic:\n\n\t\tdisk_ce = (struct ondisk_cache_entry *)((char *)mmap + src_offset);\n\nIn v4 indexes, src_offset doesn't have any particular alignment so\nthis conversion has undefined behavior.\n\nDo you know if any tests exercise this code with paths that don't\nhave convenient length?\n\n[...]\n> I'm inclined to leave it for now, as we haven't made anything worse, and\n> nobody has reported a problem.\n\nYeah, agreed.\n\nProbably the simplest fix would be to take a char *, memcpy into a\nnew (aligned) buffer and then byteswap in place, but that's\northogonal to this series.\n\nThanks,\nJonathan\n"},{"id":"233657","messageId":"20140123234456.GF18964@google.com","threadId":"35562","inReplyTo":"20140123212752.GC21705@sigill.intra.peff.net","subject":"Re: [PATCH 3/3] ewah: support platforms that require aligned reads","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-01-23T23:44:56Z","receivedAt":"2014-01-23T23:44:56Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Jeff King wrote:\n\n> --- a/ewah/ewah_io.c\n> +++ b/ewah/ewah_io.c\n> @@ -112,23 +112,38 @@ int ewah_serialize(struct ewah_bitmap *self, int fd)\n[...]\n> +#if __BYTE_ORDER != __BIG_ENDIAN\n\nIs this portable?\n\nOn a platform without __BYTE_ORDER or __BIG_ENDIAN defined,\nit is interpreted as\n\n\t#if 0 != 0\n\nwhich means that such platforms are assumed to be big endian.\nDoes Mingw define __BYTE_ORDER, for example?\n\n\n> +\t{\n> +\t\tsize_t i;\n> +\t\tfor (i = 0; i < self->buffer_size; ++i)\n> +\t\t\tself->buffer[i] = ntohll(self->buffer[i]);\n> +\t}\n> +#endif\n\nIt's tempting to guard with something like\n\n\tif (ntohl(1) != 1) {\n\t\t...\n\t}\n\nThe optimizer can tell if this is true or false at compile time, so\nit shouldn't slow anything down.\n\nWith that change,\nReviewed-by: Jonathan Nieder <jrnieder@gmail.com>\n\nThanks for the quick fix.\n\ndiff --git i/ewah/ewah_io.c w/ewah/ewah_io.c\nindex 4a7fae6..5a527a4 100644\n--- i/ewah/ewah_io.c\n+++ w/ewah/ewah_io.c\n@@ -135,13 +135,11 @@ int ewah_read_mmap(struct ewah_bitmap *self, void *map, size_t len)\n \tmemcpy(self->buffer, ptr, self->buffer_size * sizeof(uint64_t));\n \tptr += self->buffer_size * sizeof(uint64_t);\n \n-#if __BYTE_ORDER != __BIG_ENDIAN\n-\t{\n+\tif (ntohl(1) != 1) {\n \t\tsize_t i;\n \t\tfor (i = 0; i < self->buffer_size; ++i)\n \t\t\tself->buffer[i] = ntohll(self->buffer[i]);\n \t}\n-#endif\n \n \tself->rlw = self->buffer + get_be32(ptr);\n \n"},{"id":"233659","messageId":"CAFFjANR=ZKmrf6QjkbiD3z1waoYjWV3eWR_1E-qJU9Nr65bFsw@mail.gmail.com","threadId":"35562","inReplyTo":"20140123234456.GF18964@google.com","subject":"Re: [PATCH 3/3] ewah: support platforms that require aligned reads","fromName":"Vicent Martí","fromEmail":"tanoku@gmail.com","sentAt":"2014-01-23T23:49:42Z","receivedAt":"2014-01-23T23:49:42Z","isPatch":true,"sender":{"key":"tanoku@gmail.com","avatar":"https://gravatar.com/avatar/271386991cb4c2b8f1e1ed1d059f3422cc3485de7a598f65043f70be021d095b?d=mp&s=160"},"body":"On Fri, Jan 24, 2014 at 12:44 AM, Jonathan Nieder <jrnieder@gmail.com> wrote:\n>> --- a/ewah/ewah_io.c\n>> +++ b/ewah/ewah_io.c\n>> @@ -112,23 +112,38 @@ int ewah_serialize(struct ewah_bitmap *self, int fd)\n> [...]\n>> +#if __BYTE_ORDER != __BIG_ENDIAN\n>\n> Is this portable?\n\nWe explicitly set the __BYTE_ORDER macros in `compat/bswap.h`. In\nfact, this preprocessor conditional is the same one that we use when\nchoosing what version of the `ntohl` macro to define, so that's why I\ndecided to use it here.\n"},{"id":"233663","messageId":"20140124001539.GG18964@google.com","threadId":"35562","inReplyTo":"CAFFjANR=ZKmrf6QjkbiD3z1waoYjWV3eWR_1E-qJU9Nr65bFsw@mail.gmail.com","subject":"Re: [PATCH 3/3] ewah: support platforms that require aligned reads","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-01-24T00:15:39Z","receivedAt":"2014-01-24T00:15:39Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"Vicent Martí wrote:\n> On Fri, Jan 24, 2014 at 12:44 AM, Jonathan Nieder <jrnieder@gmail.com> wrote:\n\n>>> +#if __BYTE_ORDER != __BIG_ENDIAN\n>>\n>> Is this portable?\n>\n> We explicitly set the __BYTE_ORDER macros in `compat/bswap.h`. In\n> fact, this preprocessor conditional is the same one that we use when\n> choosing what version of the `ntohl` macro to define, so that's why I\n> decided to use it here.\n\nAh, thanks.  Sorry I missed that.  So feel free to add my reviewed-by\nto the patch without my tweak, too.\n"},{"id":"233675","messageId":"20140124022228.GA4521@sigill.intra.peff.net","threadId":"35562","inReplyTo":"20140123233416.GE18964@google.com","subject":"Re: [PATCH 2/3] read-cache: use get_be32 instead of hand-rolled ntoh_l","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2014-01-24T02:22:28Z","receivedAt":"2014-01-24T02:22:28Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, Jan 23, 2014 at 03:34:16PM -0800, Jonathan Nieder wrote:\n\n> Line 1484 looks more problematic:\n> \n> \t\tdisk_ce = (struct ondisk_cache_entry *)((char *)mmap + src_offset);\n> \n> In v4 indexes, src_offset doesn't have any particular alignment so\n> this conversion has undefined behavior.\n> \n> Do you know if any tests exercise this code with paths that don't\n> have convenient length?\n\nMy impression was that we are not testing v4 index at all (and grepping\nfor `--index-version`, which I think is the only way to write it,\nsupports that).\n\n-Peff\n"}]}