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

Re: [PATCH v3 2/4] object-file: refactor map_loose_object_1()

From
Jeff King <peff@peff.net>
Date
Dec 9, 2022, 02:00 UTC
Message-ID
<Y5KWpXwxdRI4QPNl@coredump.intra.peff.net>
In-Reply-To
<7419e4ac7053ab2d89a4cdc4612e5baeca48ce9f.1670532905.git.jonathantanmy@google.com>
On Thu, Dec 08, 2022 at 12:57:06PM -0800, Jonathan Tan wrote:
Show 9 quoted lines
> This function can do 3 things:
>  1. Gets an fd given a path
>  2. Simultaneously gets a path and fd given an OID
>  3. Memory maps an fd
> 
> Split this function up. Only one caller needs 1, so inline that. As for
> 2, a future patch will also need this functionality and, in addition,
> the calculated path, so extract this into a separate function with an
> out parameter for the path.

This is moving in a good direction. I like the name "map_fd" for the helper. Being able to give it a useful name like that is a good clue that it is doing a more focused and understandable job than the generic map_loose_object_1(). :)

In fact...
Show 22 quoted lines
> +static void *map_loose_object_1(struct repository *r,
> +				const struct object_id *oid,
> +				unsigned long *size,
> +				const char **path)
> +{
> +	const char *p;
> +	int fd = open_loose_object(r, oid, &p);
> +
> +	if (fd < 0)
> +		return NULL;
> +	if (path)
> +		*path = p;
> +	return map_fd(fd, p, size);
> +}
> +
>  void *map_loose_object(struct repository *r,
>  		       const struct object_id *oid,
>  		       unsigned long *size)
>  {
> -	return map_loose_object_1(r, NULL, oid, size);
> +	return map_loose_object_1(r, oid, size, NULL);
>  }

If you take my suggestion on patch 3, then the only other caller of map_loose_object_1() goes away, and we can fold it all into one reasonably-named function:

diff --git a/object-file.c b/object-file.c
index d99d05839f..429e3a746d 100644
--- a/object-file.c
+++ b/object-file.c
@@ -1233,28 +1233,18 @@ static void *map_fd(int fd, const char *path, unsigned long *size)
 	return map;
 }
 
-static void *map_loose_object_1(struct repository *r,
-				const struct object_id *oid,
-				unsigned long *size,
-				const char **path)
+void *map_loose_object(struct repository *r,
+		       const struct object_id *oid,
+		       unsigned long *size)
 {
 	const char *p;
 	int fd = open_loose_object(r, oid, &p);
 
 	if (fd < 0)
 		return NULL;
-	if (path)
-		*path = p;
 	return map_fd(fd, p, size);
 }
 
