prepare_packed_git(): sort packs by age and localness.
When accessing objects, we first look for them in packs that are linked together in the reverse order of discovery. Since younger packs tend to contain more recent objects, which are more likely to be accessed often, and local packs tend to contain objects more relevant to our specific projects, sort the list of packs before starting to access them. In addition, favoring local packs over the ones borrowed from alternates can be a win when alternates are mounted on network file systems. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Mar 9, 2007 at 03:52 UTC
b867092fec81c59a35337d24d2d976ab2acc2488
2 files changed
+57
cache.h
+1
@@ -372,6 +372,7 @@ extern struct packed_git {
372
struct packed_git *next;
373
struct pack_window *windows;
374
uint32_t *index_base;
375
+ time_t mtime;
376
off_t index_size;
377
off_t pack_size;
378
int pack_fd;
sha1_file.c
+56
@@ -739,6 +739,7 @@ struct packed_git *add_packed_git(char *path, int path_len, int local)
739
p->windows = NULL;
740
p->pack_fd = -1;
741
p->pack_local = local;
742
+ p->mtime = st.st_mtime;
743
if ((path_len > 44) && !get_sha1_hex(path + path_len - 44, sha1))
744
hashcpy(p->sha1, sha1);
745
return p;
@@ -823,6 +824,60 @@ static void prepare_packed_git_one(char *objdir, int local)
824
closedir(dir);
825
}
826
827
+static int sort_pack(const void *a_, const void *b_)
828
+{
829
+ struct packed_git *a = *((struct packed_git **)a_);
830
+ struct packed_git *b = *((struct packed_git **)b_);
831
+ int st;
832
+
833
+ /*
834
+ * Local packs tend to contain objects specific to our
835
+ * variant of the project than remote ones. In addition,
836
+ * remote ones could be on a network mounted filesystem.
837
+ * Favor local ones for these reasons.
838
+ */
839
+ st = a->pack_local - b->pack_local;
840
+ if (st)
841
+ return -st;
842
+
843
+ /*
844
+ * Younger packs tend to contain more recent objects,
845
+ * and more recent objects tend to get accessed more
846
+ * often.
847
+ */
848
+ if (a->mtime < b->mtime)
849
+ return 1;
850
+ else if (a->mtime == b->mtime)
851
+ return 0;
852
+ return -1;
853
+}
854
+
855
+static void rearrange_packed_git(void)
856
+{
857
+ struct packed_git **ary, *p;
858
+ int i, n;
859
+
860
+ for (n = 0, p = packed_git; p; p = p->next)
861
+ n++;
862
+ if (n < 2)
863
+ return;
864
+
865
+ /* prepare an array of packed_git for easier sorting */
866
+ ary = xcalloc(n, sizeof(struct packed_git *));
867
+ for (n = 0, p = packed_git; p; p = p->next)
868
+ ary[n++] = p;
869
+
870
+ qsort(ary, n, sizeof(struct packed_git *), sort_pack);
871
+
872
+ /* link them back again */
873
+ for (i = 0; i < n - 1; i++)
874
+ ary[i]->next = ary[i + 1];
875
+ ary[n - 1]->next = NULL;
876
+ packed_git = ary[0];
877
+
878
+ free(ary);
879
+}
880
+
881
static int prepare_packed_git_run_once = 0;
882
void prepare_packed_git(void)
883
{
@@ -837,6 +892,7 @@ void prepare_packed_git(void)
892
prepare_packed_git_one(alt->base, 0);
893
alt->name[-1] = '/';
894
}
895
+ rearrange_packed_git();
896
prepare_packed_git_run_once = 1;
897
}
898