Skip to content

Commit 94830fc

Browse files
ttaylorrgitster
authored andcommitted
pack-bitmap-write.c: move commit_positions into commit_pos fields
In 7cc8f97 (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b174a97 commit 94830fc

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

pack-bitmap-write.c

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -670,9 +670,7 @@ static const struct object_id *oid_access(size_t pos, const void *table)
670670
return &index[pos]->oid;
671671
}
672672

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)
676674
{
677675
int i;
678676

@@ -682,31 +680,28 @@ static void write_selected_commits_v1(struct hashfile *f,
682680
if (offsets)
683681
offsets[i] = hashfile_total(f);
684682

685-
hashwrite_be32(f, commit_positions[i]);
683+
hashwrite_be32(f, stored->commit_pos);
686684
hashwrite_u8(f, stored->xor_offset);
687685
hashwrite_u8(f, stored->flags);
688686

689687
dump_bitmap(f, stored->write_as);
690688
}
691689
}
692690

693-
static int table_cmp(const void *_va, const void *_vb, void *_data)
691+
static int table_cmp(const void *_va, const void *_vb)
694692
{
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];
698695

699-
if (a > b)
700-
return 1;
701-
else if (a < b)
696+
if (a->commit_pos < b->commit_pos)
702697
return -1;
698+
else if (a->commit_pos > b->commit_pos)
699+
return 1;
703700

704701
return 0;
705702
}
706703

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)
710705
{
711706
uint32_t i;
712707
uint32_t *table, *table_inv;
@@ -722,7 +717,7 @@ static void write_lookup_table(struct hashfile *f,
722717
* bitmap corresponds to j'th bitmapped commit (among the selected
723718
* commits) in lex order of OIDs.
724719
*/
725-
QSORT_S(table, writer.selected_nr, table_cmp, commit_positions);
720+
QSORT(table, writer.selected_nr, table_cmp);
726721

727722
/* table_inv helps us discover that relationship (i'th bitmap
728723
* to j'th commit by j = table_inv[i])
@@ -753,7 +748,7 @@ static void write_lookup_table(struct hashfile *f,
753748
xor_row = 0xffffffff;
754749
}
755750

756-
hashwrite_be32(f, commit_positions[table[i]]);
751+
hashwrite_be32(f, writer.selected[table[i]].commit_pos);
757752
hashwrite_be64(f, (uint64_t)offsets[table[i]]);
758753
hashwrite_be32(f, xor_row);
759754
}
@@ -789,7 +784,6 @@ void bitmap_writer_finish(struct pack_idx_entry **index,
789784
static uint16_t flags = BITMAP_OPT_FULL_DAG;
790785
struct strbuf tmp_file = STRBUF_INIT;
791786
struct hashfile *f;
792-
uint32_t *commit_positions = NULL;
793787
off_t *offsets = NULL;
794788
uint32_t i;
795789

@@ -814,22 +808,20 @@ void bitmap_writer_finish(struct pack_idx_entry **index,
814808
if (options & BITMAP_OPT_LOOKUP_TABLE)
815809
CALLOC_ARRAY(offsets, index_nr);
816810

817-
ALLOC_ARRAY(commit_positions, writer.selected_nr);
818-
819811
for (i = 0; i < writer.selected_nr; i++) {
820812
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);
822815

823816
if (commit_pos < 0)
824817
BUG(_("trying to write commit not in index"));
825-
826-
commit_positions[i] = commit_pos;
818+
stored->commit_pos = commit_pos;
827819
}
828820

829-
write_selected_commits_v1(f, commit_positions, offsets);
821+
write_selected_commits_v1(f, offsets);
830822

831823
if (options & BITMAP_OPT_LOOKUP_TABLE)
832-
write_lookup_table(f, commit_positions, offsets);
824+
write_lookup_table(f, offsets);
833825

834826
if (options & BITMAP_OPT_HASH_CACHE)
835827
write_hash_cache(f, index, index_nr);
@@ -844,6 +836,5 @@ void bitmap_writer_finish(struct pack_idx_entry **index,
844836
die_errno("unable to rename temporary bitmap file to '%s'", filename);
845837

846838
strbuf_release(&tmp_file);
847-
free(commit_positions);
848839
free(offsets);
849840
}

0 commit comments

Comments
 (0)