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

Optimize "diff --cached" performance.

The read_tree() function is called only from the call chain to run "git diff --cached" (this includes the internal call made by git-runstatus to run_diff_index()). The function vacates stage without any funky "merge" magic. The caller then goes and compares stage #1 entries from the tree with stage #0 entries from the original index. When adding the cache entries this way, it used the general purpose add_cache_entry(). This function looks for an existing entry to replace or if there is none to find where to insert the new entry, resolves D/F conflict and all the other things. For the purpose of reading entries into an empty stage, none of that processing is needed. We can instead append everything and then sort the result at the end. This commit changes read_tree() to first make sure that there is no existing cache entries at specified stage, and if that is the case, it runs add_cache_entry() with ADD_CACHE_JUST_APPEND flag (new), and then sort the resulting cache using qsort(). This new flag tells add_cache_entry() to omit all the checks such as "Does this path already exist? Does adding this path remove other existing entries because it turns a directory to a file?" and instead append the given cache entry straight at the end of the active cache. The caller of course is expected to sort the resulting cache at the end before using the result. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Aug 9, 2007 at 13:42 UTC af3785dc5a76f4d5ddb8039e33c322e0e8b60e72
3 files changed +85 -5
cache.h
+1
@@ -258,6 +258,7 @@ extern int index_name_pos(struct index_state *, const char *name, int namelen);
258 #define ADD_CACHE_OK_TO_ADD 1 /* Ok to add */
259 #define ADD_CACHE_OK_TO_REPLACE 2 /* Ok to replace file/directory */
260 #define ADD_CACHE_SKIP_DFCHECK 4 /* Ok to skip DF conflict checks */
261 +#define ADD_CACHE_JUST_APPEND 8 /* Append only; tree.c::read_tree() */
262 extern int add_index_entry(struct index_state *, struct cache_entry *ce, int option);
263 extern struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int really);
264 extern int remove_index_entry_at(struct index_state *, int pos);
read-cache.c
+18 -2
@@ -665,7 +665,7 @@ static int check_file_directory_conflict(struct index_state *istate,
665 return retval + has_dir_name(istate, ce, pos, ok_to_replace);
666 }
667
668 -int add_index_entry(struct index_state *istate, struct cache_entry *ce, int option)
668 +static int add_index_entry_with_check(struct index_state *istate, struct cache_entry *ce, int option)
669 {
670 int pos;
671 int ok_to_add = option & ADD_CACHE_OK_TO_ADD;
@@ -707,6 +707,22 @@ int add_index_entry(struct index_state *istate, struct cache_entry *ce, int opti
707 pos = index_name_pos(istate, ce->name, ntohs(ce->ce_flags));
708 pos = -pos-1;
709 }
710 + return pos + 1;
711 +}
712 +
713 +int add_index_entry(struct index_state *istate, struct cache_entry *ce, int option)
714 +{
715 + int pos;
716 +
717 + if (option & ADD_CACHE_JUST_APPEND)
718 + pos = istate->cache_nr;
719 + else {
720 + int ret;
721 + ret = add_index_entry_with_check(istate, ce, option);
722 + if (ret <= 0)
723 + return ret;
724 + pos = ret - 1;
725 + }
726
727 /* Make sure the array is big enough .. */
728 if (istate->cache_nr == istate->cache_alloc) {
@@ -717,7 +733,7 @@ int add_index_entry(struct index_state *istate, struct cache_entry *ce, int opti
733
734 /* Add it in.. */
735 istate->cache_nr++;
720 - if (istate->cache_nr > pos)
736 + if (istate->cache_nr > pos + 1)
737 memmove(istate->cache + pos + 1,
738 istate->cache + pos,
739 (istate->cache_nr - pos - 1) * sizeof(ce));
tree.c
+66 -3
@@ -1,4 +1,5 @@
1 #include "cache.h"
2 +#include "cache-tree.h"
3 #include "tree.h"
4 #include "blob.h"
5 #include "commit.h"
@@ -7,7 +8,7 @@
8
9 const char *tree_type = "tree";
10
10 -static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
11 +static int read_one_entry_opt(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage, int opt)
12 {
13 int len;
14 unsigned int size;
@@ -25,7 +26,23 @@ static int read_one_entry(const unsigned char *sha1, const char *base, int basel
26 memcpy(ce->name, base, baselen);
27 memcpy(ce->name + baselen, pathname, len+1);
28 hashcpy(ce->sha1, sha1);
28 - return add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
29 + return add_cache_entry(ce, opt);
30 +}
31 +
32 +static int read_one_entry(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
33 +{
34 + return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage,
35 + ADD_CACHE_OK_TO_ADD|ADD_CACHE_SKIP_DFCHECK);
36 +}
37 +
38 +/*
39 + * This is used when the caller knows there is no existing entries at
40 + * the stage that will conflict with the entry being added.
41 + */
42 +static int read_one_entry_quick(const unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage)
43 +{
44 + return read_one_entry_opt(sha1, base, baselen, pathname, mode, stage,
45 + ADD_CACHE_JUST_APPEND);
46 }
47
48 static int match_tree_entry(const char *base, int baselen, const char *path, unsigned int mode, const char **paths)
@@ -119,9 +136,55 @@ int read_tree_recursive(struct tree *tree,
136 return 0;
137 }
138
139 +static int cmp_cache_name_compare(const void *a_, const void *b_)
140 +{
141 + const struct cache_entry *ce1, *ce2;
142 +
143 + ce1 = *((const struct cache_entry **)a_);
144 + ce2 = *((const struct cache_entry **)b_);
145 + return cache_name_compare(ce1->name, ntohs(ce1->ce_flags),
146 + ce2->name, ntohs(ce2->ce_flags));
147 +}
148 +
149 int read_tree(struct tree *tree, int stage, const char **match)
150 {
124 - return read_tree_recursive(tree, "", 0, stage, match, read_one_entry);
151 + read_tree_fn_t fn = NULL;
152 + int i, err;
153 +
154 + /*
155 + * Currently the only existing callers of this function all
156 + * call it with stage=1 and after making sure there is nothing
157 + * at that stage; we could always use read_one_entry_quick().
158 + *
159 + * But when we decide to straighten out git-read-tree not to
160 + * use unpack_trees() in some cases, this will probably start
161 + * to matter.
162 + */
163 +
164 + /*
165 + * See if we have cache entry at the stage. If so,
166 + * do it the original slow way, otherwise, append and then
167 + * sort at the end.
168 + */
169 + for (i = 0; !fn && i < active_nr; i++) {
170 + struct cache_entry *ce = active_cache[i];
171 + if (ce_stage(ce) == stage)
172 + fn = read_one_entry;
173 + }
174 +
175 + if (!fn)
176 + fn = read_one_entry_quick;
177 + err = read_tree_recursive(tree, "", 0, stage, match, fn);
178 + if (fn == read_one_entry || err)
179 + return err;
180 +
181 + /*
182 + * Sort the cache entry -- we need to nuke the cache tree, though.
183 + */
184 + cache_tree_free(&active_cache_tree);
185 + qsort(active_cache, active_nr, sizeof(active_cache[0]),
186 + cmp_cache_name_compare);
187 + return 0;
188 }
189
190 struct tree *lookup_tree(const unsigned char *sha1)