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

[PATCH v2 31/51] refs: sort ref_dirs lazily

From
mhagger@alum.mit.edu <mhagger@alum.mit.edu>
Date
Dec 12, 2011, 05:38 UTC
Message-ID
<1323668338-1764-32-git-send-email-mhagger@alum.mit.edu>
In-Reply-To
<1323668338-1764-1-git-send-email-mhagger@alum.mit.edu>
From: Michael Haggerty <mhagger@alum.mit.edu>

Sort ref_dirs lazily, when the ordering is needed: for searching via search_ref_dir(), and when iterating over the references via do_for_each_ref_in_dir() and do_for_each_ref_in_dirs().

This change means that we never have to sort directories recursively, so change sort_ref_dirs() to not recurse.

NOTE: the dirs can now be sorted as a side-effect of other function
calls.  Therefore, it would be problematic to do something from a
each_ref_fn callback that could provoke the sorting of the directory
that is currently being iterated over.  This is not so likely, because
a directory is always sorted just before being iterated over and thus
can be searched through during the iteration without causing a
re-sort.  But if a callback function would add a reference to a parent
directory of the reference in the iteration, then try to resolve a
reference under that directory, inconsistency could result.

Add a comment in refs.h warning against modifications during iteration.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 refs.c |   35 +++++++++++++++--------------------
 refs.h |    7 +++++--
 2 files changed, 20 insertions(+), 22 deletions(-)
