AmendHub

Download:

jcs

/

amend

/

amendments

/

112

repo: Make repo_init a bit faster

Don't count the ids and then fetch every nth one, just fetch the ids
with bile_sorted_ids_by_type.
 
Avoid some zeroing of things we're just going to overwrite
completely.
 
Also make temporary filenames unique, in case a previous run died
during diff for some reason.

jcs made amendment 112 about 1 year ago
--- repo.c Mon Feb 6 10:30:32 2023 +++ repo.c Tue Mar 28 15:20:43 2023 @@ -102,6 +102,7 @@ repo_init(struct bile *bile, bool is_new) size_t size; char *data; unsigned short i; + unsigned long *ids; repo = xmalloczero(sizeof(struct repo), "repo"); repo->bile = bile; @@ -114,31 +115,30 @@ repo_init(struct bile *bile, bool is_new) } /* fill in file info */ - repo->nfiles = bile_count_by_type(bile, REPO_FILE_RTYPE); + repo->nfiles = bile_sorted_ids_by_type(bile, REPO_FILE_RTYPE, &ids); if (repo->nfiles) { repo->files = xcalloc(repo->nfiles, sizeof(Ptr), "repo files"); for (i = 0; i < repo->nfiles; i++) { if (i == 0 || i == repo->nfiles - 1 || ((i + 1) % 10) == 0) progress("Loading file %d/%d...", i + 1, repo->nfiles); - bob = bile_get_nth_of_type(bile, i, REPO_FILE_RTYPE); - if (bob == NULL) - panic("no %d file, but count said it should be there", i); - size = bile_read_alloc(bile, REPO_FILE_RTYPE, bob->id, &data); + size = bile_read_alloc(bile, REPO_FILE_RTYPE, ids[i], &data); if (size == 0) - panic("failed fetching file %ld", bob->id); - repo->files[i] = xmalloczero(sizeof(struct repo_file), + panic("failed fetching file %ld", ids[i]); + repo->files[i] = xmalloc(sizeof(struct repo_file), "repo file"); - repo->files[i] = repo_parse_file(bob->id, (unsigned char *)data, + repo->files[i] = repo_parse_file(ids[i], (unsigned char *)data, size); if (repo->files[i]->id >= repo->next_file_id) repo->next_file_id = repo->files[i]->id + 1; xfree(&data); } + xfree(&ids); + repo_sort_files(repo); } - repo_sort_files(repo); /* fill in amendment info */ - repo->namendments = bile_count_by_type(bile, REPO_AMENDMENT_RTYPE); + repo->namendments = bile_sorted_ids_by_type(bile, REPO_AMENDMENT_RTYPE, + &ids); if (repo->namendments) { repo->amendments = xcalloc(repo->namendments, sizeof(Ptr), "repo amendments"); @@ -147,21 +147,17 @@ repo_init(struct bile *bile, bool is_new) ((i + 1) % 10) == 0) progress("Loading amendment %d/%d...", i + 1, repo->namendments); - bob = bile_get_nth_of_type(bile, i, REPO_AMENDMENT_RTYPE); - if (bob == NULL) - panic("no %d amendment, but count said it should be there", - i); - size = bile_read_alloc(bile, REPO_AMENDMENT_RTYPE, bob->id, + size = bile_read_alloc(bile, REPO_AMENDMENT_RTYPE, ids[i], &data); if (size == 0) - panic("failed fetching amendment %ld", bob->id); - repo->amendments[i] = repo_parse_amendment(bob->id, + panic("failed fetching amendment %ld", ids[i]); + repo->amendments[i] = repo_parse_amendment(ids[i], (unsigned char *)data, size); if (repo->amendments[i]->id >= repo->next_amendment_id) repo->next_amendment_id = repo->amendments[i]->id + 1; xfree(&data); } - + xfree(&ids); repo_sort_amendments(repo); } @@ -188,7 +184,8 @@ repo_close(struct repo *repo) xfree(&amendment); } - xfree(&repo->amendments); + if (repo->amendments) + xfree(&repo->amendments); for (i = 0; i < repo->nfiles; i++) { file = repo->files[i]; @@ -197,7 +194,8 @@ repo_close(struct repo *repo) xfree(&file); } - xfree(&repo->files); + if (repo->files) + xfree(&repo->files); bile_close(repo->bile); xfree(&repo); @@ -258,7 +256,7 @@ repo_parse_amendment(unsigned long id, unsigned char * struct repo_amendment *amendment; unsigned short len, i; - amendment = xmalloczero(sizeof(struct repo_amendment), + amendment = xmalloc(sizeof(struct repo_amendment), "repo_parse_amendment"); amendment->id = id; @@ -729,8 +727,8 @@ repo_diff_file(struct repo *repo, struct repo_file *fi short error, ret, frefnum, tofile_empty = 0; /* write out old file */ - snprintf((char *)&fromfilename, sizeof(fromfilename), "[tmp] %s", - file->filename); + snprintf((char *)&fromfilename, sizeof(fromfilename), "[%lu] %s", + Time, file->filename); CtoPstr(fromfilename); error = Create(fromfilename, repo->bile->vrefnum, file->creator,