git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2] find_pack_entry(): do not keep packed_git pointer locally

From
Junio C Hamano <gitster@pobox.com>
Date
Jan 31, 2012, 18:02 UTC
Message-ID
<7vr4yf92dg.fsf@alter.siamese.dyndns.org>
In-Reply-To
<1328010239-29669-1-git-send-email-pclouds@gmail.com>
Nguyễn Thái Ngọc Duy <pclouds@gmail.com> writes:
Show 5 quoted lines
>  Changing INVALID_PACK to NULL breaks at least t1050.4. We may
>  restructure the p assignments at the end of find_pack_entry() to
>  allow setting INVALID_PACK to NULL.
>  
>  But I'm not really excited about that.

The patch to do so should be a trivial one on top of this anyway (attached).

Show 13 quoted lines
> -		if (p == last_found)
> +		if (p == find_pack_entry_last_found)
>  			p = packed_git;
>  		else
>  			p = p->next;
> -		if (p == last_found)
> +		/*
> +		 * p can be NULL from the else clause above, if initial
> +		 * f_p_e_last_found value (i.e. INVALID_PACK) is NULL, we may
> +		 * advance p again to an imaginary pack in invalid memory
> +		 */
> +		if (p == find_pack_entry_last_found)
>  			p = p->next;

But I think the real issue is that the original loop is written in an obscure way. The conversion in f7c22cc (always start looking up objects in the last used pack first, 2007-05-30) wanted to turn the traversal that always went from the tip of a linked list to instead first probe the promising one, and then scan the list from the tip like it used to do, except that it did not want to probe the one it thought promising again.

So perhaps restructuring the loop by making the logic to probe into a single pack into a helper function, e.g.

static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
{
	if (last_found) {
		if (find_one(sha1, e, last_found))
			return 1;
	}
        for (p = packed_git; p; p = p->next) {
        	if (p == last_found || !find_one(sha1, e, p))
			continue;
		last_found = p;
		return 1;
	}
        return 0;
}
would make the resulting flow far easier to follow, no?
 sha1_file.c |   16 +++++-----------
 1 files changed, 5 insertions(+), 11 deletions(-)
diff --git a/sha1_file.c b/sha1_file.c
index 77512a3..2286789 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -54,9 +54,7 @@ static struct cached_object empty_tree = {
 	0
 };
 
-/* INVALID_PACK cannot be NULL, see comments in find_pack_entry */
-#define INVALID_PACK (struct packed_git *)1
-static struct packed_git *find_pack_entry_last_found = INVALID_PACK;
+static struct packed_git *find_pack_entry_last_found;
 
 static struct cached_object *find_cached_object(const unsigned char *sha1)
 {
@@ -725,7 +723,7 @@ void free_pack_by_name(const char *pack_name)
 			free(p->bad_object_sha1);
 			*pp = p->next;
 			if (find_pack_entry_last_found == p)
-				find_pack_entry_last_found = INVALID_PACK;
+				find_pack_entry_last_found = NULL;
 			free(p);
 			return;
 		}
@@ -2024,7 +2022,7 @@ static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
 	prepare_packed_git();
 	if (!packed_git)
 		return 0;
-	p = find_pack_entry_last_found == INVALID_PACK ? packed_git : find_pack_entry_last_found;
+	p = !find_pack_entry_last_found ? packed_git : find_pack_entry_last_found;
 
 	do {
 		if (p->num_bad_objects) {
@@ -2060,12 +2058,8 @@ static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
 			p = packed_git;
 		else
 			p = p->next;
-		/*
-		 * p can be NULL from the else clause above, if initial
-		 * f_p_e_last_found value (i.e. INVALID_PACK) is NULL, we may
-		 * advance p again to an imaginary pack in invalid memory
-		 */
-		if (p == find_pack_entry_last_found)
+
+		if (p && p == find_pack_entry_last_found)
 			p = p->next;
 	} while (p);
 	return 0;
Previous: Nicolas PitreNext: Nicolas Pitre
Message 5 of 14 in “find_pack_entry(): do not keep packed_git pointer locally”
  1. find_pack_entry(): do not keep packed_git pointer locallyNguyễn Thái Ngọc Duy, Jan 30, 2012
  2. Junio C HamanoJan 30, 2012
  3. Nguyen Thai Ngoc DuyJan 31, 2012
  4. Nicolas PitreJan 31, 2012
  5. Junio C HamanoJan 31, 2012
  6. Nicolas PitreJan 31, 2012
  7. 1/2 Factor find_pack_entry()'s core outNguyễn Thái Ngọc Duy, Feb 1, 2012
  8. 2/2 find_pack_entry(): do not keep packed_git pointer locallyNguyễn Thái Ngọc Duy, Feb 1, 2012
  9. Nicolas PitreFeb 1, 2012
  10. 2/2 find_pack_entry(): do not keep packed_git pointer locallyNguyễn Thái Ngọc Duy, Feb 2, 2012
  11. Nicolas PitreFeb 1, 2012
  12. Junio C HamanoFeb 1, 2012
  13. Junio C HamanoFeb 1, 2012
  14. Nicolas PitreFeb 1, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.