@matheusbsbs / matheus-bagliano / commits / 5cfa438a76
Fork 0

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 <szeder.dev@gmail.com> Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

SZEDER Gábor committed Jun 5, 2020 at 13:00 UTC 5cfa438a76f7f67f34f6f758b62943261ded1fb5
1 file changed +9 -17
commit-graph.c
+9 -17
@@ -230,8 +230,7 @@ struct commit_graph *parse_commit_graph(void *graph_map, int fd,
230 const unsigned char *data, *chunk_lookup;
231 uint32_t i;
232 struct commit_graph *graph;
233 - uint64_t last_chunk_offset;
234 - uint32_t last_chunk_id;
233 + uint64_t next_chunk_offset;
234 uint32_t graph_signature;
235 unsigned char graph_version, hash_version;
236
@@ -281,18 +280,17 @@ struct commit_graph *parse_commit_graph(void *graph_map, int fd,
280 return NULL;
281 }
282
284 - last_chunk_id = 0;
285 - last_chunk_offset = 8;
283 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++) {
286 uint32_t chunk_id;
289 - uint64_t chunk_offset;
287 + uint64_t chunk_offset = next_chunk_offset;
288 int chunk_repeated = 0;
289
290 chunk_id = get_be32(chunk_lookup + 0);
293 - chunk_offset = get_be64(chunk_lookup + 4);
291
292 chunk_lookup += GRAPH_CHUNKLOOKUP_WIDTH;
293 + next_chunk_offset = get_be64(chunk_lookup + 4);
294
295 if (chunk_offset > graph_size - the_hash_algo->rawsz) {
296 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,
310 case GRAPH_CHUNKID_OIDLOOKUP:
311 if (graph->chunk_oid_lookup)
312 chunk_repeated = 1;
315 - else
313 + else {
314 graph->chunk_oid_lookup = data + chunk_offset;
315 + graph->num_commits = (next_chunk_offset - chunk_offset)
316 + / graph->hash_len;
317 + }
318 break;
319
320 case GRAPH_CHUNKID_DATA:
@@ -368,15 +369,6 @@ struct commit_graph *parse_commit_graph(void *graph_map, int fd,
369 free(graph);
370 return NULL;
371 }
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;
372 }
373
374 if (graph->chunk_bloom_indexes && graph->chunk_bloom_data) {