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

[PATCH 06/15] ref_remove_duplicates(): avoid redundant bisection

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Oct 23, 2013, 15:50 UTC
Message-ID
<1382543448-2586-7-git-send-email-mhagger@alum.mit.edu>
In-Reply-To
<1382543448-2586-1-git-send-email-mhagger@alum.mit.edu>

The old code called string_list_lookup(), and if that failed called string_list_insert(), thus doing the bisection search through the string list twice in the latter code path.

Instead, just call string_list_insert() right away. If an entry for that peer reference name already existed, then its util pointer is always non-NULL.

Of course this doesn't change the fact that the repeated string_list_insert() calls make the function scale like O(N^2) if the input reference list is not already approximately sorted.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
The O(N^2) algorithm mentioned above essentially boils down to
    for each reference:
        insert reference into string_list (sorted)

Because the insertion requires later array entries to be shifted to the right, the algorithm scales like O(N^2) if the entries are not already approximately in order.

I can think of three easy ways to fix this:
* Use a hash map instead of a sorted string_list to record which
  entries have already been seen.
* If the order of the result doesn't matter and it doesn't matter
  *which* of the duplicates is retained, then insert all of the names
  into the string_list unsorted, then sort them all in one go, then
  iterate through looking for duplicates and stringing the survivors
  together in a new linked list.
* If the order is important, or it is important that the *first* of
  duplicates be retained, then iterate through the linked list twice.
  The first time, just add the entries to the string_list.  Then sort
  the string_list using a stable sort (e.g., git_qsort()).  Then
  iterate through the linked list a second time as in the current
  code.
See also my comments to commit 10.

However, I believe that the O(N^2) worst case is unlikely to bite in most cases. This function is called from builtin/fetch.c:get_ref_map(), and the input list is the concatenation of several sub-lists. Most sublists come from resolving refspecs, and the last comes from find_non_local_tags(). I believe that each of the sublists is sorted. So the O(N^2) worst-case could only occur if more than one of the sublists is large and at least two of the large sublists are not in the correct order relative to each other.

I tried to concoct such a test case by creating a repository with about 20k branches and 20k tags, instrumenting ref_remove_duplicates(), and then fetching with something like

    git fetch --prune origin refs/heads/*:refs/remotes/origin/* \
                             refs/tags/*:refs/tags/*

Indeed, the running time of ref_remove_duplicates() became significant when the order of the refspecs was reversed, but it was still less than a second out of a much longer command invocation.

So I didn't bother fixing the O(N^2) algorithm. If somebody reports real-world slowness here, we can always come back to it.

 remote.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/remote.c b/remote.c
index e9fedfa..a44e897 100644
--- a/remote.c
+++ b/remote.c
@@ -750,13 +750,15 @@ void ref_remove_duplicates(struct ref *ref_map)
 	struct string_list refs = STRING_LIST_INIT_NODUP;
 	struct string_list_item *item = NULL;
 	struct ref *prev = NULL, *next = NULL;
+
 	for (; ref_map; prev = ref_map, ref_map = next) {
 		next = ref_map->next;
 		if (!ref_map->peer_ref)
 			continue;
 
-		item = string_list_lookup(&refs, ref_map->peer_ref->name);
-		if (item) {
+		item = string_list_insert(&refs, ref_map->peer_ref->name);
+		if (item->util) {
+			/* Entry already existed */
 			if (strcmp(((struct ref *)item->util)->name,
 				   ref_map->name))
 				die("%s tracks both %s and %s",
@@ -767,11 +769,9 @@ void ref_remove_duplicates(struct ref *ref_map)
 			free(ref_map->peer_ref);
 			free(ref_map);
 			ref_map = prev; /* skip this; we freed it */
-			continue;
+		} else {
+			item->util = ref_map;
 		}
-
-		item = string_list_insert(&refs, ref_map->peer_ref->name);
-		item->util = ref_map;
 	}
 	string_list_clear(&refs, 0);
 }
-- 
1.8.4
Previous: Michael HaggertyNext: Michael Haggerty
Message 14 of 34 in “Change semantics of "fetch --tags"”
  1. 00/15 Change semantics of "fetch --tags"Michael Haggerty, Oct 23, 2013
  2. 01/15 t5510: use the correct tag name in testMichael Haggerty, Oct 23, 2013
  3. 02/15 t5510: prepare test refs more straightforwardlyMichael Haggerty, Oct 23, 2013
  4. Junio C HamanoOct 23, 2013
  5. Michael HaggertyOct 24, 2013
  6. Junio C HamanoOct 24, 2013
  7. 03/15 t5510: check that "git fetch --prune --tags" does not prune branchesMichael Haggerty, Oct 23, 2013
  8. 04/15 api-remote.txt: correct section "struct refspect"Michael Haggerty, Oct 23, 2013
  9. Junio C HamanoOct 23, 2013
  10. Michael HaggertyOct 24, 2013
  11. 05/15 get_ref_map(): rename local variablesMichael Haggerty, Oct 23, 2013
  12. Junio C HamanoOct 23, 2013
  13. Michael HaggertyOct 24, 2013
  14. 06/15 ref_remove_duplicates(): avoid redundant bisectionMichael Haggerty, Oct 23, 2013
  15. 07/15 ref_remove_duplicates(): simplify functionMichael Haggerty, Oct 23, 2013
  16. 08/15 ref_remove_duplicates(): improve documentation commentMichael Haggerty, Oct 23, 2013
  17. Junio C HamanoOct 23, 2013
  18. 09/15 builtin/fetch.c: reorder function definitionsMichael Haggerty, Oct 23, 2013
  19. 10/15 fetch --tags: fetch tags *in addition to* other stuffMichael Haggerty, Oct 23, 2013
  20. Junio C HamanoOct 24, 2013
  21. Michael HaggertyOct 25, 2013
  22. Junio C HamanoOct 28, 2013
  23. Michael HaggertyOct 30, 2013
  24. Michael HaggertyOct 26, 2013
  25. 11/15 fetch --prune: prune only based on explicit refspecsMichael Haggerty, Oct 23, 2013
  26. Junio C HamanoOct 24, 2013
  27. Michael HaggertyOct 26, 2013
  28. Junio C HamanoOct 28, 2013
  29. 12/15 query_refspecs(): move some constants out of the loopMichael Haggerty, Oct 23, 2013
  30. 13/15 builtin/remote.c: reorder function definitionsMichael Haggerty, Oct 23, 2013
  31. 14/15 builtin/remote.c:update(): use struct argv_arrayMichael Haggerty, Oct 23, 2013
  32. 15/15 fetch, remote: properly convey --no-prune options to subprocessesMichael Haggerty, Oct 23, 2013
  33. Junio C HamanoOct 24, 2013
  34. Junio C HamanoOct 23, 2013

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.