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

[PATCH v3 05/10] odb: simplify calling `link_alt_odb_entry()`

From
Patrick Steinhardt <ps@pks.im>
Date
Aug 11, 2025, 13:46 UTC
Message-ID
<20250811-b4-pks-midx-deduplicate-source-info-v3-5-e442bdf2b4ad@pks.im>
In-Reply-To
<20250811-b4-pks-midx-deduplicate-source-info-v3-0-e442bdf2b4ad@pks.im>

Callers of `link_alt_odb_entry()` are expected to pass in three different paths:

  - The (potentially relative) path of the object directory that we're
    about to add.
  - The base that should be used to resolve a relative object directory
    path.
  - The resolved path to the object database's objects directory.

Juggling those three paths makes the calling convention somewhat hard to grok at first.

As it turns out, the third parameter is redundant: we always pass in the resolved path of the object database's primary source, and we already pass in the database itself. So instead, we can resolve that path in the function itself.

One downside of this is that one caller of `link_alt_odb_entry()` calls this function in a loop, so we were able to resolve the directory a single time, only. But ultimately, we only ever end up with a rather limited number of alternates anyway, so the extra couple of cycles we save feels more like a micro optimization.

Refactor the code accordingly.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 odb.c | 25 +++++++------------------
 1 file changed, 7 insertions(+), 18 deletions(-)
