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

Re: [PATCH 3/6] fast-import: release unfreed strbufs

From
Patrick Steinhardt <ps@pks.im>
Date
Mar 28, 2024, 08:21 UTC
Message-ID
<ZgUod3QKptINaPBF@tanuki>
In-Reply-To
<20240322000304.76810-4-thalia@archibald.dev>
On Fri, Mar 22, 2024 at 12:03:33AM +0000, Thalia Archibald wrote:
Show 18 quoted lines
> These strbufs are owned. Release them at the end of their scopes.
> 
> Signed-off-by: Thalia Archibald <thalia@archibald.dev>
> ---
>  builtin/fast-import.c | 29 ++++++++++++++++++-----------
>  1 file changed, 18 insertions(+), 11 deletions(-)
> 
> diff --git a/builtin/fast-import.c b/builtin/fast-import.c
> index 1b3d6784c1..d6f998f363 100644
> --- a/builtin/fast-import.c
> +++ b/builtin/fast-import.c
> @@ -2364,6 +2364,7 @@ static void file_change_m(const char *p, struct branch *b)
>  	/* Git does not track empty, non-toplevel directories. */
>  	if (S_ISDIR(mode) && is_empty_tree_oid(&oid) && *path.buf) {
>  		tree_content_remove(&b->branch_tree, path.buf, NULL, 0);
> +		strbuf_release(&path);
>  		return;
>  	}

Oh, now you get to my comment in the preceding patch. With this patch we're now in a somewhat weird in-between state where the buffers are still static, but we release their memory after each call. So we kind of get the worst of both worlds: static variables without being able to reuse the buffer's memory.

If we were to change this then we should definitely mark the buffers as non-static. If so, it would be great to demonstrate that this does not significantly impact performance.

