@matheusbsbs / matheus-bagliano / commits / c2e525d97f
Fork 0

git-pickaxe: optimize by avoiding repeated read_sha1_file().

It turns out that pickaxe reads the same blob repeatedly while blame can reuse the blob already read for the parent when handling a child commit when it's parent's turn to pass its blame to the grandparent. Have a cache in the origin structure to keep the blob there, which will be garbage collected when the origin loses the last reference to it. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Nov 5, 2006 at 11:51 UTC c2e525d97f81bc178567cdf4dd7056ce6224eb58
1 file changed +62 -38
builtin-pickaxe.c
+62 -38
@@ -40,6 +40,11 @@ static int max_score_digits;
40 #define DEBUG 0
41 #endif
42
43 +/* stats */
44 +static int num_read_blob;
45 +static int num_get_patch;
46 +static int num_commits;
47 +
48 #define PICKAXE_BLAME_MOVE 01
49 #define PICKAXE_BLAME_COPY 02
50 #define PICKAXE_BLAME_COPY_HARDER 04
@@ -63,10 +68,25 @@ static unsigned blame_copy_score;
68 struct origin {
69 int refcnt;
70 struct commit *commit;
71 + mmfile_t file;
72 unsigned char blob_sha1[20];
73 char path[FLEX_ARRAY];
74 };
75
76 +static char *fill_origin_blob(struct origin *o, mmfile_t *file)
77 +{
78 + if (!o->file.ptr) {
79 + char type[10];
80 + num_read_blob++;
81 + file->ptr = read_sha1_file(o->blob_sha1, type,
82 + (unsigned long *)(&(file->size)));
83 + o->file = *file;
84 + }
85 + else
86 + *file = o->file;
87 + return file->ptr;
88 +}
89 +
90 static inline struct origin *origin_incref(struct origin *o)
91 {
92 if (o)
@@ -77,6 +97,8 @@ static inline struct origin *origin_incref(struct origin *o)
97 static void origin_decref(struct origin *o)
98 {
99 if (o && --o->refcnt <= 0) {
100 + if (o->file.ptr)
101 + free(o->file.ptr);
102 memset(o, 0, sizeof(*o));
103 free(o);
104 }
@@ -431,25 +453,14 @@ static struct patch *compare_buffer(mmfile_t *file_p, mmfile_t *file_o,
453 static struct patch *get_patch(struct origin *parent, struct origin *origin)
454 {
455 mmfile_t file_p, file_o;
434 - char type[10];
435 - char *blob_p, *blob_o;
456 struct patch *patch;
457
438 - blob_p = read_sha1_file(parent->blob_sha1, type,
439 - (unsigned long *) &file_p.size);
440 - blob_o = read_sha1_file(origin->blob_sha1, type,
441 - (unsigned long *) &file_o.size);
442 - file_p.ptr = blob_p;
443 - file_o.ptr = blob_o;
444 - if (!file_p.ptr || !file_o.ptr) {
445 - free(blob_p);
446 - free(blob_o);
458 + fill_origin_blob(parent, &file_p);
459 + fill_origin_blob(origin, &file_o);
460 + if (!file_p.ptr || !file_o.ptr)
461 return NULL;
448 - }
449 -
462 patch = compare_buffer(&file_p, &file_o, 0);
451 - free(blob_p);
452 - free(blob_o);
463 + num_get_patch++;
464 return patch;
465 }
466
@@ -784,20 +795,14 @@ static int find_move_in_parent(struct scoreboard *sb,
795 int last_in_target, made_progress;
796 struct blame_entry *e, split[3];
797 mmfile_t file_p;
787 - char type[10];
788 - char *blob_p;
798
799 last_in_target = find_last_in_target(sb, target);
800 if (last_in_target < 0)
801 return 1; /* nothing remains for this target */
802
794 - blob_p = read_sha1_file(parent->blob_sha1, type,
795 - (unsigned long *) &file_p.size);
796 - file_p.ptr = blob_p;
797 - if (!file_p.ptr) {
798 - free(blob_p);
803 + fill_origin_blob(parent, &file_p);
804 + if (!file_p.ptr)
805 return 0;
800 - }
806
807 made_progress = 1;
808 while (made_progress) {
@@ -814,7 +819,6 @@ static int find_move_in_parent(struct scoreboard *sb,
819 decref_split(split);
820 }
821 }
817 - free(blob_p);
822 return 0;
823 }
824
@@ -900,8 +904,6 @@ static int find_copy_in_parent(struct scoreboard *sb,
904 struct diff_filepair *p = diff_queued_diff.queue[i];
905 struct origin *norigin;
906 mmfile_t file_p;
903 - char type[10];
904 - char *blob;
907 struct blame_entry this[3];
908
909 if (!DIFF_FILE_VALID(p->one))
@@ -912,9 +914,7 @@ static int find_copy_in_parent(struct scoreboard *sb,
914
915 norigin = get_origin(sb, parent, p->one->path);
916 hashcpy(norigin->blob_sha1, p->one->sha1);
915 - blob = read_sha1_file(norigin->blob_sha1, type,
916 - (unsigned long *) &file_p.size);
917 - file_p.ptr = blob;
917 + fill_origin_blob(norigin, &file_p);
918 if (!file_p.ptr)
919 continue;
920
@@ -925,7 +925,6 @@ static int find_copy_in_parent(struct scoreboard *sb,
925 this);
926 decref_split(this);
927 }
928 - free(blob);
928 origin_decref(norigin);
929 }
930
@@ -953,6 +952,28 @@ static int find_copy_in_parent(struct scoreboard *sb,
952 return retval;
953 }
954
955 +/* The blobs of origin and porigin exactly match, so everything
956 + * origin is suspected for can be blamed on the parent.
957 + */
958 +static void pass_whole_blame(struct scoreboard *sb,
959 + struct origin *origin, struct origin *porigin)
960 +{
961 + struct blame_entry *e;
962 +
963 + if (!porigin->file.ptr && origin->file.ptr) {
964 + /* Steal its file */
965 + porigin->file = origin->file;
966 + origin->file.ptr = NULL;
967 + }
968 + for (e = sb->ent; e; e = e->next) {
969 + if (cmp_suspect(e->suspect, origin))
970 + continue;
971 + origin_incref(porigin);
972 + origin_decref(e->suspect);
973 + e->suspect = porigin;
974 + }
975 +}
976 +
977 #define MAXPARENT 16
978
979 static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
@@ -986,13 +1007,7 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
1007 if (!porigin)
1008 continue;
1009 if (!hashcmp(porigin->blob_sha1, origin->blob_sha1)) {
989 - struct blame_entry *e;
990 - for (e = sb->ent; e; e = e->next)
991 - if (e->suspect == origin) {
992 - origin_incref(porigin);
993 - origin_decref(e->suspect);
994 - e->suspect = porigin;
995 - }
1010 + pass_whole_blame(sb, origin, porigin);
1011 origin_decref(porigin);
1012 goto finish;
1013 }
@@ -1010,6 +1025,7 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
1025 }
1026 }
1027
1028 + num_commits++;
1029 for (i = 0, parent = commit->parents;
1030 i < MAXPARENT && parent;
1031 parent = parent->next, i++) {
@@ -1068,7 +1084,8 @@ static void assign_blame(struct scoreboard *sb, struct rev_info *revs, int opt)
1084
1085 origin_incref(suspect);
1086 commit = suspect->commit;
1071 - parse_commit(commit);
1087 + if (!commit->object.parsed)
1088 + parse_commit(commit);
1089 if (!(commit->object.flags & UNINTERESTING) &&
1090 !(revs->max_age != -1 && commit->date < revs->max_age))
1091 pass_blame(sb, suspect, opt);
@@ -1735,6 +1752,7 @@ int cmd_pickaxe(int argc, const char **argv, const char *prefix)
1752 die("no such path %s in %s", path, final_commit_name);
1753
1754 sb.final_buf = read_sha1_file(o->blob_sha1, type, &sb.final_buf_size);
1755 + num_read_blob++;
1756 lno = prepare_lines(&sb);
1757
1758 if (bottom < 1)
@@ -1772,5 +1790,11 @@ int cmd_pickaxe(int argc, const char **argv, const char *prefix)
1790 free(ent);
1791 ent = e;
1792 }
1793 +
1794 + if (DEBUG) {
1795 + printf("num read blob: %d\n", num_read_blob);
1796 + printf("num get patch: %d\n", num_get_patch);
1797 + printf("num commits: %d\n", num_commits);
1798 + }
1799 return 0;
1800 }