Skip to content

Commit c01f51c

Browse files
pcloudsgitster
authored andcommitted
find_pack_entry(): do not keep packed_git pointer locally
Commit f7c22cc (always start looking up objects in the last used pack first - 2007-05-30) introduce a static packed_git* pointer as an optimization. The kept pointer however may become invalid if free_pack_by_name() happens to free that particular pack. Current code base does not access packs after calling free_pack_by_name() so it should not be a problem. Anyway, move the pointer out so that free_pack_by_name() can reset it to avoid running into troubles in future. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Acked-by: Nicolas Pitre <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9509973 commit c01f51c

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

sha1_file.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ static struct cached_object empty_tree = {
5454
0
5555
};
5656

57+
static struct packed_git *last_found_pack;
58+
5759
static struct cached_object *find_cached_object(const unsigned char *sha1)
5860
{
5961
int i;
@@ -720,6 +722,8 @@ void free_pack_by_name(const char *pack_name)
720722
close_pack_index(p);
721723
free(p->bad_object_sha1);
722724
*pp = p->next;
725+
if (last_found_pack == p)
726+
last_found_pack = NULL;
723727
free(p);
724728
return;
725729
}
@@ -2046,27 +2050,22 @@ static int fill_pack_entry(const unsigned char *sha1,
20462050

20472051
static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
20482052
{
2049-
static struct packed_git *last_found = (void *)1;
20502053
struct packed_git *p;
20512054

20522055
prepare_packed_git();
20532056
if (!packed_git)
20542057
return 0;
2055-
p = (last_found == (void *)1) ? packed_git : last_found;
20562058

2057-
do {
2058-
if (fill_pack_entry(sha1, e, p)) {
2059-
last_found = p;
2060-
return 1;
2061-
}
2059+
if (last_found_pack && fill_pack_entry(sha1, e, last_found_pack))
2060+
return 1;
20622061

2063-
if (p == last_found)
2064-
p = packed_git;
2065-
else
2066-
p = p->next;
2067-
if (p == last_found)
2068-
p = p->next;
2069-
} while (p);
2062+
for (p = packed_git; p; p = p->next) {
2063+
if (p == last_found_pack || !fill_pack_entry(sha1, e, p))
2064+
continue;
2065+
2066+
last_found_pack = p;
2067+
return 1;
2068+
}
20702069
return 0;
20712070
}
20722071

0 commit comments

Comments
 (0)