-void *map_loose_object(struct repository *r,
-		       const struct object_id *oid,
-		       unsigned long *size)
-{
-	return map_loose_object_1(r, oid, size, NULL);
-}
-
 enum unpack_loose_header_result unpack_loose_header(git_zstream *stream,
 						    unsigned char *map,
 						    unsigned long mapsize,

-Peff
Previous: Jonathan TanNext: Jonathan Tan
Message 47 of 85 in “Don't lazy-fetch commits when parsing them”
  1. 0/4 Don't lazy-fetch commits when parsing themJonathan Tan, Nov 30, 2022
  2. 1/4 object-file: reread object with exact same argsJonathan Tan, Nov 30, 2022
  3. 2/4 object-file: refactor corrupt object diagnosisJonathan Tan, Nov 30, 2022
  4. Jeff KingNov 30, 2022
  5. Junio C HamanoNov 30, 2022
  6. Jonathan TanDec 1, 2022
  7. 3/4 object-file: refactor replace object lookupJonathan Tan, Nov 30, 2022
  8. Jeff KingNov 30, 2022
  9. 4/4 commit: don't lazy-fetch commitsJonathan Tan, Nov 30, 2022
  10. Jeff KingNov 30, 2022
  11. Jonathan TanDec 1, 2022
  12. Jeff KingDec 1, 2022
  13. Junio C HamanoNov 30, 2022
  14. Jeff KingNov 30, 2022
  15. 0/4 Don't lazy-fetch commits when parsing themJonathan Tan, Dec 1, 2022
  16. 1/4 object-file: reread object with exact same argsJonathan Tan, Dec 1, 2022
  17. 3/4 object-file: refactor replace object lookupJonathan Tan, Dec 1, 2022
  18. 2/4 object-file: refactor corrupt object diagnosisJonathan Tan, Dec 1, 2022
  19. 4/4 commit: don't lazy-fetch commitsJonathan Tan, Dec 1, 2022
  20. Jeff KingDec 1, 2022
  21. Jonathan TanDec 1, 2022
  22. Jeff KingDec 2, 2022
  23. Jonathan TanDec 6, 2022
  24. Jeff KingDec 6, 2022
  25. Junio C HamanoDec 1, 2022
  26. 0/3 Don't lazy-fetch commits when parsing themJonathan Tan, Dec 7, 2022
  27. 1/3 object-file: don't exit early if skipping looseJonathan Tan, Dec 7, 2022
  28. Junio C HamanoDec 7, 2022
  29. Jeff KingDec 7, 2022
  30. Junio C HamanoDec 7, 2022
  31. Jonathan TanDec 7, 2022
  32. 2/3 object-file: emit corruption errors when detectedJonathan Tan, Dec 7, 2022
  33. Junio C HamanoDec 7, 2022
  34. Ævar Arnfjörð BjarmasonDec 7, 2022
  35. Jeff KingDec 7, 2022
  36. Ævar Arnfjörð BjarmasonDec 7, 2022
  37. Jonathan TanDec 7, 2022
  38. Ævar Arnfjörð BjarmasonDec 7, 2022
  39. Jeff KingDec 8, 2022
  40. Jeff KingDec 7, 2022
  41. 3/3 commit: don't lazy-fetch commitsJonathan Tan, Dec 7, 2022
  42. Junio C HamanoDec 7, 2022
  43. Jeff KingDec 7, 2022
  44. 0/4 Don't lazy-fetch commits when parsing themJonathan Tan, Dec 8, 2022
  45. 1/4 object-file: remove OBJECT_INFO_IGNORE_LOOSEJonathan Tan, Dec 8, 2022
  46. 2/4 object-file: refactor map_loose_object_1()Jonathan Tan, Dec 8, 2022
  47. Jeff KingDec 9, 2022
  48. Jonathan TanDec 9, 2022
  49. Jeff KingDec 9, 2022
  50. Jeff KingDec 9, 2022
  51. 3/4 object-file: emit corruption errors when detectedJonathan Tan, Dec 8, 2022
  52. Jeff KingDec 9, 2022
  53. Jonathan TanDec 9, 2022
  54. Ævar Arnfjörð BjarmasonDec 9, 2022
  55. Jonathan TanDec 9, 2022
  56. 4/4 commit: don't lazy-fetch commitsJonathan Tan, Dec 8, 2022
  57. Ævar Arnfjörð BjarmasonDec 9, 2022
  58. 0/4 Don't lazy-fetch commits when parsing themJonathan Tan, Dec 9, 2022
  59. 1/4 object-file: remove OBJECT_INFO_IGNORE_LOOSEJonathan Tan, Dec 9, 2022
  60. 2/4 object-file: refactor map_loose_object_1()Jonathan Tan, Dec 9, 2022
  61. 3/4 object-file: emit corruption errors when detectedJonathan Tan, Dec 9, 2022
  62. Junio C HamanoDec 10, 2022
  63. Jonathan TanDec 12, 2022
  64. Jeff KingDec 12, 2022
  65. Jonathan TanDec 12, 2022
  66. Jeff KingDec 12, 2022
  67. Jonathan TanDec 12, 2022
  68. Jeff KingDec 12, 2022
  69. Jonathan TanDec 12, 2022
  70. Jeff KingDec 13, 2022
  71. 4/4 commit: don't lazy-fetch commitsJonathan Tan, Dec 9, 2022
  72. 0/4 Don't lazy-fetch commits when parsing themJonathan Tan, Dec 12, 2022
  73. 1/4 object-file: remove OBJECT_INFO_IGNORE_LOOSEJonathan Tan, Dec 12, 2022
  74. 2/4 object-file: refactor map_loose_object_1()Jonathan Tan, Dec 12, 2022
  75. 4/4 commit: don't lazy-fetch commitsJonathan Tan, Dec 12, 2022
  76. 3/4 object-file: emit corruption errors when detectedJonathan Tan, Dec 12, 2022
  77. Junio C HamanoDec 13, 2022
  78. Jeff KingDec 13, 2022
  79. 0/4 Don't lazy-fetch commits when parsing themJonathan Tan, Dec 14, 2022
  80. 1/4 object-file: remove OBJECT_INFO_IGNORE_LOOSEJonathan Tan, Dec 14, 2022
  81. 2/4 object-file: refactor map_loose_object_1()Jonathan Tan, Dec 14, 2022
  82. 3/4 object-file: emit corruption errors when detectedJonathan Tan, Dec 14, 2022
  83. 4/4 commit: don't lazy-fetch commitsJonathan Tan, Dec 14, 2022
  84. Jeff KingDec 14, 2022
  85. Junio C HamanoDec 15, 2022

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.