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

Re: [PATCH 2/7] submodule.c: convert is_submodule_modified to use strbuf_getwholeline_fd

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Mar 23, 2017, 22:50 UTC
Message-ID
<20170323225019.GD20794@aiede.mtv.corp.google.com>
In-Reply-To
<20170323223338.32274-3-sbeller@google.com>
Stefan Beller wrote:
Show 7 quoted lines
> Instead of implementing line reading yet again, make use of our beautiful
> library functions.
>
> Signed-off-by: Stefan Beller <sbeller@google.com>
> ---
>  submodule.c | 14 ++------------
>  1 file changed, 2 insertions(+), 12 deletions(-)
This changes buffering behavior in two ways:
- by using strbuf_getwholeline_fd instead of strbuf_read, we avoid
  having to allocate memory for the entire child process output at
  once.  That is, we limit maximum memory usage (good).
- by using strbuf_getwholeline_fd instead of strbuf_read, we xread
  one byte at a time instead of larger chunks.  That means more
  overhead due to context switches (bad).

Some callers of getwholeline_fd need the one-byte-at-a-time thing to avoid waiting too long for input, and in some cases the alternative is deadlock. We know this caller doesn't fall into that category because it was doing fine slurping the entire file at once. As the getwholeline_fd API doc comment explains:

 * It reads one character at a time, so it is very slow.  Do not
 * use it unless you need the correct position in the file
 * descriptor.

Can this caller use xfdopen and strbuf_getwholeline instead to get back the benefit of buffering (i.e., something like the below)?

Another consequence of switching to streaming is that we may close before the child finishes. Do we have to worry about handling SIGPIPE in the child? I haven't checked how this handles that --- a test might be useful.

Thanks and hope that helps, Jonathan

