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

[PATCH v3 04/13] attr: don't recompute default attribute source

From
Patrick Steinhardt <ps@pks.im>
Date
Apr 29, 2024, 06:34 UTC
Message-ID
<929bacbfceeb1627b257f68d9f66155878a11e18.1714371422.git.ps@pks.im>
In-Reply-To
<cover.1714371422.git.ps@pks.im>

The `default_attr_source()` function lazily computes the attr source supposedly once, only. This is done via a static variable `attr_source` that contains the resolved object ID of the attr source's tree. If the variable is the null object ID then we try to look up the attr source, otherwise we skip over it.

This approach is flawed though: the variable will never be set to anything else but the null object ID in case there is no attr source. Consequently, we re-compute the information on every call. And in the worst case, when we silently ignore bad trees, this will cause us to try and look up the treeish every single time.

Improve this by introducing a separate variable `has_attr_source` to track whether we already computed the attr source and, if so, whether we have an attr source or not.

This also allows us to convert the `ignore_bad_attr_tree` to not be static anymore as the code will only be executed once anyway.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 attr.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
diff --git a/attr.c b/attr.c
index 679e42258c..9d911aeb31 100644
--- a/attr.c
+++ b/attr.c
@@ -1206,15 +1206,16 @@ static void collect_some_attrs(struct index_state *istate,
 }
 
 static const char *default_attr_source_tree_object_name;
-static int ignore_bad_attr_tree;
 
 void set_git_attr_source(const char *tree_object_name)
 {
 	default_attr_source_tree_object_name = xstrdup(tree_object_name);
 }
 
-static void compute_default_attr_source(struct object_id *attr_source)
+static int compute_default_attr_source(struct object_id *attr_source)
 {
+	int ignore_bad_attr_tree = 0;
+
 	if (!default_attr_source_tree_object_name)
 		default_attr_source_tree_object_name = getenv(GIT_ATTR_SOURCE_ENVIRONMENT);
 
@@ -1230,22 +1231,28 @@ static void compute_default_attr_source(struct object_id *attr_source)
 		ignore_bad_attr_tree = 1;
 	}
 
-	if (!default_attr_source_tree_object_name || !is_null_oid(attr_source))
-		return;
+	if (!default_attr_source_tree_object_name)
+		return 0;
 
 	if (repo_get_oid_treeish(the_repository,
 				 default_attr_source_tree_object_name,
-				 attr_source) && !ignore_bad_attr_tree)
-		die(_("bad --attr-source or GIT_ATTR_SOURCE"));
+				 attr_source)) {
+		if (!ignore_bad_attr_tree)
+			die(_("bad --attr-source or GIT_ATTR_SOURCE"));
+		return 0;
+	}
+
+	return 1;
 }
 
 static struct object_id *default_attr_source(void)
 {
 	static struct object_id attr_source;
+	static int has_attr_source = -1;
 
-	if (is_null_oid(&attr_source))
-		compute_default_attr_source(&attr_source);
-	if (is_null_oid(&attr_source))
+	if (has_attr_source < 0)
+		has_attr_source = compute_default_attr_source(&attr_source);
+	if (!has_attr_source)
 		return NULL;
 	return &attr_source;
 }