diff --git a/refs.c b/refs.c
index ce141ea..f01da78 100644
--- a/refs.c
+++ b/refs.c
@@ -256,9 +256,14 @@ static struct ref_entry *search_ref_dir(struct ref_dir *dir, const char *refname
 
 	/*
 	 * We need dir to be sorted so that binary search works.
-	 * Calling sort_ref_dir() here is not quite as terribly
-	 * inefficient as it looks, because directories that are
-	 * already sorted are not re-sorted.
+	 * Calling sort_ref_dir() here is not as terribly inefficient
+	 * as it looks.  (1) If the directory is already sorted, it is
+	 * not re-sorted. (2) When adding a reference,
+	 * search_ref_dir() is only called to find the containing
+	 * subdirectories; there is no search of the directory to
+	 * which the reference will be stored.  Thus adding a bunch of
+	 * references one after the other to a single subdirectory
+	 * doesn't require *any* intermediate sorting.
 	 */
 	sort_ref_dir(dir);
 
@@ -364,26 +369,16 @@ static int is_dup_ref(const struct ref_entry *ref1, const struct ref_entry *ref2
 }
 
 /*
- * Sort the entries in dir and its subdirectories (if they are not
- * already sorted).
+ * Sort the entries in dir (if they are not already sorted).  Sort
+ * only dir itself, not its subdirectories.
  */
 static void sort_ref_dir(struct ref_dir *dir)
 {
 	int i, j;
 	struct ref_entry *last = NULL;
 
-	if (dir->sorted == dir->nr) {
-		/*
-		 * This directory is already sorted and de-duped, but
-		 * we still have to sort subdirectories.
-		 */
-		for (i = 0; i < dir->nr; i++) {
-			struct ref_entry *entry = dir->entries[i];
-			if (entry->flag & REF_DIR)
-				sort_ref_dir(&entry->u.subdir);
-		}
-		return;
-	}
+	if (dir->sorted == dir->nr)
+		return; /* This directory is already sorted and de-duped */
 
 	qsort(dir->entries, dir->nr, sizeof(*dir->entries), ref_entry_cmp);
 
@@ -393,7 +388,6 @@ static void sort_ref_dir(struct ref_dir *dir)
 		if (last && is_dup_ref(last, entry)) {
 			free_ref_entry(entry);
 		} else if (entry->flag & REF_DIR) {
-			sort_ref_dir(&entry->u.subdir);
 			dir->entries[i++] = entry;
 			last = NULL;
 		} else {
@@ -430,6 +424,7 @@ static int do_for_each_ref_in_dir(struct ref_dir *dir, int offset,
 				  each_ref_fn fn, int trim, int flags, void *cb_data)
 {
 	int i;
+	sort_ref_dir(dir);
 	for (i = offset; i < dir->nr; i++) {
 		struct ref_entry *entry = dir->entries[i];
 		int retval;
@@ -453,6 +448,8 @@ static int do_for_each_ref_in_dirs(struct ref_dir *dir1,
 	int retval;
 	int i1 = 0, i2 = 0;
 
+	sort_ref_dir(dir1);
+	sort_ref_dir(dir2);
 	while (1) {
 		struct ref_entry *e1, *e2, *entry;
 		int cmp;
@@ -701,7 +698,6 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)
 		    !get_sha1_hex(refline + 1, sha1))
 			hashcpy(last->u.value.peeled, sha1);
 	}
-	sort_ref_dir(dir);
 }
 
 void add_extra_ref(const char *refname, const unsigned char *sha1, int flag)
@@ -803,7 +799,6 @@ static struct ref_dir *get_loose_refs(struct ref_cache *refs)
 {
 	if (!refs->did_loose) {
 		get_ref_dir(refs, "refs", &refs->loose);
-		sort_ref_dir(&refs->loose);
 		refs->did_loose = 1;
 	}
 	return &refs->loose;
diff --git a/refs.h b/refs.h
index d498291..5bb4678 100644
--- a/refs.h
+++ b/refs.h
@@ -15,8 +15,11 @@ struct ref_lock {
 #define REF_ISBROKEN 0x04
 
 /*
- * Calls the specified function for each ref file until it returns nonzero,
- * and returns the value
+ * Calls the specified function for each ref file until it returns
+ * nonzero, and returns the value.  Please note that it is not safe to
+ * modify references while an iteration is in progress, unless the
+ * same callback function invocation that modifies the reference also
+ * returns a nonzero value to immediately stop the iteration.
  */
 typedef int each_ref_fn(const char *refname, const unsigned char *sha1, int flags, void *cb_data);
 extern int head_ref(each_ref_fn, void *);
-- 
1.7.8
Previous: Junio C HamanoNext: mhagger@alum.mit.edu
Message 58 of 79 in “ref-api-C and ref-api-D re-roll”
  1. 00/51 ref-api-C and ref-api-D re-rollmhagger@alum.mit.edu, Dec 12, 2011
  2. 01/51 struct ref_entry: document name membermhagger@alum.mit.edu, Dec 12, 2011
  3. 02/51 refs: rename "refname" variablesmhagger@alum.mit.edu, Dec 12, 2011
  4. Junio C HamanoDec 13, 2011
  5. 03/51 refs: rename parameters result -> sha1mhagger@alum.mit.edu, Dec 12, 2011
  6. 04/51 clear_ref_array(): rename from free_ref_array()mhagger@alum.mit.edu, Dec 12, 2011
  7. 05/51 is_refname_available(): remove the "quiet" argumentmhagger@alum.mit.edu, Dec 12, 2011
  8. 06/51 parse_ref_line(): add docstringmhagger@alum.mit.edu, Dec 12, 2011
  9. 07/51 add_ref(): add docstringmhagger@alum.mit.edu, Dec 12, 2011
  10. 08/51 is_dup_ref(): extract function from sort_ref_array()mhagger@alum.mit.edu, Dec 12, 2011
  11. Jeff KingDec 12, 2011
  12. Michael HaggertyDec 12, 2011
  13. Junio C HamanoDec 12, 2011
  14. Junio C HamanoDec 12, 2011
  15. Michael HaggertyDec 13, 2011
  16. Michael HaggertyDec 13, 2011
  17. 09/51 refs: change signatures of get_packed_refs() and get_loose_refs()mhagger@alum.mit.edu, Dec 12, 2011
  18. 10/51 get_ref_dir(): change signaturemhagger@alum.mit.edu, Dec 12, 2011
  19. 11/51 resolve_gitlink_ref(): improve docstringmhagger@alum.mit.edu, Dec 12, 2011
  20. 12/51 Pass a (ref_cache *) to the resolve_gitlink_*() helper functionsmhagger@alum.mit.edu, Dec 12, 2011
  21. 13/51 resolve_gitlink_ref_recursive(): change to work with struct ref_cachemhagger@alum.mit.edu, Dec 12, 2011
  22. 14/51 repack_without_ref(): remove temporarymhagger@alum.mit.edu, Dec 12, 2011
  23. 15/51 create_ref_entry(): extract function from add_ref()mhagger@alum.mit.edu, Dec 12, 2011
  24. 16/51 add_ref(): take a (struct ref_entry *) parametermhagger@alum.mit.edu, Dec 12, 2011
  25. 17/51 do_for_each_ref(): correctly terminate while processesing extra_refsmhagger@alum.mit.edu, Dec 12, 2011
  26. Junio C HamanoDec 12, 2011
  27. 18/51 do_for_each_ref_in_array(): new functionmhagger@alum.mit.edu, Dec 12, 2011
  28. 19/51 do_for_each_ref_in_arrays(): new functionmhagger@alum.mit.edu, Dec 12, 2011
  29. 20/51 repack_without_ref(): reimplement using do_for_each_ref_in_array()mhagger@alum.mit.edu, Dec 12, 2011
  30. Junio C HamanoDec 12, 2011
  31. 21/51 names_conflict(): new function, extracted from is_refname_available()mhagger@alum.mit.edu, Dec 12, 2011
  32. 22/51 names_conflict(): simplify implementationmhagger@alum.mit.edu, Dec 12, 2011
  33. 23/51 is_refname_available(): reimplement using do_for_each_ref_in_array()mhagger@alum.mit.edu, Dec 12, 2011
  34. 24/51 refs.c: reorder definitions more logicallymhagger@alum.mit.edu, Dec 12, 2011
  35. 25/51 free_ref_entry(): new functionmhagger@alum.mit.edu, Dec 12, 2011
  36. 26/51 check_refname_component(): return 0 for zero-length componentsmhagger@alum.mit.edu, Dec 12, 2011
  37. 27/51 struct ref_entry: nest the value part in a unionmhagger@alum.mit.edu, Dec 12, 2011
  38. 28/51 refs.c: rename ref_array -> ref_dirmhagger@alum.mit.edu, Dec 12, 2011
  39. Junio C HamanoDec 13, 2011
  40. Michael HaggertyDec 13, 2011
  41. Junio C HamanoDec 13, 2011
  42. Michael HaggertyDec 13, 2011
  43. Junio C HamanoDec 13, 2011
  44. Michael HaggertyDec 13, 2011
  45. Junio C HamanoDec 13, 2011
  46. Junio C HamanoDec 14, 2011
  47. Jeff KingDec 14, 2011
  48. Michael HaggertyDec 15, 2011
  49. Jeff KingDec 15, 2011
  50. Michael HaggertyJan 17, 2012
  51. Michael HaggertyFeb 10, 2012
  52. Jeff KingFeb 10, 2012
  53. Junio C HamanoFeb 10, 2012
  54. Michael HaggertyFeb 11, 2012
  55. 29/51 refs: store references hierarchicallymhagger@alum.mit.edu, Dec 12, 2011
  56. 30/51 sort_ref_dir(): do not sort if already sortedmhagger@alum.mit.edu, Dec 12, 2011
  57. Junio C HamanoDec 12, 2011
  58. 31/51 refs: sort ref_dirs lazilymhagger@alum.mit.edu, Dec 12, 2011
  59. 32/51 do_for_each_ref(): only iterate over the subtree that was requestedmhagger@alum.mit.edu, Dec 12, 2011
  60. 33/51 get_ref_dir(): keep track of the current ref_dirmhagger@alum.mit.edu, Dec 12, 2011
  61. 34/51 refs: wrap top-level ref_dirs in ref_entriesmhagger@alum.mit.edu, Dec 12, 2011
  62. 35/51 get_packed_refs(): return (ref_entry *) instead of (ref_dir *)mhagger@alum.mit.edu, Dec 12, 2011
  63. 36/51 get_loose_refs(): return (ref_entry *) instead of (ref_dir *)mhagger@alum.mit.edu, Dec 12, 2011
  64. 37/51 is_refname_available(): take (ref_entry *) instead of (ref_dir *)mhagger@alum.mit.edu, Dec 12, 2011
  65. 38/51 find_ref(): take (ref_entry *) instead of (ref_dir *)mhagger@alum.mit.edu, Dec 12, 2011
  66. 39/51 read_packed_refs(): take (ref_entry *) instead of (ref_dir *)mhagger@alum.mit.edu, Dec 12, 2011
  67. 40/51 add_ref(): take (ref_entry *) instead of (ref_dir *)mhagger@alum.mit.edu, Dec 12, 2011
  68. 41/51 find_containing_direntry(): use (ref_entry *) instead of (ref_dir *)mhagger@alum.mit.edu, Dec 12, 2011
  69. 42/51 search_ref_dir(): take (ref_entry *) instead of (ref_dir *)mhagger@alum.mit.edu, Dec 12, 2011
  70. 43/51 add_entry(): take (ref_entry *) instead of (ref_dir *)mhagger@alum.mit.edu, Dec 12, 2011
  71. 44/51 do_for_each_ref_in_dir*(): take (ref_entry *) instead of (ref_dir *)mhagger@alum.mit.edu, Dec 12, 2011
  72. 45/51 sort_ref_dir(): take (ref_entry *) instead of (ref_dir *)mhagger@alum.mit.edu, Dec 12, 2011
  73. 46/51 struct ref_dir: store a reference to the enclosing ref_cachemhagger@alum.mit.edu, Dec 12, 2011
  74. 47/51 read_loose_refs(): take a (ref_entry *) as argumentmhagger@alum.mit.edu, Dec 12, 2011
  75. 48/51 refs: read loose references lazilymhagger@alum.mit.edu, Dec 12, 2011
  76. 49/51 is_refname_available(): query only possibly-conflicting referencesmhagger@alum.mit.edu, Dec 12, 2011
  77. 50/51 read_packed_refs(): keep track of the directory being worked inmhagger@alum.mit.edu, Dec 12, 2011
  78. 51/51 repack_without_ref(): call clear_packed_ref_cache()mhagger@alum.mit.edu, Dec 12, 2011
  79. Junio C HamanoDec 12, 2011

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.