commit 5e22b7375b034172c405da8657858663d1051eb3 from: Omar Polo via: Thomas Adam date: Tue May 31 23:14:47 2022 UTC got patch: avoid open/sync/close the fileindex over and over again Instead of flushing the fileindex after every patch in the patchfile just reuse the same fileindex and sync it only at the end of the patch operation. This speeds up 'got patch' on large repositories by quite a lot. commit - 9a267125aa0f9487f325eea3e3c551ad9a19ca51 commit + 5e22b7375b034172c405da8657858663d1051eb3 blob - 83c0f25c125f10f982a164a3a028f7554654e8c4 blob + df7a870d179d5039ef98102550709b5ffff9889d --- include/got_worktree.h +++ include/got_worktree.h @@ -549,7 +549,8 @@ got_worktree_path_info(struct got_worktree *, struct g * Prepare for applying a patch. */ const struct got_error * -got_worktree_patch_prepare(struct got_fileindex **, struct got_worktree *); +got_worktree_patch_prepare(struct got_fileindex **, char **, + struct got_worktree *); /* * Lookup paths for the "old" and "new" file before patching and check their @@ -559,6 +560,16 @@ const struct got_error * got_worktree_patch_check_path(const char *, const char *, char **, char **, struct got_worktree *, struct got_repository *, struct got_fileindex *); +const struct got_error * +got_worktree_patch_schedule_add(const char *, struct got_repository *, + struct got_worktree *, struct got_fileindex *, got_worktree_checkout_cb, + void *); + +const struct got_error * +got_worktree_patch_schedule_rm(const char *, struct got_repository *, + struct got_worktree *, struct got_fileindex *, got_worktree_delete_cb, + void *); + /* Complete the patch operation. */ const struct got_error * -got_worktree_patch_complete(struct got_fileindex *); +got_worktree_patch_complete(struct got_fileindex *, char *); blob - b8905674bba37940821ad62fa951f062eb2f9093 blob + d839f0b09e4b212b919a562a9c3f31e974d718ed --- lib/patch.c +++ lib/patch.c @@ -572,27 +572,16 @@ patch_add(void *arg, unsigned char status, const char static const struct got_error * apply_patch(struct got_worktree *worktree, struct got_repository *repo, - const char *old, const char *new, struct got_patch *p, int nop, - struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg) + struct got_fileindex *fileindex, const char *old, const char *new, + struct got_patch *p, int nop, struct patch_args *pa, + got_cancel_cb cancel_cb, void *cancel_arg) { const struct got_error *err = NULL; - struct got_pathlist_head oldpaths, newpaths; - struct got_pathlist_entry *pe; int file_renamed = 0; char *oldpath = NULL, *newpath = NULL; char *tmppath = NULL, *template = NULL, *parent = NULL;; FILE *tmp = NULL; mode_t mode = GOT_DEFAULT_FILE_MODE; - - TAILQ_INIT(&oldpaths); - TAILQ_INIT(&newpaths); - - err = got_pathlist_insert(&pe, &oldpaths, old, NULL); - if (err) - goto done; - err = got_pathlist_insert(&pe, &newpaths, new, NULL); - if (err) - goto done; if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree), old) == -1) { @@ -626,8 +615,8 @@ apply_patch(struct got_worktree *worktree, struct got_ goto done; if (p->old != NULL && p->new == NULL) { - err = got_worktree_schedule_delete(worktree, &oldpaths, - 0, NULL, patch_delete, pa, repo, 0, 0); + err = got_worktree_patch_schedule_rm(old, repo, worktree, + fileindex, patch_delete, pa); goto done; } @@ -657,24 +646,23 @@ apply_patch(struct got_worktree *worktree, struct got_ } if (file_renamed) { - err = got_worktree_schedule_delete(worktree, &oldpaths, - 0, NULL, patch_delete, pa, repo, 0, 0); + err = got_worktree_patch_schedule_rm(old, repo, worktree, + fileindex, patch_delete, pa); if (err == NULL) - err = got_worktree_schedule_add(worktree, &newpaths, - patch_add, pa, repo, 1); + err = got_worktree_patch_schedule_add(new, repo, + worktree, fileindex, patch_add, + pa); if (err) unlink(newpath); } else if (p->old == NULL) { - err = got_worktree_schedule_add(worktree, &newpaths, - patch_add, pa, repo, 1); + err = got_worktree_patch_schedule_add(new, repo, worktree, + fileindex, patch_add, pa); if (err) unlink(newpath); } else err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL); done: - got_pathlist_free(&oldpaths); - got_pathlist_free(&newpaths); free(parent); free(template); if (tmppath != NULL) @@ -719,8 +707,9 @@ got_patch(int fd, struct got_worktree *worktree, struc int nop, int strip, int reverse, got_patch_progress_cb progress_cb, void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg) { - const struct got_error *err = NULL; + const struct got_error *err = NULL, *complete_err; struct got_fileindex *fileindex = NULL; + char *fileindex_path = NULL; char *oldpath, *newpath; struct imsgbuf *ibuf; int imsg_fds[2] = {-1, -1}; @@ -760,7 +749,8 @@ got_patch(int fd, struct got_worktree *worktree, struc if (err) goto done; - err = got_worktree_patch_prepare(&fileindex, worktree); + err = got_worktree_patch_prepare(&fileindex, &fileindex_path, + worktree); if (err) goto done; @@ -782,8 +772,8 @@ got_patch(int fd, struct got_worktree *worktree, struc err = got_worktree_patch_check_path(p.old, p.new, &oldpath, &newpath, worktree, repo, fileindex); if (err == NULL) - err = apply_patch(worktree, repo, oldpath, newpath, - &p, nop, &pa, cancel_cb, cancel_arg); + err = apply_patch(worktree, repo, fileindex, oldpath, + newpath, &p, nop, &pa, cancel_cb, cancel_arg); if (err != NULL) { failed = 1; /* recoverable errors */ @@ -805,8 +795,9 @@ got_patch(int fd, struct got_worktree *worktree, struc } done: - if (fileindex) - got_worktree_patch_complete(fileindex); + complete_err = got_worktree_patch_complete(fileindex, fileindex_path); + if (complete_err && err == NULL) + err = complete_err; if (fd != -1 && close(fd) == -1 && err == NULL) err = got_error_from_errno("close"); if (ibuf != NULL) blob - 71cf5fb20060f1c97b2f78b48c8389898bd11669 blob + 4b4d9c7fe17283dd56b89923ad3705aa52e12ad1 --- lib/worktree.c +++ lib/worktree.c @@ -8855,14 +8855,9 @@ patch_can_edit(const char *path, unsigned char status, const struct got_error * got_worktree_patch_prepare(struct got_fileindex **fileindex, - struct got_worktree *worktree) + char **fileindex_path, struct got_worktree *worktree) { - const struct got_error *err; - char *fileindex_path = NULL; - - err = open_fileindex(fileindex, &fileindex_path, worktree); - free(fileindex_path); - return err; + return open_fileindex(fileindex, fileindex_path, worktree); } const struct got_error * @@ -8913,8 +8908,56 @@ done: } const struct got_error * -got_worktree_patch_complete(struct got_fileindex *fileindex) +got_worktree_patch_schedule_add(const char *path, struct got_repository *repo, + struct got_worktree *worktree, struct got_fileindex *fileindex, + got_worktree_checkout_cb progress_cb, void *progress_arg) { - got_fileindex_free(fileindex); - return NULL; + struct schedule_addition_args saa; + + memset(&saa, 0, sizeof(saa)); + saa.worktree = worktree; + saa.fileindex = fileindex; + saa.progress_cb = progress_cb; + saa.progress_arg = progress_arg; + saa.repo = repo; + + return worktree_status(worktree, path, fileindex, repo, + schedule_addition, &saa, NULL, NULL, 1, 0); } + +const struct got_error * +got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo, + struct got_worktree *worktree, struct got_fileindex *fileindex, + got_worktree_delete_cb progress_cb, void *progress_arg) +{ + struct schedule_deletion_args sda; + + memset(&sda, 0, sizeof(sda)); + sda.worktree = worktree; + sda.fileindex = fileindex; + sda.progress_cb = progress_cb; + sda.progress_arg = progress_arg; + sda.repo = repo; + sda.delete_local_mods = 0; + sda.keep_on_disk = 0; + sda.ignore_missing_paths = 0; + sda.status_codes = NULL; + + return worktree_status(worktree, path, fileindex, repo, + schedule_for_deletion, &sda, NULL, NULL, 1, 1); +} + +const struct got_error * +got_worktree_patch_complete(struct got_fileindex *fileindex, + char *fileindex_path) +{ + const struct got_error *err = NULL; + + if (fileindex) { + err = sync_fileindex(fileindex, fileindex_path); + got_fileindex_free(fileindex); + } + + free(fileindex_path); + return err; +}