diff --git i/submodule.c w/submodule.c
index c1b7b78260..184d5739fc 100644
--- i/submodule.c
+++ w/submodule.c
@@ -1043,6 +1043,7 @@ unsigned is_submodule_modified(const char *path, int ignore_untracked)
 {
 	struct child_process cp = CHILD_PROCESS_INIT;
 	struct strbuf buf = STRBUF_INIT;
+	FILE *fp;
 	unsigned dirty_submodule = 0;
 	const char *git_dir;
 
@@ -1070,7 +1071,8 @@ unsigned is_submodule_modified(const char *path, int ignore_untracked)
 	if (start_command(&cp))
 		die("Could not run 'git status --porcelain' in submodule %s", path);
 
-	while (strbuf_getwholeline_fd(&buf, cp.out, '\n') != EOF) {
+	fp = xfdopen(cp.out, "r");
+	while (strbuf_getwholeline(&buf, fp, '\n') != EOF) {
 		if ((buf.buf[0] == '?') && (buf.buf[1] == '?')) {
 			dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED;
 			if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED)
@@ -1082,7 +1084,7 @@ unsigned is_submodule_modified(const char *path, int ignore_untracked)
 				break;
 		}
 	}
-	close(cp.out);
+	fclose(fp);
 
 	if (finish_command(&cp))
 		die("'git status --porcelain' failed in submodule %s", path);
Previous: Stefan BellerNext: Stefan Beller
Message 26 of 73 in “[PATCHv3 0/3] short status: improve reporting for submodule changes”
  1. Stefan BellerMar 23, 2017
  2. 1/3 submodule.c: port is_submodule_modified to use porcelain 2Stefan Beller, Mar 23, 2017
  3. Jonathan NiederMar 23, 2017
  4. Junio C HamanoMar 23, 2017
  5. Stefan BellerMar 23, 2017
  6. 2/3 submodule.c, is_submodule_modified: stricter checking for submodulesStefan Beller, Mar 23, 2017
  7. Jonathan NiederMar 23, 2017
  8. 3/3 short status: improve reporting for submodule changesStefan Beller, Mar 23, 2017
  9. Jonathan NiederMar 23, 2017
  10. 0/8 short status: improve reporting for submodule changesStefan Beller, Mar 23, 2017
  11. 3/8 submodule.c: convert is_submodule_modified to use strbuf_getwholeline_fdStefan Beller, Mar 23, 2017
  12. 1/8 submodule.c: port is_submodule_modified to use porcelain 2Stefan Beller, Mar 23, 2017
  13. 5/8 submodule.c: factor out early loop termination in is_submodule_modifiedStefan Beller, Mar 23, 2017
  14. 2/8 submodule.c: use argv_array in is_submodule_modifiedStefan Beller, Mar 23, 2017
  15. 4/8 submodule.c: port is_submodule_modified to use porcelain 2Stefan Beller, Mar 23, 2017
  16. 8/8 submodule.c: correctly handle nested submodules in is_submodule_modifiedStefan Beller, Mar 23, 2017
  17. 6/8 submodule.c: stricter checking for submodules in is_submodule_modifiedStefan Beller, Mar 23, 2017
  18. 7/8 short status: improve reporting for submodule changesStefan Beller, Mar 23, 2017
  19. Stefan BellerMar 23, 2017
  20. 0/7 short status: improve reporting for submodule changesStefan Beller, Mar 23, 2017
  21. 1/7 submodule.c: use argv_array in is_submodule_modifiedStefan Beller, Mar 23, 2017
  22. Jonathan NiederMar 23, 2017
  23. 4/7 submodule.c: factor out early loop termination in is_submodule_modifiedStefan Beller, Mar 23, 2017
  24. 5/7 submodule.c: stricter checking for submodules in is_submodule_modifiedStefan Beller, Mar 23, 2017
  25. 2/7 submodule.c: convert is_submodule_modified to use strbuf_getwholeline_fdStefan Beller, Mar 23, 2017
  26. Jonathan NiederMar 23, 2017
  27. Stefan BellerMar 23, 2017
  28. Stefan BellerMar 23, 2017
  29. 6/7 short status: improve reporting for submodule changesStefan Beller, Mar 23, 2017
  30. 0/7 short status: improve reporting for submodule changesStefan Beller, Mar 24, 2017
  31. 1/7 submodule.c: use argv_array in is_submodule_modifiedStefan Beller, Mar 24, 2017
  32. Jonathan NiederMar 24, 2017
  33. 2/7 submodule.c: factor out early loop termination in is_submodule_modifiedStefan Beller, Mar 24, 2017
  34. Jonathan NiederMar 24, 2017
  35. 3/7 submodule.c: convert is_submodule_modified to use strbuf_getwholelineStefan Beller, Mar 24, 2017
  36. Jonathan NiederMar 24, 2017
  37. Stefan BellerMar 25, 2017
  38. 5/7 submodule.c: stricter checking for submodules in is_submodule_modifiedStefan Beller, Mar 24, 2017
  39. Jonathan NiederMar 24, 2017
  40. 6/7 short status: improve reporting for submodule changesStefan Beller, Mar 24, 2017
  41. Jonathan NiederMar 24, 2017
  42. 7/7 submodule.c: correctly handle nested submodules in is_submodule_modifiedStefan Beller, Mar 24, 2017
  43. Jonathan NiederMar 24, 2017
  44. Stefan BellerMar 25, 2017
  45. 4/7 submodule.c: port is_submodule_modified to use porcelain 2Stefan Beller, Mar 24, 2017
  46. Jonathan NiederMar 24, 2017
  47. 0/7 short status: improve reporting for submodule changesStefan Beller, Mar 25, 2017
  48. 1/7 submodule.c: use argv_array in is_submodule_modifiedStefan Beller, Mar 25, 2017
  49. 2/7 submodule.c: factor out early loop termination in is_submodule_modifiedStefan Beller, Mar 25, 2017
  50. 3/7 submodule.c: convert is_submodule_modified to use strbuf_getwholelineStefan Beller, Mar 25, 2017
  51. 4/7 submodule.c: port is_submodule_modified to use porcelain 2Stefan Beller, Mar 25, 2017
  52. 7/7 submodule.c: correctly handle nested submodules in is_submodule_modifiedStefan Beller, Mar 25, 2017
  53. Junio C HamanoMar 27, 2017
  54. Jonathan NiederMar 28, 2017
  55. Junio C HamanoMar 30, 2017
  56. Stefan BellerMar 28, 2017
  57. 6/7 short status: improve reporting for submodule changesStefan Beller, Mar 25, 2017
  58. 5/7 submodule.c: stricter checking for submodules in is_submodule_modifiedStefan Beller, Mar 25, 2017
  59. Jonathan NiederMar 25, 2017
  60. 0/7 short status: improve reporting for submodule changesStefan Beller, Mar 28, 2017
  61. 2/2 submodule.c: correctly handle nested submodules in is_submodule_modifiedStefan Beller, Mar 28, 2017
  62. Jonathan NiederMar 28, 2017
  63. Stefan BellerMar 29, 2017
  64. 1/2 short status: improve reporting for submodule changesStefan Beller, Mar 28, 2017
  65. Jonathan NiederMar 28, 2017
  66. [PATCHv9 (6,7)/7] short status: improve reporting for submodule changesStefan Beller, Mar 29, 2017
  67. 1/2 short status: improve reporting for submodule changesStefan Beller, Mar 29, 2017
  68. Jonathan NiederMar 29, 2017
  69. 2/2 submodule.c: correctly handle nested submodules in is_submodule_modifiedStefan Beller, Mar 29, 2017
  70. Jonathan NiederMar 29, 2017
  71. Junio C HamanoMar 30, 2017
  72. 3/7 submodule.c: port is_submodule_modified to use porcelain 2Stefan Beller, Mar 23, 2017
  73. 7/7 submodule.c: correctly handle nested submodules in is_submodule_modifiedStefan Beller, Mar 23, 2017

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.