Racy git: avoid having to be always too careful
Immediately after a bulk checkout, most of the paths in the working tree would have the same timestamp as the index file, and this would force ce_match_stat() to take slow path for all of them. When writing an index file out, if many of the paths have very new (read: the same timestamp as the index file being written out) timestamp, we are better off delaying the return from the command, to make sure that later command to touch the working tree files will leave newer timestamps than recorded in the index, thereby avoiding to take the slow path. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Aug 5, 2006 at 04:16 UTC
b7e58b17b50bbbf00299fc89c480efd7cc92c1bd
1 file changed
+40
-4
read-cache.c
+40
-4
@@ -5,6 +5,7 @@
5
*/
6
#include "cache.h"
7
#include "cache-tree.h"
8
+#include <time.h>
9
10
/* Index extensions.
11
*
@@ -923,7 +924,7 @@ static void ce_smudge_racily_clean_entry(struct cache_entry *ce)
924
* $ echo filfre >nitfol
925
* $ git-update-index --add nitfol
926
*
926
- * but it does not. Whe the second update-index runs,
927
+ * but it does not. When the second update-index runs,
928
* it notices that the entry "frotz" has the same timestamp
929
* as index, and if we were to smudge it by resetting its
930
* size to zero here, then the object name recorded
@@ -945,7 +946,9 @@ int write_cache(int newfd, struct cache_entry **cache, int entries)
946
{
947
SHA_CTX c;
948
struct cache_header hdr;
948
- int i, removed;
949
+ int i, removed, recent;
950
+ struct stat st;
951
+ time_t now;
952
953
for (i = removed = 0; i < entries; i++)
954
if (!cache[i]->ce_mode)
@@ -959,15 +962,19 @@ int write_cache(int newfd, struct cache_entry **cache, int entries)
962
if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
963
return -1;
964
965
+ now = fstat(newfd, &st) ? 0 : st.st_mtime;
966
+ recent = 0;
967
for (i = 0; i < entries; i++) {
968
struct cache_entry *ce = cache[i];
969
+ time_t entry_time = (time_t) ntohl(ce->ce_mtime.sec);
970
if (!ce->ce_mode)
971
continue;
966
- if (index_file_timestamp &&
967
- index_file_timestamp <= ntohl(ce->ce_mtime.sec))
972
+ if (index_file_timestamp && index_file_timestamp <= entry_time)
973
ce_smudge_racily_clean_entry(ce);
974
if (ce_write(&c, newfd, ce, ce_size(ce)) < 0)
975
return -1;
976
+ if (now && now <= entry_time)
977
+ recent++;
978
}
979
980
/* Write extension data here */
@@ -983,5 +990,34 @@ int write_cache(int newfd, struct cache_entry **cache, int entries)
990
return -1;
991
}
992
}
993
+
994
+ /*
995
+ * To prevent later ce_match_stat() from always falling into
996
+ * check_fs(), if we have too many entries that can trigger
997
+ * racily clean check, we are better off delaying the return.
998
+ * We arbitrarily say if more than 20 paths or 25% of total
999
+ * paths are very new, we delay the return until the index
1000
+ * file gets a new timestamp.
1001
+ *
1002
+ * NOTE! NOTE! NOTE!
1003
+ *
1004
+ * This assumes that nobody is touching the working tree while
1005
+ * we are updating the index.
1006
+ */
1007
+ if (20 < recent || entries <= recent * 4) {
1008
+ now = fstat(newfd, &st) ? 0 : st.st_mtime;
1009
+ while (now && !fstat(newfd, &st) && st.st_mtime <= now) {
1010
+ struct timespec rq, rm;
1011
+ off_t where = lseek(newfd, 0, SEEK_CUR);
1012
+ rq.tv_sec = 0;
1013
+ rq.tv_nsec = 250000000;
1014
+ nanosleep(&rq, &rm);
1015
+ if ((where == (off_t) -1) ||
1016
+ (write(newfd, "", 1) != 1) ||
1017
+ (lseek(newfd, -1, SEEK_CUR) != where) ||
1018
+ ftruncate(newfd, where))
1019
+ break;
1020
+ }
1021
+ }
1022
return ce_flush(&c, newfd);
1023
}