-- 
2.45.0-rc1
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 44 of 67 in “Stop relying on SHA1 fallback for `the_hash_algo`”
  1. 00/11 Stop relying on SHA1 fallback for `the_hash_algo`Patrick Steinhardt, Apr 19, 2024
  2. 01/11 path: harden validation of HEAD with non-standard hashesPatrick Steinhardt, Apr 19, 2024
  3. brian m. carlsonApr 19, 2024
  4. Patrick SteinhardtApr 22, 2024
  5. Junio C HamanoApr 22, 2024
  6. Patrick SteinhardtApr 23, 2024
  7. Junio C HamanoApr 23, 2024
  8. 02/11 parse-options-cb: only abbreviate hashes when hash algo is knownPatrick Steinhardt, Apr 19, 2024
  9. Justin ToblerApr 23, 2024
  10. 03/11 attr: don't recompute default attribute sourcePatrick Steinhardt, Apr 19, 2024
  11. Justin ToblerApr 23, 2024
  12. 04/11 attr: fix BUG() when parsing attrs outside of repoPatrick Steinhardt, Apr 19, 2024
  13. 05/11 remote-curl: fix parsing of detached SHA256 headsPatrick Steinhardt, Apr 19, 2024
  14. 06/11 builtin/rev-parse: allow shortening to more than 40 hex charactersPatrick Steinhardt, Apr 19, 2024
  15. 07/11 builtin/blame: don't access potentially unitialized `the_hash_algo`Patrick Steinhardt, Apr 19, 2024
  16. 08/11 builtin/bundle: abort "verify" early when there is no repositoryPatrick Steinhardt, Apr 19, 2024
  17. 09/11 builtin/diff: explicitly set hash algo when there is no repoPatrick Steinhardt, Apr 19, 2024
  18. Junio C HamanoApr 22, 2024
  19. 10/11 builtin/shortlog: don't set up revisions without repoPatrick Steinhardt, Apr 19, 2024
  20. Justin ToblerApr 23, 2024
  21. 11/11 repository: stop setting SHA1 as the default object hashPatrick Steinhardt, Apr 19, 2024
  22. brian m. carlsonApr 19, 2024
  23. Junio C HamanoApr 19, 2024
  24. Patrick SteinhardtApr 22, 2024
  25. 00/12 Stop relying on SHA1 fallback for `the_hash_algo`Patrick Steinhardt, Apr 23, 2024
  26. 01/12 path: harden validation of HEAD with non-standard hashesPatrick Steinhardt, Apr 23, 2024
  27. 02/12 path: move `validate_headref()` to its only userPatrick Steinhardt, Apr 23, 2024
  28. 03/12 parse-options-cb: only abbreviate hashes when hash algo is knownPatrick Steinhardt, Apr 23, 2024
  29. 04/12 attr: don't recompute default attribute sourcePatrick Steinhardt, Apr 23, 2024
  30. 05/12 attr: fix BUG() when parsing attrs outside of repoPatrick Steinhardt, Apr 23, 2024
  31. 06/12 remote-curl: fix parsing of detached SHA256 headsPatrick Steinhardt, Apr 23, 2024
  32. 07/12 builtin/rev-parse: allow shortening to more than 40 hex charactersPatrick Steinhardt, Apr 23, 2024
  33. 08/12 builtin/blame: don't access potentially unitialized `the_hash_algo`Patrick Steinhardt, Apr 23, 2024
  34. 09/12 builtin/bundle: abort "verify" early when there is no repositoryPatrick Steinhardt, Apr 23, 2024
  35. 10/12 builtin/diff: explicitly set hash algo when there is no repoPatrick Steinhardt, Apr 23, 2024
  36. 11/12 builtin/shortlog: don't set up revisions without repoPatrick Steinhardt, Apr 23, 2024
  37. 12/12 repository: stop setting SHA1 as the default object hashPatrick Steinhardt, Apr 23, 2024
  38. Junio C HamanoApr 27, 2024
  39. Patrick SteinhardtApr 29, 2024
  40. 00/13 Stop relying on SHA1 fallback for `the_hash_algo`Patrick Steinhardt, Apr 29, 2024
  41. 01/13 path: harden validation of HEAD with non-standard hashesPatrick Steinhardt, Apr 29, 2024
  42. 02/13 path: move `validate_headref()` to its only userPatrick Steinhardt, Apr 29, 2024
  43. 03/13 parse-options-cb: only abbreviate hashes when hash algo is knownPatrick Steinhardt, Apr 29, 2024
  44. 04/13 attr: don't recompute default attribute sourcePatrick Steinhardt, Apr 29, 2024
  45. 05/13 attr: fix BUG() when parsing attrs outside of repoPatrick Steinhardt, Apr 29, 2024
  46. 06/13 remote-curl: fix parsing of detached SHA256 headsPatrick Steinhardt, Apr 29, 2024
  47. 07/13 builtin/rev-parse: allow shortening to more than 40 hex charactersPatrick Steinhardt, Apr 29, 2024
  48. 08/13 builtin/blame: don't access potentially unitialized `the_hash_algo`Patrick Steinhardt, Apr 29, 2024
  49. 09/13 builtin/bundle: abort "verify" early when there is no repositoryPatrick Steinhardt, Apr 29, 2024
  50. 10/13 builtin/diff: explicitly set hash algo when there is no repoPatrick Steinhardt, Apr 29, 2024
  51. 11/13 builtin/shortlog: don't set up revisions without repoPatrick Steinhardt, Apr 29, 2024
  52. 12/13 oss-fuzz/commit-graph: set up hash algorithmPatrick Steinhardt, Apr 29, 2024
  53. 13/13 repository: stop setting SHA1 as the default object hashPatrick Steinhardt, Apr 29, 2024
  54. 00/13 Stop relying on SHA1 fallback for `the_hash_algo`Patrick Steinhardt, May 7, 2024
  55. 01/13 path: harden validation of HEAD with non-standard hashesPatrick Steinhardt, May 7, 2024
  56. 02/13 path: move `validate_headref()` to its only userPatrick Steinhardt, May 7, 2024
  57. 03/13 parse-options-cb: only abbreviate hashes when hash algo is knownPatrick Steinhardt, May 7, 2024
  58. 04/13 attr: don't recompute default attribute sourcePatrick Steinhardt, May 7, 2024
  59. 05/13 attr: fix BUG() when parsing attrs outside of repoPatrick Steinhardt, May 7, 2024
  60. 06/13 remote-curl: fix parsing of detached SHA256 headsPatrick Steinhardt, May 7, 2024
  61. 07/13 builtin/rev-parse: allow shortening to more than 40 hex charactersPatrick Steinhardt, May 7, 2024
  62. 08/13 builtin/blame: don't access potentially unitialized `the_hash_algo`Patrick Steinhardt, May 7, 2024
  63. 09/13 builtin/bundle: abort "verify" early when there is no repositoryPatrick Steinhardt, May 7, 2024
  64. 10/13 builtin/diff: explicitly set hash algo when there is no repoPatrick Steinhardt, May 7, 2024
  65. 11/13 builtin/shortlog: don't set up revisions without repoPatrick Steinhardt, May 7, 2024
  66. 12/13 oss-fuzz/commit-graph: set up hash algorithmPatrick Steinhardt, May 7, 2024
  67. 13/13 repository: stop setting SHA1 as the default object hashPatrick Steinhardt, May 7, 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.