Skip to content

Commit 5cfa438

Browse files
szedergitster
authored andcommitted
commit-graph: simplify parse_commit_graph() #2
The Chunk Lookup table stores the chunks' starting offset in the commit-graph file, not their sizes. Consequently, the size of a chunk can only be calculated by subtracting its offset from the offset of the subsequent chunk (or that of the terminating label). This is currenly implemented in a bit complicated way: as we iterate over the entries of the Chunk Lookup table, we check the id of each chunk and store its starting offset, then we check the id of the last seen chunk and calculate its size using its previously saved offset. At the moment there is only one chunk for which we calculate its size, but this patch series will add more, and the repeated chunk id checks are not that pretty. Instead let's read ahead the offset of the next chunk on each iteration, so we can calculate the size of each chunk right away, right where we store its starting offset. Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2ad4f1a commit 5cfa438

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

commit-graph.c

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,7 @@ struct commit_graph *parse_commit_graph(void *graph_map, int fd,
230230
const unsigned char *data, *chunk_lookup;
231231
uint32_t i;
232232
struct commit_graph *graph;
233-
uint64_t last_chunk_offset;
234-
uint32_t last_chunk_id;
233+
uint64_t next_chunk_offset;
235234
uint32_t graph_signature;
236235
unsigned char graph_version, hash_version;
237236

@@ -281,18 +280,17 @@ struct commit_graph *parse_commit_graph(void *graph_map, int fd,
281280
return NULL;
282281
}
283282

284-
last_chunk_id = 0;
285-
last_chunk_offset = 8;
286283
chunk_lookup = data + 8;
287-
for (i = 0; i <= graph->num_chunks; i++) {
284+
next_chunk_offset = get_be64(chunk_lookup + 4);
285+
for (i = 0; i < graph->num_chunks; i++) {
288286
uint32_t chunk_id;
289-
uint64_t chunk_offset;
287+
uint64_t chunk_offset = next_chunk_offset;
290288
int chunk_repeated = 0;
291289

292290
chunk_id = get_be32(chunk_lookup + 0);
293-
chunk_offset = get_be64(chunk_lookup + 4);
294291

295292
chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
293+
next_chunk_offset = get_be64(chunk_lookup + 4);
296294

297295
if (chunk_offset > graph_size - the_hash_algo->rawsz) {
298296
error(_("commit-graph improper chunk offset %08x%08x"), (uint32_t)(chunk_offset >> 32),
@@ -312,8 +310,11 @@ struct commit_graph *parse_commit_graph(void *graph_map, int fd,
312310
case GRAPH_CHUNKID_OIDLOOKUP:
313311
if (graph->chunk_oid_lookup)
314312
chunk_repeated = 1;
315-
else
313+
else {
316314
graph->chunk_oid_lookup = data + chunk_offset;
315+
graph->num_commits = (next_chunk_offset - chunk_offset)
316+
/ graph->hash_len;
317+
}
317318
break;
318319

319320
case GRAPH_CHUNKID_DATA:
@@ -368,15 +369,6 @@ struct commit_graph *parse_commit_graph(void *graph_map, int fd,
368369
free(graph);
369370
return NULL;
370371
}
371-
372-
if (last_chunk_id == GRAPH_CHUNKID_OIDLOOKUP)
373-
{
374-
graph->num_commits = (chunk_offset - last_chunk_offset)
375-
/ graph->hash_len;
376-
}
377-
378-
last_chunk_id = chunk_id;
379-
last_chunk_offset = chunk_offset;
380372
}
381373

382374
if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {

0 commit comments

Comments
 (0)