diff --git a/odb.c b/odb.c
index 0c808bb288..4f884e3b50 100644
--- a/odb.c
+++ b/odb.c
@@ -142,8 +142,7 @@ static void read_info_alternates(struct object_database *odb,
 static struct odb_source *link_alt_odb_entry(struct object_database *odb,
 					     const char *dir,
 					     const char *relative_base,
-					     int depth,
-					     const char *normalized_objdir)
+					     int depth)
 {
 	struct odb_source *alternate = NULL;
 	struct strbuf pathbuf = STRBUF_INIT;
@@ -170,7 +169,10 @@ static struct odb_source *link_alt_odb_entry(struct object_database *odb,
 	while (pathbuf.len && pathbuf.buf[pathbuf.len - 1] == '/')
 		strbuf_setlen(&pathbuf, pathbuf.len - 1);
 
-	if (!alt_odb_usable(odb, &pathbuf, normalized_objdir, &pos))
+	strbuf_reset(&tmp);
+	strbuf_realpath(&tmp, odb->sources->path, 1);
+
+	if (!alt_odb_usable(odb, &pathbuf, tmp.buf, &pos))
 		goto error;
 
 	CALLOC_ARRAY(alternate, 1);
@@ -227,7 +229,6 @@ static const char *parse_alt_odb_entry(const char *string,
 static void link_alt_odb_entries(struct object_database *odb, const char *alt,
 				 int sep, const char *relative_base, int depth)
 {
-	struct strbuf objdirbuf = STRBUF_INIT;
 	struct strbuf dir = STRBUF_INIT;
 
 	if (!alt || !*alt)
@@ -239,17 +240,13 @@ static void link_alt_odb_entries(struct object_database *odb, const char *alt,
 		return;
 	}
 
-	strbuf_realpath(&objdirbuf, odb->sources->path, 1);
-
 	while (*alt) {
 		alt = parse_alt_odb_entry(alt, sep, &dir);
 		if (!dir.len)
 			continue;
-		link_alt_odb_entry(odb, dir.buf,
-				   relative_base, depth, objdirbuf.buf);
+		link_alt_odb_entry(odb, dir.buf, relative_base, depth);
 	}
 	strbuf_release(&dir);
-	strbuf_release(&objdirbuf);
 }
 
 static void read_info_alternates(struct object_database *odb,
@@ -317,20 +314,12 @@ void odb_add_to_alternates_file(struct object_database *odb,
 struct odb_source *odb_add_to_alternates_memory(struct object_database *odb,
 						const char *dir)
 {
-	struct odb_source *alternate;
-	char *objdir;
-
 	/*
 	 * Make sure alternates are initialized, or else our entry may be
 	 * overwritten when they are.
 	 */
 	odb_prepare_alternates(odb);
-
-	objdir = real_pathdup(odb->sources->path, 1);
-	alternate = link_alt_odb_entry(odb, dir, NULL, 0, objdir);
-
-	free(objdir);
-	return alternate;
+	return link_alt_odb_entry(odb, dir, NULL, 0);
 }
 
 struct odb_source *odb_set_temporary_primary_source(struct object_database *odb,
-- 
2.51.0.rc1.163.g2494970778.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 47 of 56 in “midx: stop deduplicating info redundant with their sources”
  1. 0/8 midx: stop deduplicating info redundant with their sourcesPatrick Steinhardt, Jul 29, 2025
  2. 1/8 odb: store locality in object database sourcesPatrick Steinhardt, Jul 29, 2025
  3. Toon ClaesAug 6, 2025
  4. Patrick SteinhardtAug 7, 2025
  5. Karthik NayakAug 7, 2025
  6. 2/8 odb: allow `odb_find_source()` to failPatrick Steinhardt, Jul 29, 2025
  7. 3/8 odb: return newly created in-memory sourcesPatrick Steinhardt, Jul 29, 2025
  8. Toon ClaesAug 6, 2025
  9. Patrick SteinhardtAug 7, 2025
  10. Karthik NayakAug 7, 2025
  11. 4/8 midx: drop redundant `struct repository` parameterPatrick Steinhardt, Jul 29, 2025
  12. 5/8 midx: load multi-pack indices via their sourcePatrick Steinhardt, Jul 29, 2025
  13. Karthik NayakAug 7, 2025
  14. Karthik NayakAug 7, 2025
  15. 6/8 midx: write multi-pack indices via their sourcePatrick Steinhardt, Jul 29, 2025
  16. Karthik NayakAug 7, 2025
  17. 7/8 midx: stop duplicating info redundant with its owning sourcePatrick Steinhardt, Jul 29, 2025
  18. 8/8 midx: compute paths via their sourcePatrick Steinhardt, Jul 29, 2025
  19. Junio C HamanoJul 29, 2025
  20. Patrick SteinhardtJul 30, 2025
  21. 0/9 midx: stop duplicating info redundant with their sourcesPatrick Steinhardt, Aug 7, 2025
  22. 1/9 odb: store locality in object database sourcesPatrick Steinhardt, Aug 7, 2025
  23. Taylor BlauAug 7, 2025
  24. 2/9 odb: allow `odb_find_source()` to failPatrick Steinhardt, Aug 7, 2025
  25. Taylor BlauAug 7, 2025
  26. Patrick SteinhardtAug 11, 2025
  27. 3/9 odb: return newly created in-memory sourcesPatrick Steinhardt, Aug 7, 2025
  28. Taylor BlauAug 7, 2025
  29. Patrick SteinhardtAug 11, 2025
  30. 4/9 odb: simplify calling `link_alt_odb_entry()`Patrick Steinhardt, Aug 7, 2025
  31. Taylor BlauAug 7, 2025
  32. 5/9 midx: drop redundant `struct repository` parameterPatrick Steinhardt, Aug 7, 2025
  33. 6/9 midx: load multi-pack indices via their sourcePatrick Steinhardt, Aug 7, 2025
  34. Taylor BlauAug 7, 2025
  35. 7/9 midx: write multi-pack indices via their sourcePatrick Steinhardt, Aug 7, 2025
  36. Taylor BlauAug 7, 2025
  37. 8/9 midx: stop duplicating info redundant with its owning sourcePatrick Steinhardt, Aug 7, 2025
  38. 9/9 midx: compute paths via their sourcePatrick Steinhardt, Aug 7, 2025
  39. Taylor BlauAug 7, 2025
  40. Patrick SteinhardtAug 11, 2025
  41. Karthik NayakAug 7, 2025
  42. 00/10 midx: stop duplicating info redundant with their sourcesPatrick Steinhardt, Aug 11, 2025
  43. 01/10 odb: store locality in object database sourcesPatrick Steinhardt, Aug 11, 2025
  44. 02/10 odb: allow `odb_find_source()` to failPatrick Steinhardt, Aug 11, 2025
  45. 03/10 odb: consistently use "dir" to refer to alternate's directoryPatrick Steinhardt, Aug 11, 2025
  46. 04/10 odb: return newly created in-memory sourcesPatrick Steinhardt, Aug 11, 2025
  47. 05/10 odb: simplify calling `link_alt_odb_entry()`Patrick Steinhardt, Aug 11, 2025
  48. 06/10 midx: drop redundant `struct repository` parameterPatrick Steinhardt, Aug 11, 2025
  49. 07/10 midx: load multi-pack indices via their sourcePatrick Steinhardt, Aug 11, 2025
  50. 08/10 midx: write multi-pack indices via their sourcePatrick Steinhardt, Aug 11, 2025
  51. 09/10 midx: stop duplicating info redundant with its owning sourcePatrick Steinhardt, Aug 11, 2025
  52. 10/10 midx: compute paths via their sourcePatrick Steinhardt, Aug 11, 2025
  53. Junio C HamanoAug 28, 2025
  54. Taylor BlauAug 29, 2025
  55. Derrick StoleeAug 30, 2025
  56. Patrick SteinhardtSep 2, 2025

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.