midx: simplify computation of pack name lengths

Before writing the multi-pack-index, we compute the length of the pack-index names concatenated together. This forms the data in the pack name chunk, and we precompute it to compute chunk offsets. The value is also modified to fit alignment needs. Previously, this computation was coupled with adding packs from the existing multi-pack-index and the remaining packs in the object dir not already covered by the multi-pack-index. In anticipation of this becoming more complicated with the 'expire' subcommand, simplify the computation by centralizing it to a single loop before writing the file. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Jun 10, 2019 at 16:35 UTC dba6175c64cdc9485198395b4c3c715fd2402230
1 file changed +9 -9
midx.c
+9 -9
@@ -433,7 +433,6 @@ struct pack_list {
433 uint32_t nr;
434 uint32_t alloc_list;
435 uint32_t alloc_names;
436 - size_t pack_name_concat_len;
436 struct multi_pack_index *m;
437 };
438
@@ -468,7 +467,6 @@ static void add_pack_to_midx(const char *full_path, size_t full_path_len,
467 }
468
469 packs->names[packs->nr] = xstrdup(file_name);
471 - packs->pack_name_concat_len += strlen(file_name) + 1;
470 packs->nr++;
471 }
472 }
@@ -812,6 +810,7 @@ int write_midx_file(const char *object_dir)
810 uint32_t nr_entries, num_large_offsets = 0;
811 struct pack_midx_entry *entries = NULL;
812 int large_offsets_needed = 0;
813 + int pack_name_concat_len = 0;
814
815 midx_name = get_midx_filename(object_dir);
816 if (safe_create_leading_directories(midx_name)) {
@@ -827,7 +826,6 @@ int write_midx_file(const char *object_dir)
826 packs.alloc_names = packs.alloc_list;
827 packs.list = NULL;
828 packs.names = NULL;
830 - packs.pack_name_concat_len = 0;
829 ALLOC_ARRAY(packs.list, packs.alloc_list);
830 ALLOC_ARRAY(packs.names, packs.alloc_names);
831
@@ -838,7 +836,6 @@ int write_midx_file(const char *object_dir)
836
837 packs.list[packs.nr] = NULL;
838 packs.names[packs.nr] = xstrdup(packs.m->pack_names[i]);
841 - packs.pack_name_concat_len += strlen(packs.names[packs.nr]) + 1;
839 packs.nr++;
840 }
841 }
@@ -848,10 +845,6 @@ int write_midx_file(const char *object_dir)
845 if (packs.m && packs.nr == packs.m->num_packs)
846 goto cleanup;
847
851 - if (packs.pack_name_concat_len % MIDX_CHUNK_ALIGNMENT)
852 - packs.pack_name_concat_len += MIDX_CHUNK_ALIGNMENT -
853 - (packs.pack_name_concat_len % MIDX_CHUNK_ALIGNMENT);
854 -
848 ALLOC_ARRAY(pack_perm, packs.nr);
849 sort_packs_by_name(packs.names, packs.nr, pack_perm);
850
@@ -864,6 +857,13 @@ int write_midx_file(const char *object_dir)
857 large_offsets_needed = 1;
858 }
859
860 + for (i = 0; i < packs.nr; i++)
861 + pack_name_concat_len += strlen(packs.names[i]) + 1;
862 +
863 + if (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT)
864 + pack_name_concat_len += MIDX_CHUNK_ALIGNMENT -
865 + (pack_name_concat_len % MIDX_CHUNK_ALIGNMENT);
866 +
867 hold_lock_file_for_update(&lk, midx_name, LOCK_DIE_ON_ERROR);
868 f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
869 FREE_AND_NULL(midx_name);
@@ -881,7 +881,7 @@ int write_midx_file(const char *object_dir)
881
882 cur_chunk++;
883 chunk_ids[cur_chunk] = MIDX_CHUNKID_OIDFANOUT;
884 - chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + packs.pack_name_concat_len;
884 + chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + pack_name_concat_len;
885
886 cur_chunk++;
887 chunk_ids[cur_chunk] = MIDX_CHUNKID_OIDLOOKUP;