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

Re: [PATCH v3 2/2] alloc.c: remove the redundant commit_count variable

From
Jeff King <peff@peff.net>
Date
Jul 11, 2014, 00:30 UTC
Message-ID
<20140711003053.GB11360@sigill.intra.peff.net>
In-Reply-To
<53BF28F4.8050704@ramsay1.demon.co.uk>
On Fri, Jul 11, 2014 at 12:59:48AM +0100, Ramsay Jones wrote:
Show 5 quoted lines
> The 'commit_count' static variable is used in alloc_commit_node()
> to set the 'index' field of the commit structure to a unique value.
> This variable assumes the same value as the 'count' field of the
> 'commit_state' allocator state structure, which may be used in its
> place.

I don't think we want to do this, because there is a bug in the current code that I have not reported yet. :)

The code you're touching here was trying to make sure that each commit gets a unique index, under the assumption that commits only get allocated via alloc_commit_node. But I think that assumption is wrong. We can also get commit objects by allocating an OBJ_NONE (e.g., via lookup_unknown_object) and then converting it into an OBJ_COMMIT when we find out what it is.

We should be allocating a commit index to it at that point (but we don't currently), at which point the commit_count and commit_state.count indices will no longer be in sync.

This only happens in a few places. Most calls will probably be via lookup_commit (which gets called when we parse_object such an unknown object), but there are others (e.g., peel_object may fill in the type). So we could fix it with something like:

diff --git a/commit.c b/commit.c
index fb7897c..f4f4278 100644
--- a/commit.c
+++ b/commit.c
@@ -65,8 +65,11 @@ struct commit *lookup_commit(const unsigned char *sha1)
 		struct commit *c = alloc_commit_node();
 		return create_object(sha1, OBJ_COMMIT, c);
 	}
-	if (!obj->type)
+	if (!obj->type) {
+		struct commit *c = (struct commit *)obj;
 		obj->type = OBJ_COMMIT;
+		c->index = alloc_commit_index();
+	}
 	return check_commit(obj, sha1, 0);
 }
 

where alloc_commit_index() would be a thin wrapper around "return
commit_count++". And then find and update any other callsites that need
it.

The downside is that it's hard to find those callsites. A safer
alternative would be to speculatively allocate a commit index for all
unknown objects. Then if any other code switches the type to commit,
they already have an index. But it also means we'll have holes in our
commit index space, which makes commit-slabs less efficient.

We do not call lookup_unknown_object all that often, though, so it might
be an OK tradeoff (and in at least one case, in diff_tree_stdin, I think
the code can be converted not to use lookup_unknown_object in the first
place). So worrying about the performance may not be worth it.

-Peff
Previous: Ramsay JonesNext: Ramsay Jones
Message 2 of 35 in “alloc.c: remove the redundant commit_count variable”
  1. 2/2 alloc.c: remove the redundant commit_count variableRamsay Jones, Jul 10, 2014
  2. Jeff KingJul 11, 2014
  3. Ramsay JonesJul 11, 2014
  4. Jeff KingJul 11, 2014
  5. Ramsay JonesJul 11, 2014
  6. 0/7 ensure index is set for all OBJ_COMMIT objects variableJeff King, Jul 11, 2014
  7. 1/7 alloc.c: remove the alloc_raw_commit_node() functionJeff King, Jul 11, 2014
  8. 2/7 move setting of object->type to alloc_* functionsJeff King, Jul 11, 2014
  9. Ramsay JonesJul 12, 2014
  10. Jeff KingJul 12, 2014
  11. Jeff KingJul 13, 2014
  12. 1/8 alloc.c: remove the alloc_raw_commit_node() functionJeff King, Jul 13, 2014
  13. Junio C HamanoJul 15, 2014
  14. 2/8 alloc: write out allocator definitionsJeff King, Jul 13, 2014
  15. Junio C HamanoJul 15, 2014
  16. 3/8 move setting of object->type to alloc_* functionsJeff King, Jul 13, 2014
  17. Junio C HamanoJul 15, 2014
  18. 4/8 parse_object_buffer: do not set object typeJeff King, Jul 13, 2014
  19. 5/8 add object_as_type helper for casting objectsJeff King, Jul 13, 2014
  20. 6/8 alloc: factor out commit indexJeff King, Jul 13, 2014
  21. 7/8 object_as_type: set commit indexJeff King, Jul 13, 2014
  22. 8/8 diff-tree: avoid lookup_unknown_objectJeff King, Jul 13, 2014
  23. Ramsay JonesJul 13, 2014
  24. Jeff KingJul 14, 2014
  25. Ramsay JonesJul 14, 2014
  26. Ramsay JonesJul 12, 2014
  27. Jeff KingJul 12, 2014
  28. 3/7 parse_object_buffer: do not set object typeJeff King, Jul 11, 2014
  29. 4/7 add object_as_type helper for casting objectsJeff King, Jul 11, 2014
  30. Ramsay JonesJul 11, 2014
  31. Jeff KingJul 11, 2014
  32. 5/7 alloc: factor out commit indexJeff King, Jul 11, 2014
  33. 6/7 object_as_type: set commit indexJeff King, Jul 11, 2014
  34. 7/7 diff-tree: avoid lookup_unknown_objectJeff King, Jul 11, 2014
  35. Ramsay JonesJul 11, 2014

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.