allocate one large indexed object array upfront, not an array of pointers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
diff --git a/libexec/got-index-pack/got-index-pack.c b/libexec/got-index-pack/got-index-pack.c
index ece294c..fdf9a48 100644
--- a/libexec/got-index-pack/got-index-pack.c
+++ b/libexec/got-index-pack/got-index-pack.c
@@ -423,20 +423,20 @@ indexed_obj_cmp(const void *pa, const void *pb)
{
struct got_indexed_object *a, *b;
- a = *(struct got_indexed_object **)pa;
- b = *(struct got_indexed_object **)pb;
+ a = (struct got_indexed_object *)pa;
+ b = (struct got_indexed_object *)pb;
return got_object_id_cmp(&a->id, &b->id);
}
static void
make_packidx(struct got_packidx *packidx, int nobj,
- struct got_indexed_object **objects)
+ struct got_indexed_object *objects)
{
struct got_indexed_object *obj;
int i;
uint32_t idx = 0;
- mergesort(objects, nobj, sizeof(struct got_indexed_object *),
+ mergesort(objects, nobj, sizeof(struct got_indexed_object),
indexed_obj_cmp);
memset(packidx->hdr.fanout_table, 0,
@@ -444,7 +444,7 @@ make_packidx(struct got_packidx *packidx, int nobj,
packidx->nlargeobj = 0;
for (i = 0; i < nobj; i++) {
- obj = objects[i];
+ obj = &objects[i];
if (obj->valid)
add_indexed_object(packidx, idx++, obj);
}
@@ -482,7 +482,7 @@ index_pack(struct got_pack *pack, int idxfd, uint8_t *pack_hash,
struct got_packidx packidx;
char buf[8];
int nobj, nvalid, nloose, nresolved = 0, i;
- struct got_indexed_object **objects = NULL, *obj;
+ struct got_indexed_object *objects = NULL, *obj;
SHA1_CTX ctx;
uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
ssize_t r, w;
@@ -552,7 +552,7 @@ index_pack(struct got_pack *pack, int idxfd, uint8_t *pack_hash,
nvalid = 0;
nloose = 0;
- objects = calloc(nobj, sizeof(struct got_indexed_object *));
+ objects = calloc(nobj, sizeof(struct got_indexed_object));
if (objects == NULL)
return got_error_from_errno("calloc");
@@ -571,11 +571,7 @@ index_pack(struct got_pack *pack, int idxfd, uint8_t *pack_hash,
if (err)
goto done;
- obj = calloc(1, sizeof(*obj));
- if (obj == NULL) {
- err = got_error_from_errno("calloc");
- goto done;
- }
+ obj = &objects[i];
obj->crc = crc32(0L, NULL, 0);
/* Store offset to type+size information for this object. */
@@ -589,8 +585,6 @@ index_pack(struct got_pack *pack, int idxfd, uint8_t *pack_hash,
if (err)
goto done;
- objects[i] = obj;
-
if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
SEEK_SET) == -1) {
err = got_error_from_errno("lseek");
@@ -601,7 +595,7 @@ index_pack(struct got_pack *pack, int idxfd, uint8_t *pack_hash,
obj->type == GOT_OBJ_TYPE_TREE ||
obj->type == GOT_OBJ_TYPE_COMMIT ||
obj->type == GOT_OBJ_TYPE_TAG) {
- objects[i]->valid = 1;
+ obj->valid = 1;
nloose++;
} else if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
have_ref_deltas = 1;
@@ -622,14 +616,14 @@ index_pack(struct got_pack *pack, int idxfd, uint8_t *pack_hash,
while (nvalid != nobj) {
int n = 0;
for (i = 0; i < nobj; i++) {
- if (objects[i]->type != GOT_OBJ_TYPE_REF_DELTA &&
- objects[i]->type != GOT_OBJ_TYPE_OFFSET_DELTA)
+ obj = &objects[i];
+ if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
+ obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
continue;
- if (objects[i]->valid)
+ if (obj->valid)
continue;
- obj = objects[i];
if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
== -1) {
err = got_error_from_errno("lseek");
@@ -647,7 +641,7 @@ index_pack(struct got_pack *pack, int idxfd, uint8_t *pack_hash,
continue;
}
- objects[i]->valid = 1;
+ obj->valid = 1;
n++;
if (have_ref_deltas)
update_packidx(&packidx, nobj, obj);
@@ -701,7 +695,7 @@ index_pack(struct got_pack *pack, int idxfd, uint8_t *pack_hash,
if (err)
goto done;
for(i = 0; i < nobj; i++){
- PUTBE32(buf, objects[i]->crc);
+ PUTBE32(buf, objects[i].crc);
err = hwrite(idxfd, buf, 4, &ctx);
if (err)
goto done;