Skip to content

Commit e5ace71

Browse files
committed
Merge branch 'jk/oid-array-cleanup'
Code clean-up. * jk/oid-array-cleanup: commit-graph: use size_t for array allocation and indexing commit-graph: replace packed_oid_list with oid_array commit-graph: drop count_distinct_commits() function oid-array: provide a for-loop iterator oid-array: make sort function public cache.h: move hash/oid functions to hash.h t0064: make duplicate tests more robust t0064: drop sha1 mention from filename oid-array.h: drop sha1 mention from header guard
2 parents 21127fa + 3361390 commit e5ace71

File tree

6 files changed

+157
-199
lines changed

6 files changed

+157
-199
lines changed

cache.h

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,100 +1123,6 @@ const char *repo_find_unique_abbrev(struct repository *r, const struct object_id
11231123
int repo_find_unique_abbrev_r(struct repository *r, char *hex, const struct object_id *oid, int len);
11241124
#define find_unique_abbrev_r(hex, oid, len) repo_find_unique_abbrev_r(the_repository, hex, oid, len)
11251125

1126-
extern const struct object_id null_oid;
1127-
1128-
static inline int hashcmp(const unsigned char *sha1, const unsigned char *sha2)
1129-
{
1130-
/*
1131-
* Teach the compiler that there are only two possibilities of hash size
1132-
* here, so that it can optimize for this case as much as possible.
1133-
*/
1134-
if (the_hash_algo->rawsz == GIT_MAX_RAWSZ)
1135-
return memcmp(sha1, sha2, GIT_MAX_RAWSZ);
1136-
return memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
1137-
}
1138-
1139-
static inline int oidcmp(const struct object_id *oid1, const struct object_id *oid2)
1140-
{
1141-
return hashcmp(oid1->hash, oid2->hash);
1142-
}
1143-
1144-
static inline int hasheq(const unsigned char *sha1, const unsigned char *sha2)
1145-
{
1146-
/*
1147-
* We write this here instead of deferring to hashcmp so that the
1148-
* compiler can properly inline it and avoid calling memcmp.
1149-
*/
1150-
if (the_hash_algo->rawsz == GIT_MAX_RAWSZ)
1151-
return !memcmp(sha1, sha2, GIT_MAX_RAWSZ);
1152-
return !memcmp(sha1, sha2, GIT_SHA1_RAWSZ);
1153-
}
1154-
1155-
static inline int oideq(const struct object_id *oid1, const struct object_id *oid2)
1156-
{
1157-
return hasheq(oid1->hash, oid2->hash);
1158-
}
1159-
1160-
static inline int is_null_oid(const struct object_id *oid)
1161-
{
1162-
return oideq(oid, &null_oid);
1163-
}
1164-
1165-
static inline void hashcpy(unsigned char *sha_dst, const unsigned char *sha_src)
1166-
{
1167-
memcpy(sha_dst, sha_src, the_hash_algo->rawsz);
1168-
}
1169-
1170-
static inline void oidcpy(struct object_id *dst, const struct object_id *src)
1171-
{
1172-
memcpy(dst->hash, src->hash, GIT_MAX_RAWSZ);
1173-
}
1174-
1175-
static inline struct object_id *oiddup(const struct object_id *src)
1176-
{
1177-
struct object_id *dst = xmalloc(sizeof(struct object_id));
1178-
oidcpy(dst, src);
1179-
return dst;
1180-
}
1181-
1182-
static inline void hashclr(unsigned char *hash)
1183-
{
1184-
memset(hash, 0, the_hash_algo->rawsz);
1185-
}
1186-
1187-
static inline void oidclr(struct object_id *oid)
1188-
{
1189-
memset(oid->hash, 0, GIT_MAX_RAWSZ);
1190-
}
1191-
1192-
static inline void oidread(struct object_id *oid, const unsigned char *hash)
1193-
{
1194-
memcpy(oid->hash, hash, the_hash_algo->rawsz);
1195-
}
1196-
1197-
static inline int is_empty_blob_sha1(const unsigned char *sha1)
1198-
{
1199-
return hasheq(sha1, the_hash_algo->empty_blob->hash);
1200-
}
1201-
1202-
static inline int is_empty_blob_oid(const struct object_id *oid)
1203-
{
1204-
return oideq(oid, the_hash_algo->empty_blob);
1205-
}
1206-
1207-
static inline int is_empty_tree_sha1(const unsigned char *sha1)
1208-
{
1209-
return hasheq(sha1, the_hash_algo->empty_tree->hash);
1210-
}
1211-
1212-
static inline int is_empty_tree_oid(const struct object_id *oid)
1213-
{
1214-
return oideq(oid, the_hash_algo->empty_tree);
1215-
}
1216-
1217-
const char *empty_tree_oid_hex(void);
1218-
const char *empty_blob_oid_hex(void);
1219-
12201126
/* set default permissions by passing mode arguments to open(2) */
12211127
int git_mkstemps_mode(char *pattern, int suffix_len, int mode);
12221128
int git_mkstemp_mode(char *pattern, int mode);

commit-graph.c

Lines changed: 18 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -932,21 +932,15 @@ struct tree *get_commit_tree_in_graph(struct repository *r, const struct commit
932932

933933
struct packed_commit_list {
934934
struct commit **list;
935-
int nr;
936-
int alloc;
937-
};
938-
939-
struct packed_oid_list {
940-
struct object_id *list;
941-
int nr;
942-
int alloc;
935+
size_t nr;
936+
size_t alloc;
943937
};
944938

945939
struct write_commit_graph_context {
946940
struct repository *r;
947941
struct object_directory *odb;
948942
char *graph_name;
949-
struct packed_oid_list oids;
943+
struct oid_array oids;
950944
struct packed_commit_list commits;
951945
int num_extra_edges;
952946
unsigned long approx_nr_objects;
@@ -1240,13 +1234,6 @@ static int write_graph_chunk_bloom_data(struct hashfile *f,
12401234
return 0;
12411235
}
12421236

1243-
static int oid_compare(const void *_a, const void *_b)
1244-
{
1245-
const struct object_id *a = (const struct object_id *)_a;
1246-
const struct object_id *b = (const struct object_id *)_b;
1247-
return oidcmp(a, b);
1248-
}
1249-
12501237
static int add_packed_commits(const struct object_id *oid,
12511238
struct packed_git *pack,
12521239
uint32_t pos,
@@ -1267,10 +1254,7 @@ static int add_packed_commits(const struct object_id *oid,
12671254
if (type != OBJ_COMMIT)
12681255
return 0;
12691256

1270-
ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1271-
oidcpy(&(ctx->oids.list[ctx->oids.nr]), oid);
1272-
ctx->oids.nr++;
1273-
1257+
oid_array_append(&ctx->oids, oid);
12741258
set_commit_pos(ctx->r, oid);
12751259

12761260
return 0;
@@ -1281,9 +1265,7 @@ static void add_missing_parents(struct write_commit_graph_context *ctx, struct c
12811265
struct commit_list *parent;
12821266
for (parent = commit->parents; parent; parent = parent->next) {
12831267
if (!(parent->item->object.flags & REACHABLE)) {
1284-
ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1285-
oidcpy(&ctx->oids.list[ctx->oids.nr], &(parent->item->object.oid));
1286-
ctx->oids.nr++;
1268+
oid_array_append(&ctx->oids, &parent->item->object.oid);
12871269
parent->item->object.flags |= REACHABLE;
12881270
}
12891271
}
@@ -1302,7 +1284,7 @@ static void close_reachable(struct write_commit_graph_context *ctx)
13021284
ctx->oids.nr);
13031285
for (i = 0; i < ctx->oids.nr; i++) {
13041286
display_progress(ctx->progress, i + 1);
1305-
commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
1287+
commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
13061288
if (commit)
13071289
commit->object.flags |= REACHABLE;
13081290
}
@@ -1319,7 +1301,7 @@ static void close_reachable(struct write_commit_graph_context *ctx)
13191301
0);
13201302
for (i = 0; i < ctx->oids.nr; i++) {
13211303
display_progress(ctx->progress, i + 1);
1322-
commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
1304+
commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
13231305

13241306
if (!commit)
13251307
continue;
@@ -1339,7 +1321,7 @@ static void close_reachable(struct write_commit_graph_context *ctx)
13391321
ctx->oids.nr);
13401322
for (i = 0; i < ctx->oids.nr; i++) {
13411323
display_progress(ctx->progress, i + 1);
1342-
commit = lookup_commit(ctx->r, &ctx->oids.list[i]);
1324+
commit = lookup_commit(ctx->r, &ctx->oids.oid[i]);
13431325

13441326
if (commit)
13451327
commit->object.flags &= ~REACHABLE;
@@ -1567,9 +1549,7 @@ static int fill_oids_from_commits(struct write_commit_graph_context *ctx,
15671549

15681550
oidset_iter_init(commits, &iter);
15691551
while ((oid = oidset_iter_next(&iter))) {
1570-
ALLOC_GROW(ctx->oids.list, ctx->oids.nr + 1, ctx->oids.alloc);
1571-
oidcpy(&ctx->oids.list[ctx->oids.nr], oid);
1572-
ctx->oids.nr++;
1552+
oid_array_append(&ctx->oids, oid);
15731553
}
15741554

15751555
return 0;
@@ -1588,35 +1568,6 @@ static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
15881568
stop_progress(&ctx->progress);
15891569
}
15901570

1591-
static uint32_t count_distinct_commits(struct write_commit_graph_context *ctx)
1592-
{
1593-
uint32_t i, count_distinct = 1;
1594-
1595-
if (ctx->report_progress)
1596-
ctx->progress = start_delayed_progress(
1597-
_("Counting distinct commits in commit graph"),
1598-
ctx->oids.nr);
1599-
display_progress(ctx->progress, 0); /* TODO: Measure QSORT() progress */
1600-
QSORT(ctx->oids.list, ctx->oids.nr, oid_compare);
1601-
1602-
for (i = 1; i < ctx->oids.nr; i++) {
1603-
display_progress(ctx->progress, i + 1);
1604-
if (!oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i])) {
1605-
if (ctx->split) {
1606-
struct commit *c = lookup_commit(ctx->r, &ctx->oids.list[i]);
1607-
1608-
if (!c || commit_graph_position(c) != COMMIT_NOT_FROM_GRAPH)
1609-
continue;
1610-
}
1611-
1612-
count_distinct++;
1613-
}
1614-
}
1615-
stop_progress(&ctx->progress);
1616-
1617-
return count_distinct;
1618-
}
1619-
16201571
static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
16211572
{
16221573
uint32_t i;
@@ -1628,15 +1579,14 @@ static void copy_oids_to_commits(struct write_commit_graph_context *ctx)
16281579
ctx->progress = start_delayed_progress(
16291580
_("Finding extra edges in commit graph"),
16301581
ctx->oids.nr);
1631-
for (i = 0; i < ctx->oids.nr; i++) {
1582+
oid_array_sort(&ctx->oids);
1583+
for (i = 0; i < ctx->oids.nr; i = oid_array_next_unique(&ctx->oids, i)) {
16321584
unsigned int num_parents;
16331585

16341586
display_progress(ctx->progress, i + 1);
1635-
if (i > 0 && oideq(&ctx->oids.list[i - 1], &ctx->oids.list[i]))
1636-
continue;
16371587

16381588
ALLOC_GROW(ctx->commits.list, ctx->commits.nr + 1, ctx->commits.alloc);
1639-
ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.list[i]);
1589+
ctx->commits.list[ctx->commits.nr] = lookup_commit(ctx->r, &ctx->oids.oid[i]);
16401590

16411591
if (ctx->split && flags != COMMIT_GRAPH_SPLIT_REPLACE &&
16421592
commit_graph_position(ctx->commits.list[ctx->commits.nr]) != COMMIT_NOT_FROM_GRAPH)
@@ -2155,7 +2105,7 @@ int write_commit_graph(struct object_directory *odb,
21552105
const struct commit_graph_opts *opts)
21562106
{
21572107
struct write_commit_graph_context *ctx;
2158-
uint32_t i, count_distinct = 0;
2108+
uint32_t i;
21592109
int res = 0;
21602110
int replace = 0;
21612111
struct bloom_filter_settings bloom_settings = DEFAULT_BLOOM_FILTER_SETTINGS;
@@ -2227,26 +2177,16 @@ int write_commit_graph(struct object_directory *odb,
22272177
}
22282178

22292179
ctx->approx_nr_objects = approximate_object_count();
2230-
ctx->oids.alloc = ctx->approx_nr_objects / 32;
2231-
2232-
if (ctx->split && opts && ctx->oids.alloc > opts->max_commits)
2233-
ctx->oids.alloc = opts->max_commits;
22342180

2235-
if (ctx->append) {
2181+
if (ctx->append)
22362182
prepare_commit_graph_one(ctx->r, ctx->odb);
2237-
if (ctx->r->objects->commit_graph)
2238-
ctx->oids.alloc += ctx->r->objects->commit_graph->num_commits;
2239-
}
2240-
2241-
if (ctx->oids.alloc < 1024)
2242-
ctx->oids.alloc = 1024;
2243-
ALLOC_ARRAY(ctx->oids.list, ctx->oids.alloc);
22442183

22452184
if (ctx->append && ctx->r->objects->commit_graph) {
22462185
struct commit_graph *g = ctx->r->objects->commit_graph;
22472186
for (i = 0; i < g->num_commits; i++) {
2248-
const unsigned char *hash = g->chunk_oid_lookup + g->hash_len * i;
2249-
hashcpy(ctx->oids.list[ctx->oids.nr++].hash, hash);
2187+
struct object_id oid;
2188+
hashcpy(oid.hash, g->chunk_oid_lookup + g->hash_len * i);
2189+
oid_array_append(&ctx->oids, &oid);
22502190
}
22512191
}
22522192

@@ -2268,17 +2208,6 @@ int write_commit_graph(struct object_directory *odb,
22682208

22692209
close_reachable(ctx);
22702210

2271-
count_distinct = count_distinct_commits(ctx);
2272-
2273-
if (count_distinct >= GRAPH_EDGE_LAST_MASK) {
2274-
error(_("the commit graph format cannot write %d commits"), count_distinct);
2275-
res = -1;
2276-
goto cleanup;
2277-
}
2278-
2279-
ctx->commits.alloc = count_distinct;
2280-
ALLOC_ARRAY(ctx->commits.list, ctx->commits.alloc);
2281-
22822211
copy_oids_to_commits(ctx);
22832212

22842213
if (ctx->commits.nr >= GRAPH_EDGE_LAST_MASK) {
@@ -2313,7 +2242,7 @@ int write_commit_graph(struct object_directory *odb,
23132242
cleanup:
23142243
free(ctx->graph_name);
23152244
free(ctx->commits.list);
2316-
free(ctx->oids.list);
2245+
oid_array_clear(&ctx->oids);
23172246

23182247
if (ctx->commit_graph_filenames_after) {
23192248
for (i = 0; i < ctx->num_commit_graphs_after; i++) {

0 commit comments

Comments
 (0)