@matheusbsbs / matheus-bagliano / commits / 1510fea781
Fork 0

Avoid accessing a slow working copy during diffcore operations.

The Cygwin folks have done a fine job at creating a POSIX layer on Windows That Just Works(tm). However it comes with a penalty; accessing files in the working tree by way of stat/open/mmap can be slower for diffcore than inflating the data from a blob which is stored in a packfile. This performance problem is especially an issue in merge-recursive when dealing with nearly 7000 added files, as we are loading each file's content from the working directory to perform rename detection. I have literally seen (and sadly watched) paint dry in less time than it takes for merge-recursive to finish such a merge. On the other hand this very same merge runs very fast on Solaris. If Git is compiled with NO_FAST_WORKING_DIRECTORY set then we will avoid looking at the working directory when the blob in question is available within a packfile and the caller doesn't need the data unpacked into a temporary file. We don't use loose objects as they have the same open/mmap/close costs as the working directory file access, but have the additional CPU overhead of needing to inflate the content before use. So it is still faster to use the working tree file over the loose object. If the caller needs the file data unpacked into a temporary file its likely because they are going to call an external diff program, passing the file as a parameter. In this case reusing the working tree file will be faster as we don't need to inflate the data and write it out to a temporary file. The NO_FAST_WORKING_DIRECTORY feature is enabled by default on Cygwin, as that is the platform which currently appears to benefit the most from this option. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Shawn O. Pearce committed Dec 14, 2006 at 06:15 UTC 1510fea781cb0517eeba8c378964f7bc4f9577ab
2 files changed +28 -3
Makefile
+7
@@ -69,6 +69,9 @@ all:
69 #
70 # Define NO_MMAP if you want to avoid mmap.
71 #
72 +# Define NO_FAST_WORKING_DIRECTORY if accessing objects in pack files is
73 +# generally faster on your platform than accessing the working directory.
74 +#
75 # Define NO_IPV6 if you lack IPv6 support and getaddrinfo().
76 #
77 # Define NO_SOCKADDR_STORAGE if your platform does not have struct
@@ -355,6 +358,7 @@ ifeq ($(uname_O),Cygwin)
358 NO_SYMLINK_HEAD = YesPlease
359 NEEDS_LIBICONV = YesPlease
360 NO_C99_FORMAT = YesPlease
361 + NO_FAST_WORKING_DIRECTORY = UnfortunatelyYes
362 # There are conflicting reports about this.
363 # On some boxes NO_MMAP is needed, and not so elsewhere.
364 # Try uncommenting this if you see things break -- YMMV.
@@ -506,6 +510,9 @@ ifdef NO_MMAP
510 COMPAT_CFLAGS += -DNO_MMAP
511 COMPAT_OBJS += compat/mmap.o
512 endif
513 +ifdef NO_FAST_WORKING_DIRECTORY
514 + BASIC_CFLAGS += -DNO_FAST_WORKING_DIRECTORY
515 +endif
516 ifdef NO_IPV6
517 BASIC_CFLAGS += -DNO_IPV6
518 endif
diff.c
+21 -3
@@ -12,6 +12,12 @@
12 #include "xdiff-interface.h"
13 #include "color.h"
14
15 +#ifdef NO_FAST_WORKING_DIRECTORY
16 +#define FAST_WORKING_DIRECTORY 0
17 +#else
18 +#define FAST_WORKING_DIRECTORY 1
19 +#endif
20 +
21 static int use_size_cache;
22
23 static int diff_detect_rename_default;
@@ -1158,7 +1164,7 @@ void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
1164 * the work tree has that object contents, return true, so that
1165 * prepare_temp_file() does not have to inflate and extract.
1166 */
1161 -static int work_tree_matches(const char *name, const unsigned char *sha1)
1167 +static int reuse_worktree_file(const char *name, const unsigned char *sha1, int want_file)
1168 {
1169 struct cache_entry *ce;
1170 struct stat st;
@@ -1179,6 +1185,18 @@ static int work_tree_matches(const char *name, const unsigned char *sha1)
1185 if (!active_cache)
1186 return 0;
1187
1188 + /* We want to avoid the working directory if our caller
1189 + * doesn't need the data in a normal file, this system
1190 + * is rather slow with its stat/open/mmap/close syscalls,
1191 + * and the object is contained in a pack file. The pack
1192 + * is probably already open and will be faster to obtain
1193 + * the data through than the working directory. Loose
1194 + * objects however would tend to be slower as they need
1195 + * to be individually opened and inflated.
1196 + */
1197 + if (FAST_WORKING_DIRECTORY && !want_file && has_sha1_pack(sha1, NULL))
1198 + return 0;
1199 +
1200 len = strlen(name);
1201 pos = cache_name_pos(name, len);
1202 if (pos < 0)
@@ -1265,7 +1283,7 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only)
1283 if (s->data)
1284 return err;
1285 if (!s->sha1_valid ||
1268 - work_tree_matches(s->path, s->sha1)) {
1286 + reuse_worktree_file(s->path, s->sha1, 0)) {
1287 struct stat st;
1288 int fd;
1289 if (lstat(s->path, &st) < 0) {
@@ -1372,7 +1390,7 @@ static void prepare_temp_file(const char *name,
1390 }
1391
1392 if (!one->sha1_valid ||
1375 - work_tree_matches(name, one->sha1)) {
1393 + reuse_worktree_file(name, one->sha1, 1)) {
1394 struct stat st;
1395 if (lstat(name, &st) < 0) {
1396 if (errno == ENOENT)