]> git.ipfire.org Git - thirdparty/git.git/commitdiff
pack-bitmap: move some initialization to `bitmap_writer_init()`
authorTaylor Blau <me@ttaylorr.com>
Thu, 23 May 2024 21:26:23 +0000 (17:26 -0400)
committerJunio C Hamano <gitster@pobox.com>
Fri, 24 May 2024 18:40:41 +0000 (11:40 -0700)
The pack-bitmap-writer machinery uses a oidmap (backed by khash.h) to
map from commits selected for bitmaps (by OID) to a bitmapped_commit
structure (containing the bitmap itself, among other things like its XOR
offset, etc.)

This map was initialized at the end of `bitmap_writer_build()`. New
entries are added in `pack-bitmap-write.c::store_selected()`, which is
called by the bitmap_builder machinery (which is responsible for
traversing history and generating the actual bitmaps).

Reorganize when this field is initialized and when entries are added to
it so that we can quickly determine whether a commit is a candidate for
pseudo-merge selection, or not (since it was already selected to receive
a bitmap, and thus storing it in a pseudo-merge would be redundant).

The changes are as follows:

  - Introduce a new `bitmap_writer_init()` function which initializes
    the `writer.bitmaps` field (instead of waiting until the end of
    `bitmap_writer_build()`).

  - Add map entries in `push_bitmapped_commit()` (which is called via
    `bitmap_writer_select_commits()`) with OID keys and NULL values to
    track whether or not we *expect* to write a bitmap for some given
    commit.

  - Validate that a NULL entry is found matching the given key when we
    store a selected bitmap.

Signed-off-by: Taylor Blau <me@ttaylorr.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/pack-objects.c
midx-write.c
pack-bitmap-write.c
pack-bitmap.h

index 26a6d0d79194423fa0e5abc5938f0ea8c654597b..6209264e60c65d395c84038e0d84f12a0296bafc 100644 (file)
@@ -1340,7 +1340,8 @@ static void write_pack_file(void)
                                    hash_to_hex(hash));
 
                        if (write_bitmap_index) {
-                               bitmap_writer_init(&bitmap_writer);
+                               bitmap_writer_init(&bitmap_writer,
+                                                  the_repository);
                                bitmap_writer_set_checksum(&bitmap_writer, hash);
                                bitmap_writer_build_type_index(&bitmap_writer,
                                        &to_pack, written_list, nr_written);
index 7c0c08c64b223e4ecc6e0c668fe0874cdbecd8a3..c747d1a6af33d7e92ae52f10c5d262ed24371deb 100644 (file)
@@ -820,7 +820,7 @@ static int write_midx_bitmap(const char *midx_name,
        for (i = 0; i < pdata->nr_objects; i++)
                index[i] = &pdata->objects[i].idx;
 
-       bitmap_writer_init(&writer);
+       bitmap_writer_init(&writer, the_repository);
        bitmap_writer_show_progress(&writer, flags & MIDX_PROGRESS);
        bitmap_writer_build_type_index(&writer, pdata, index,
                                       pdata->nr_objects);
index 6cae670412c1e3103f480b6c6fc7c9745adcc519..d8870155831f7092edb926b309c8c4be7933a4e7 100644 (file)
@@ -27,9 +27,12 @@ struct bitmapped_commit {
        uint32_t commit_pos;
 };
 
-void bitmap_writer_init(struct bitmap_writer *writer)
+void bitmap_writer_init(struct bitmap_writer *writer, struct repository *r)
 {
        memset(writer, 0, sizeof(struct bitmap_writer));
+       if (writer->bitmaps)
+               BUG("bitmap writer already initialized");
+       writer->bitmaps = kh_init_oid_map();
 }
 
 void bitmap_writer_free(struct bitmap_writer *writer)
@@ -128,11 +131,21 @@ void bitmap_writer_build_type_index(struct bitmap_writer *writer,
 static inline void push_bitmapped_commit(struct bitmap_writer *writer,
                                         struct commit *commit)
 {
+       int hash_ret;
+       khiter_t hash_pos;
+
        if (writer->selected_nr >= writer->selected_alloc) {
                writer->selected_alloc = (writer->selected_alloc + 32) * 2;
                REALLOC_ARRAY(writer->selected, writer->selected_alloc);
        }
 
+       hash_pos = kh_put_oid_map(writer->bitmaps, commit->object.oid,
+                                 &hash_ret);
+       if (!hash_ret)
+               die(_("duplicate entry when writing bitmap index: %s"),
+                   oid_to_hex(&commit->object.oid));
+       kh_value(writer->bitmaps, hash_pos) = NULL;
+
        writer->selected[writer->selected_nr].commit = commit;
        writer->selected[writer->selected_nr].bitmap = NULL;
        writer->selected[writer->selected_nr].write_as = NULL;
@@ -483,14 +496,14 @@ static void store_selected(struct bitmap_writer *writer,
 {
        struct bitmapped_commit *stored = &writer->selected[ent->idx];
        khiter_t hash_pos;
-       int hash_ret;
 
        stored->bitmap = bitmap_to_ewah(ent->bitmap);
 
-       hash_pos = kh_put_oid_map(writer->bitmaps, commit->object.oid, &hash_ret);
-       if (hash_ret == 0)
-               die("Duplicate entry when writing index: %s",
+       hash_pos = kh_get_oid_map(writer->bitmaps, commit->object.oid);
+       if (hash_pos == kh_end(writer->bitmaps))
+               die(_("attempted to store non-selected commit: '%s'"),
                    oid_to_hex(&commit->object.oid));
+
        kh_value(writer->bitmaps, hash_pos) = stored;
 }
 
@@ -506,7 +519,6 @@ int bitmap_writer_build(struct bitmap_writer *writer,
        uint32_t *mapping;
        int closed = 1; /* until proven otherwise */
 
-       writer->bitmaps = kh_init_oid_map();
        writer->to_pack = to_pack;
 
        if (writer->show_progress)
index 3091095f3368c539218fec40aebf77570965e30b..f87e60153ddbe70ab51d47d49bba2acfc52638b4 100644 (file)
@@ -114,7 +114,7 @@ struct bitmap_writer {
        unsigned char pack_checksum[GIT_MAX_RAWSZ];
 };
 
-void bitmap_writer_init(struct bitmap_writer *writer);
+void bitmap_writer_init(struct bitmap_writer *writer, struct repository *r);
 void bitmap_writer_show_progress(struct bitmap_writer *writer, int show);
 void bitmap_writer_set_checksum(struct bitmap_writer *writer,
                                const unsigned char *sha1);