summaryrefslogtreecommitdiff
path: root/pack-bitmap.c
diff options
context:
space:
mode:
Diffstat (limited to 'pack-bitmap.c')
-rw-r--r--pack-bitmap.c192
1 files changed, 129 insertions, 63 deletions
diff --git a/pack-bitmap.c b/pack-bitmap.c
index e07c798879..5a8689cdf8 100644
--- a/pack-bitmap.c
+++ b/pack-bitmap.c
@@ -326,6 +326,13 @@ failed:
munmap(bitmap_git->map, bitmap_git->map_size);
bitmap_git->map = NULL;
bitmap_git->map_size = 0;
+
+ kh_destroy_oid_map(bitmap_git->bitmaps);
+ bitmap_git->bitmaps = NULL;
+
+ kh_destroy_oid_pos(bitmap_git->ext_index.positions);
+ bitmap_git->ext_index.positions = NULL;
+
return -1;
}
@@ -622,7 +629,7 @@ static void show_objects_for_type(
enum object_type object_type,
show_reachable_fn show_reach)
{
- size_t pos = 0, i = 0;
+ size_t i = 0;
uint32_t offset;
struct ewah_iterator it;
@@ -630,13 +637,15 @@ static void show_objects_for_type(
struct bitmap *objects = bitmap_git->result;
- if (bitmap_git->reuse_objects == bitmap_git->pack->num_objects)
- return;
-
ewah_iterator_init(&it, type_filter);
- while (i < objects->word_alloc && ewah_iterator_next(&filter, &it)) {
+ for (i = 0; i < objects->word_alloc &&
+ ewah_iterator_next(&filter, &it); i++) {
eword_t word = objects->words[i] & filter;
+ size_t pos = (i * BITS_IN_EWORD);
+
+ if (!word)
+ continue;
for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
struct object_id oid;
@@ -648,9 +657,6 @@ static void show_objects_for_type(
offset += ewah_bit_ctz64(word >> offset);
- if (pos + offset < bitmap_git->reuse_objects)
- continue;
-
entry = &bitmap_git->pack->revindex[pos + offset];
nth_packed_object_oid(&oid, bitmap_git->pack, entry->nr);
@@ -659,9 +665,6 @@ static void show_objects_for_type(
show_reach(&oid, object_type, 0, hash, bitmap_git->pack, entry->offset);
}
-
- pos += BITS_IN_EWORD;
- i++;
}
}
@@ -768,66 +771,139 @@ cleanup:
return NULL;
}
-int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
- struct packed_git **packfile,
- uint32_t *entries,
- off_t *up_to)
+static void try_partial_reuse(struct bitmap_index *bitmap_git,
+ size_t pos,
+ struct bitmap *reuse,
+ struct pack_window **w_curs)
{
+ struct revindex_entry *revidx;
+ off_t offset;
+ enum object_type type;
+ unsigned long size;
+
+ if (pos >= bitmap_git->pack->num_objects)
+ return; /* not actually in the pack */
+
+ revidx = &bitmap_git->pack->revindex[pos];
+ offset = revidx->offset;
+ type = unpack_object_header(bitmap_git->pack, w_curs, &offset, &size);
+ if (type < 0)
+ return; /* broken packfile, punt */
+
+ if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA) {
+ off_t base_offset;
+ int base_pos;
+
+ /*
+ * Find the position of the base object so we can look it up
+ * in our bitmaps. If we can't come up with an offset, or if
+ * that offset is not in the revidx, the pack is corrupt.
+ * There's nothing we can do, so just punt on this object,
+ * and the normal slow path will complain about it in
+ * more detail.
+ */
+ base_offset = get_delta_base(bitmap_git->pack, w_curs,
+ &offset, type, revidx->offset);
+ if (!base_offset)
+ return;
+ base_pos = find_revindex_position(bitmap_git->pack, base_offset);
+ if (base_pos < 0)
+ return;
+
+ /*
+ * We assume delta dependencies always point backwards. This
+ * lets us do a single pass, and is basically always true
+ * due to the way OFS_DELTAs work. You would not typically
+ * find REF_DELTA in a bitmapped pack, since we only bitmap
+ * packs we write fresh, and OFS_DELTA is the default). But
+ * let's double check to make sure the pack wasn't written with
+ * odd parameters.
+ */
+ if (base_pos >= pos)
+ return;
+
+ /*
+ * And finally, if we're not sending the base as part of our
+ * reuse chunk, then don't send this object either. The base
+ * would come after us, along with other objects not
+ * necessarily in the pack, which means we'd need to convert
+ * to REF_DELTA on the fly. Better to just let the normal
+ * object_entry code path handle it.
+ */
+ if (!bitmap_get(reuse, base_pos))
+ return;
+ }
+
/*
- * Reuse the packfile content if we need more than
- * 90% of its objects
+ * If we got here, then the object is OK to reuse. Mark it.
*/
- static const double REUSE_PERCENT = 0.9;
+ bitmap_set(reuse, pos);
+}
+int reuse_partial_packfile_from_bitmap(struct bitmap_index *bitmap_git,
+ struct packed_git **packfile_out,
+ uint32_t *entries,
+ struct bitmap **reuse_out)
+{
struct bitmap *result = bitmap_git->result;
- uint32_t reuse_threshold;
- uint32_t i, reuse_objects = 0;
+ struct bitmap *reuse;
+ struct pack_window *w_curs = NULL;
+ size_t i = 0;
+ uint32_t offset;
assert(result);
- for (i = 0; i < result->word_alloc; ++i) {
- if (result->words[i] != (eword_t)~0) {
- reuse_objects += ewah_bit_ctz64(~result->words[i]);
- break;
- }
+ while (i < result->word_alloc && result->words[i] == (eword_t)~0)
+ i++;
- reuse_objects += BITS_IN_EWORD;
- }
+ /* Don't mark objects not in the packfile */
+ if (i > bitmap_git->pack->num_objects / BITS_IN_EWORD)
+ i = bitmap_git->pack->num_objects / BITS_IN_EWORD;
-#ifdef GIT_BITMAP_DEBUG
- {
- const unsigned char *sha1;
- struct revindex_entry *entry;
+ reuse = bitmap_word_alloc(i);
+ memset(reuse->words, 0xFF, i * sizeof(eword_t));
- entry = &bitmap_git->reverse_index->revindex[reuse_objects];
- sha1 = nth_packed_object_sha1(bitmap_git->pack, entry->nr);
+ for (; i < result->word_alloc; ++i) {
+ eword_t word = result->words[i];
+ size_t pos = (i * BITS_IN_EWORD);
+
+ for (offset = 0; offset < BITS_IN_EWORD; ++offset) {
+ if ((word >> offset) == 0)
+ break;
- fprintf(stderr, "Failed to reuse at %d (%016llx)\n",
- reuse_objects, result->words[i]);
- fprintf(stderr, " %s\n", hash_to_hex(sha1));
+ offset += ewah_bit_ctz64(word >> offset);
+ try_partial_reuse(bitmap_git, pos + offset, reuse, &w_curs);
+ }
}
-#endif
- if (!reuse_objects)
- return -1;
+ unuse_pack(&w_curs);
- if (reuse_objects >= bitmap_git->pack->num_objects) {
- bitmap_git->reuse_objects = *entries = bitmap_git->pack->num_objects;
- *up_to = -1; /* reuse the full pack */
- *packfile = bitmap_git->pack;
- return 0;
+ *entries = bitmap_popcount(reuse);
+ if (!*entries) {
+ bitmap_free(reuse);
+ return -1;
}
- reuse_threshold = bitmap_popcount(bitmap_git->result) * REUSE_PERCENT;
+ /*
+ * Drop any reused objects from the result, since they will not
+ * need to be handled separately.
+ */
+ bitmap_and_not(result, reuse);
+ *packfile_out = bitmap_git->pack;
+ *reuse_out = reuse;
+ return 0;
+}
- if (reuse_objects < reuse_threshold)
- return -1;
+int bitmap_walk_contains(struct bitmap_index *bitmap_git,
+ struct bitmap *bitmap, const struct object_id *oid)
+{
+ int idx;
- bitmap_git->reuse_objects = *entries = reuse_objects;
- *up_to = bitmap_git->pack->revindex[reuse_objects].offset;
- *packfile = bitmap_git->pack;
+ if (!bitmap)
+ return 0;
- return 0;
+ idx = bitmap_position(bitmap_git, oid);
+ return idx >= 0 && bitmap_get(bitmap, idx);
}
void traverse_bitmap_commit_list(struct bitmap_index *bitmap_git,
@@ -1118,16 +1194,6 @@ void free_bitmap_index(struct bitmap_index *b)
int bitmap_has_oid_in_uninteresting(struct bitmap_index *bitmap_git,
const struct object_id *oid)
{
- int pos;
-
- if (!bitmap_git)
- return 0; /* no bitmap loaded */
- if (!bitmap_git->haves)
- return 0; /* walk had no "haves" */
-
- pos = bitmap_position_packfile(bitmap_git, oid);
- if (pos < 0)
- return 0;
-
- return bitmap_get(bitmap_git->haves, pos);
+ return bitmap_git &&
+ bitmap_walk_contains(bitmap_git, bitmap_git->haves, oid);
}