commit 9b2430434d761486865c6b0961d5053349c571eb from: Omar Polo date: Fri Jul 28 19:05:44 2023 UTC speed up read_fileindex_path() Use a local buffer instead of calling reallocarray() every 8 bytes; the speed up is measurable. The downside is that we're now limited to paths long at most PATH_MAX bytes, but since this is the fileindex it's not an issue in practice. ok jamsek stsp commit - 996fba9b2127b5170b74da3dcdf8eabc7a5f8985 commit + 9b2430434d761486865c6b0961d5053349c571eb blob - e989c6b7cd01f55aa6173b8bbf04441802b11fe6 blob + 76f82954347761e003c7cb88088506c09a363673 --- lib/fileindex.c +++ lib/fileindex.c @@ -578,38 +578,26 @@ read_fileindex_val16(uint16_t *val, struct got_hash *c static const struct got_error * read_fileindex_path(char **path, struct got_hash *ctx, FILE *infile) { - const struct got_error *err = NULL; const size_t chunk_size = 8; - size_t n, len = 0, totlen = chunk_size; - - *path = malloc(totlen); - if (*path == NULL) - return got_error_from_errno("malloc"); + char p[PATH_MAX]; + size_t n, len = 0; do { - if (len + chunk_size > totlen) { - char *p = reallocarray(*path, totlen + chunk_size, 1); - if (p == NULL) { - err = got_error_from_errno("reallocarray"); - break; - } - totlen += chunk_size; - *path = p; - } - n = fread(*path + len, 1, chunk_size, infile); - if (n != chunk_size) { - err = got_ferror(infile, GOT_ERR_FILEIDX_BAD); - break; - } - got_hash_update(ctx, *path + len, chunk_size); + if (len + chunk_size > sizeof(p)) + return got_error(GOT_ERR_FILEIDX_BAD); + + n = fread(&p[len], 1, chunk_size, infile); + if (n != chunk_size) + return got_ferror(infile, GOT_ERR_FILEIDX_BAD); + + got_hash_update(ctx, &p[len], chunk_size); len += chunk_size; - } while (memchr(*path + len - chunk_size, '\0', chunk_size) == NULL); + } while (memchr(&p[len - chunk_size], '\0', chunk_size) == NULL); - if (err) { - free(*path); - *path = NULL; - } - return err; + *path = strdup(p); + if (*path == NULL) + return got_error_from_errno("strdup"); + return NULL; } static const struct got_error *