The same is true for all the other instances.
Patrick
Show 87 quoted lines
> @@ -2409,11 +2410,11 @@ static void file_change_m(const char *p, struct branch *b)
>  				command_buf.buf);
>  	}
>  
> -	if (!*path.buf) {
> +	if (*path.buf)
> +		tree_content_set(&b->branch_tree, path.buf, &oid, mode, NULL);
> +	else
>  		tree_content_replace(&b->branch_tree, &oid, mode, NULL);
> -		return;
> -	}
> -	tree_content_set(&b->branch_tree, path.buf, &oid, mode, NULL);
> +	strbuf_release(&path);
>  }
>  
>  static void file_change_d(const char *p, struct branch *b)
> @@ -2422,6 +2423,7 @@ static void file_change_d(const char *p, struct branch *b)
>  
>  	parse_path_eol(&path, p, "path");
>  	tree_content_remove(&b->branch_tree, path.buf, NULL, 1);
> +	strbuf_release(&path);
>  }
>  
>  static void file_change_cr(const char *p, struct branch *b, int rename)
> @@ -2440,17 +2442,18 @@ static void file_change_cr(const char *p, struct branch *b, int rename)
>  		tree_content_get(&b->branch_tree, source.buf, &leaf, 1);
>  	if (!leaf.versions[1].mode)
>  		die("Path %s not in branch", source.buf);
> -	if (!*dest.buf) {	/* C "path/to/subdir" "" */
> +	if (*dest.buf)
> +		tree_content_set(&b->branch_tree, dest.buf,
> +			&leaf.versions[1].oid,
> +			leaf.versions[1].mode,
> +			leaf.tree);
> +	else	/* C "path/to/subdir" "" */
>  		tree_content_replace(&b->branch_tree,
>  			&leaf.versions[1].oid,
>  			leaf.versions[1].mode,
>  			leaf.tree);
> -		return;
> -	}
> -	tree_content_set(&b->branch_tree, dest.buf,
> -		&leaf.versions[1].oid,
> -		leaf.versions[1].mode,
> -		leaf.tree);
> +	strbuf_release(&source);
> +	strbuf_release(&dest);
>  }
>  
>  static void note_change_n(const char *p, struct branch *b, unsigned char *old_fanout)
> @@ -2804,6 +2807,7 @@ static void parse_new_commit(const char *arg)
>  	free(author);
>  	free(committer);
>  	free(encoding);
> +	strbuf_release(&msg);
>  
>  	if (!store_object(OBJ_COMMIT, &new_data, NULL, &b->oid, next_mark))
>  		b->pack_id = pack_id;
> @@ -2886,6 +2890,7 @@ static void parse_new_tag(const char *arg)
>  	strbuf_addch(&new_data, '\n');
>  	strbuf_addbuf(&new_data, &msg);
>  	free(tagger);
> +	strbuf_release(&msg);
>  
>  	if (store_object(OBJ_TAG, &new_data, NULL, &t->oid, next_mark))
>  		t->pack_id = MAX_PACK_ID;
> @@ -3171,6 +3176,7 @@ static void print_ls(int mode, const unsigned char *hash, const char *path)
>  		strbuf_addch(&line, '\n');
>  	}
>  	cat_blob_write(line.buf, line.len);
> +	strbuf_release(&line);
>  }
>  
>  static void parse_ls(const char *p, struct branch *b)
> @@ -3206,6 +3212,7 @@ static void parse_ls(const char *p, struct branch *b)
>  		release_tree_content_recursive(leaf.tree);
>  	if (!b || root != &b->branch_tree)
>  		release_tree_entry(root);
> +	strbuf_release(&path);
>  }
>  
>  static void checkpoint(void)
> -- 
> 2.44.0
> 
> 
> 
Previous: Thalia ArchibaldNext: Thalia Archibald
Message 6 of 84 in “fast-import: tighten parsing of paths”
  1. 0/6 fast-import: tighten parsing of pathsThalia Archibald, Mar 22, 2024
  2. 1/6 fast-import: tighten parsing of pathsThalia Archibald, Mar 22, 2024
  3. Thalia ArchibaldMar 22, 2024
  4. Patrick SteinhardtMar 28, 2024
  5. 3/6 fast-import: release unfreed strbufsThalia Archibald, Mar 22, 2024
  6. Patrick SteinhardtMar 28, 2024
  7. Thalia ArchibaldApr 1, 2024
  8. 2/6 fast-import: directly use strbufs for pathsThalia Archibald, Mar 22, 2024
  9. Patrick SteinhardtMar 28, 2024
  10. 4/6 fast-import: remove dead strbufThalia Archibald, Mar 22, 2024
  11. Patrick SteinhardtMar 28, 2024
  12. 6/6 fast-import: forbid escaped NUL in pathsThalia Archibald, Mar 22, 2024
  13. 5/6 fast-import: document C-style escapes for pathsThalia Archibald, Mar 22, 2024
  14. Patrick SteinhardtMar 28, 2024
  15. Thalia ArchibaldApr 1, 2024
  16. 0/8 fast-import: tighten parsing of pathsThalia Archibald, Apr 1, 2024
  17. 1/8 fast-import: tighten path unquotingThalia Archibald, Apr 1, 2024
  18. Patrick SteinhardtApr 10, 2024
  19. Chris TorekApr 10, 2024
  20. Thalia ArchibaldApr 10, 2024
  21. Chris TorekApr 10, 2024
  22. Thalia ArchibaldApr 10, 2024
  23. Patrick SteinhardtApr 10, 2024
  24. Thalia ArchibaldApr 10, 2024
  25. Thalia ArchibaldApr 10, 2024
  26. 2/8 fast-import: directly use strbufs for pathsThalia Archibald, Apr 1, 2024
  27. Patrick SteinhardtApr 10, 2024
  28. Thalia ArchibaldApr 10, 2024
  29. Patrick SteinhardtApr 10, 2024
  30. 3/8 fast-import: allow unquoted empty path for rootThalia Archibald, Apr 1, 2024
  31. Patrick SteinhardtApr 10, 2024
  32. 4/8 fast-import: remove dead strbufThalia Archibald, Apr 1, 2024
  33. 5/8 fast-import: improve documentation for unquoted pathsThalia Archibald, Apr 1, 2024
  34. 6/8 fast-import: document C-style escapes for pathsThalia Archibald, Apr 1, 2024
  35. 7/8 fast-import: forbid escaped NUL in pathsThalia Archibald, Apr 1, 2024
  36. 8/8 fast-import: make comments more preciseThalia Archibald, Apr 1, 2024
  37. Thalia ArchibaldApr 7, 2024
  38. Eric SunshineApr 7, 2024
  39. Patrick SteinhardtApr 8, 2024
  40. Thalia ArchibaldApr 8, 2024
  41. Patrick SteinhardtApr 8, 2024
  42. Junio C HamanoApr 8, 2024
  43. 0/8 fast-import: tighten parsing of pathsThalia Archibald, Apr 10, 2024
  44. 1/8 fast-import: tighten path unquotingThalia Archibald, Apr 10, 2024
  45. 2/8 fast-import: directly use strbufs for pathsThalia Archibald, Apr 10, 2024
  46. 3/8 fast-import: allow unquoted empty path for rootThalia Archibald, Apr 10, 2024
  47. Junio C HamanoApr 11, 2024
  48. 5/8 fast-import: improve documentation for unquoted pathsThalia Archibald, Apr 10, 2024
  49. Junio C HamanoApr 11, 2024
  50. 6/8 fast-import: document C-style escapes for pathsThalia Archibald, Apr 10, 2024
  51. Junio C HamanoApr 10, 2024
  52. Thalia ArchibaldApr 10, 2024
  53. Junio C HamanoApr 11, 2024
  54. Patrick SteinhardtApr 11, 2024
  55. 7/8 fast-import: forbid escaped NUL in pathsThalia Archibald, Apr 10, 2024
  56. Junio C HamanoApr 10, 2024
  57. 8/8 fast-import: make comments more preciseThalia Archibald, Apr 10, 2024
  58. Junio C HamanoApr 10, 2024
  59. 4/8 fast-import: remove dead strbufThalia Archibald, Apr 10, 2024
  60. Junio C HamanoApr 11, 2024
  61. 0/8 fast-import: tighten parsing of pathsThalia Archibald, Apr 12, 2024
  62. 2/8 fast-import: directly use strbufs for pathsThalia Archibald, Apr 12, 2024
  63. 3/8 fast-import: allow unquoted empty path for rootThalia Archibald, Apr 12, 2024
  64. 4/8 fast-import: remove dead strbufThalia Archibald, Apr 12, 2024
  65. 5/8 fast-import: improve documentation for path quotingThalia Archibald, Apr 12, 2024
  66. 6/8 fast-import: document C-style escapes for pathsThalia Archibald, Apr 12, 2024
  67. 7/8 fast-import: forbid escaped NUL in pathsThalia Archibald, Apr 12, 2024
  68. 8/8 fast-import: make comments more preciseThalia Archibald, Apr 12, 2024
  69. 1/8 fast-import: tighten path unquotingThalia Archibald, Apr 12, 2024
  70. Junio C HamanoApr 12, 2024
  71. Thalia ArchibaldApr 13, 2024
  72. Junio C HamanoApr 13, 2024
  73. 0/8 fast-import: tighten parsing of pathsThalia Archibald, Apr 14, 2024
  74. 2/8 fast-import: directly use strbufs for pathsThalia Archibald, Apr 14, 2024
  75. 1/8 fast-import: tighten path unquotingThalia Archibald, Apr 14, 2024
  76. 3/8 fast-import: allow unquoted empty path for rootThalia Archibald, Apr 14, 2024
  77. 4/8 fast-import: remove dead strbufThalia Archibald, Apr 14, 2024
  78. 5/8 fast-import: improve documentation for path quotingThalia Archibald, Apr 14, 2024
  79. 6/8 fast-import: document C-style escapes for pathsThalia Archibald, Apr 14, 2024
  80. 7/8 fast-import: forbid escaped NUL in pathsThalia Archibald, Apr 14, 2024
  81. 8/8 fast-import: make comments more preciseThalia Archibald, Apr 14, 2024
  82. Patrick SteinhardtApr 15, 2024
  83. Junio C HamanoApr 15, 2024
  84. Thalia ArchibaldApr 1, 2024

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.