Commit 976846014daddf292e080f2857ae63173f7b1433

Stefan Sperling 2020-03-18T16:13:46

write CRC info in one chunk rather than calling write(2) once per object

diff --git a/libexec/got-index-pack/got-index-pack.c b/libexec/got-index-pack/got-index-pack.c
index eaf56a1..6d9aba3 100644
--- a/libexec/got-index-pack/got-index-pack.c
+++ b/libexec/got-index-pack/got-index-pack.c
@@ -476,6 +476,7 @@ add_indexed_object(struct got_packidx *packidx, uint32_t idx,
 
 	memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
 	    SHA1_DIGEST_LENGTH);
+	packidx->hdr.crc32[idx] = htobe32(obj->crc);
 	if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
 		packidx->hdr.offsets[idx] = htobe32(obj->off);
 	else {
@@ -621,6 +622,11 @@ index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
 		err = got_error_from_errno("calloc");
 		goto done;
 	}
+	packidx.hdr.crc32 = calloc(nobj, sizeof(uint32_t));
+	if (packidx.hdr.crc32 == NULL) {
+		err = got_error_from_errno("calloc");
+		goto done;
+	}
 	packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
 	if (packidx.hdr.offsets == NULL) {
 		err = got_error_from_errno("calloc");
@@ -795,6 +801,9 @@ index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
 
 	make_packidx(&packidx, nobj, objects);
 
+	free(objects);
+	objects = NULL;
+
 	SHA1Init(&ctx);
 	putbe32(buf, GOT_PACKIDX_V2_MAGIC);
 	putbe32(buf + 4, GOT_PACKIDX_VERSION);
@@ -809,12 +818,9 @@ index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
 	    nobj * SHA1_DIGEST_LENGTH, &ctx);
 	if (err)
 		goto done;
-	for(i = 0; i < nobj; i++){
-		putbe32(buf, objects[i].crc);
-		err = hwrite(idxfd, buf, 4, &ctx);
-		if (err)
-			goto done;
-	}
+	err = hwrite(idxfd, packidx.hdr.crc32, nobj * sizeof(uint32_t), &ctx);
+	if (err)
+		goto done;
 	err = hwrite(idxfd, packidx.hdr.offsets, nobj * sizeof(uint32_t),
 	    &ctx);
 	if (err)