pack-bitmap-write.c: move commit_positions into commit_pos fields

In 7cc8f971085 (pack-objects: implement bitmap writing, 2013-12-21), the bitmapped_commit struct was introduced, including the 'commit_pos' field, which has been unused ever since its introduction more than a decade ago. Instead, we have used the nearby `commit_positions` array leaving the bitmapped_commit struct with an unused 4-byte field. We could drop the `commit_pos` field as unused, and continue to store the values in the auxiliary array. But we could also drop the array and store the data for each bitmapped_commit struct inside of the structure itself, which is what this patch does. In any spot that we previously read `commit_positions[i]`, we can now instead read `writer.selected[i].commit_pos`. There are a few spots that need changing as a result: - write_selected_commits_v1() is a simple transformation, since we're just reading the field. As a result, the function no longer needs an explicit argument to pass the commit_positions array. - write_lookup_table() also no longer needs the explicit commit_positions array passed in as an argument. But it still needs to sort an array of indices into the writer.selected array to read them in commit_pos order, so table_cmp() is adjusted accordingly. - bitmap_writer_finish() no longer needs to allocate, populate, and free the commit_positions table. Instead, we can just write the data directly into each struct bitmapped_commit. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed May 14, 2024 at 15:56 UTC 94830fcaccebbe48916c22a1a773e5b25a366c61
1 file changed +16 -25
pack-bitmap-write.c
+16 -25
@@ -670,9 +670,7 @@ static const struct object_id *oid_access(size_t pos, const void *table)
670 return &index[pos]->oid;
671 }
672
673 -static void write_selected_commits_v1(struct hashfile *f,
674 - uint32_t *commit_positions,
675 - off_t *offsets)
673 +static void write_selected_commits_v1(struct hashfile *f, off_t *offsets)
674 {
675 int i;
676
@@ -682,7 +680,7 @@ static void write_selected_commits_v1(struct hashfile *f,
680 if (offsets)
681 offsets[i] = hashfile_total(f);
682
685 - hashwrite_be32(f, commit_positions[i]);
683 + hashwrite_be32(f, stored->commit_pos);
684 hashwrite_u8(f, stored->xor_offset);
685 hashwrite_u8(f, stored->flags);
686
@@ -690,23 +688,20 @@ static void write_selected_commits_v1(struct hashfile *f,
688 }
689 }
690
693 -static int table_cmp(const void *_va, const void *_vb, void *_data)
691 +static int table_cmp(const void *_va, const void *_vb)
692 {
695 - uint32_t *commit_positions = _data;
696 - uint32_t a = commit_positions[*(uint32_t *)_va];
697 - uint32_t b = commit_positions[*(uint32_t *)_vb];
693 + struct bitmapped_commit *a = &writer.selected[*(uint32_t *)_va];
694 + struct bitmapped_commit *b = &writer.selected[*(uint32_t *)_vb];
695
699 - if (a > b)
700 - return 1;
701 - else if (a < b)
696 + if (a->commit_pos < b->commit_pos)
697 return -1;
698 + else if (a->commit_pos > b->commit_pos)
699 + return 1;
700
701 return 0;
702 }
703
707 -static void write_lookup_table(struct hashfile *f,
708 - uint32_t *commit_positions,
709 - off_t *offsets)
704 +static void write_lookup_table(struct hashfile *f, off_t *offsets)
705 {
706 uint32_t i;
707 uint32_t *table, *table_inv;
@@ -722,7 +717,7 @@ static void write_lookup_table(struct hashfile *f,
717 * bitmap corresponds to j'th bitmapped commit (among the selected
718 * commits) in lex order of OIDs.
719 */
725 - QSORT_S(table, writer.selected_nr, table_cmp, commit_positions);
720 + QSORT(table, writer.selected_nr, table_cmp);
721
722 /* table_inv helps us discover that relationship (i'th bitmap
723 * to j'th commit by j = table_inv[i])
@@ -753,7 +748,7 @@ static void write_lookup_table(struct hashfile *f,
748 xor_row = 0xffffffff;
749 }
750
756 - hashwrite_be32(f, commit_positions[table[i]]);
751 + hashwrite_be32(f, writer.selected[table[i]].commit_pos);
752 hashwrite_be64(f, (uint64_t)offsets[table[i]]);
753 hashwrite_be32(f, xor_row);
754 }
@@ -789,7 +784,6 @@ void bitmap_writer_finish(struct pack_idx_entry **index,
784 static uint16_t flags = BITMAP_OPT_FULL_DAG;
785 struct strbuf tmp_file = STRBUF_INIT;
786 struct hashfile *f;
792 - uint32_t *commit_positions = NULL;
787 off_t *offsets = NULL;
788 uint32_t i;
789
@@ -814,22 +808,20 @@ void bitmap_writer_finish(struct pack_idx_entry **index,
808 if (options & BITMAP_OPT_LOOKUP_TABLE)
809 CALLOC_ARRAY(offsets, index_nr);
810
817 - ALLOC_ARRAY(commit_positions, writer.selected_nr);
818 -
811 for (i = 0; i < writer.selected_nr; i++) {
812 struct bitmapped_commit *stored = &writer.selected[i];
821 - int commit_pos = oid_pos(&stored->commit->object.oid, index, index_nr, oid_access);
813 + int commit_pos = oid_pos(&stored->commit->object.oid, index,
814 + index_nr, oid_access);
815
816 if (commit_pos < 0)
817 BUG(_("trying to write commit not in index"));
825 -
826 - commit_positions[i] = commit_pos;
818 + stored->commit_pos = commit_pos;
819 }
820
829 - write_selected_commits_v1(f, commit_positions, offsets);
821 + write_selected_commits_v1(f, offsets);
822
823 if (options & BITMAP_OPT_LOOKUP_TABLE)
832 - write_lookup_table(f, commit_positions, offsets);
824 + write_lookup_table(f, offsets);
825
826 if (options & BITMAP_OPT_HASH_CACHE)
827 write_hash_cache(f, index, index_nr);
@@ -844,6 +836,5 @@ void bitmap_writer_finish(struct pack_idx_entry **index,
836 die_errno("unable to rename temporary bitmap file to '%s'", filename);
837
838 strbuf_release(&tmp_file);
847 - free(commit_positions);
839 free(offsets);
840 }