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

Re: [PATCH 03/12] shallow.c: use commit-slab for commit depth instead of commit->util

From
Jeff King <peff@peff.net>
Date
May 12, 2018, 09:07 UTC
Message-ID
<20180512090748.GB28279@sigill.intra.peff.net>
In-Reply-To
<20180512080028.29611-4-pclouds@gmail.com>
On Sat, May 12, 2018 at 10:00:19AM +0200, Nguyễn Thái Ngọc Duy wrote:
Show 25 quoted lines
> @@ -82,25 +84,29 @@ struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
>  	struct object_array stack = OBJECT_ARRAY_INIT;
>  	struct commit *commit = NULL;
>  	struct commit_graft *graft;
> +	struct commit_depth depths;
>  
> +	init_commit_depth(&depths);
>  	while (commit || i < heads->nr || stack.nr) {
>  		struct commit_list *p;
>  		if (!commit) {
>  			if (i < heads->nr) {
> +				int **depth_slot;
>  				commit = (struct commit *)
>  					deref_tag(heads->objects[i++].item, NULL, 0);
>  				if (!commit || commit->object.type != OBJ_COMMIT) {
>  					commit = NULL;
>  					continue;
>  				}
> -				if (!commit->util)
> -					commit->util = xmalloc(sizeof(int));
> -				*(int *)commit->util = 0;
> +				depth_slot = commit_depth_at(&depths, commit);
> +				if (!*depth_slot)
> +					*depth_slot = xmalloc(sizeof(int));
> +				**depth_slot = 0;

It looks like we could save ourselves an extra layer of indirection (and tiny heap allocations) by just storing an "int" directly in the slab. Do we ever use the NULL as a sentinel value?

Here we just allocate it if not set. Let's see if we can find some others...

Show 20 quoted lines
> @@ -116,25 +122,32 @@ struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
>  		}
>  		commit->object.flags |= not_shallow_flag;
>  		for (p = commit->parents, commit = NULL; p; p = p->next) {
> -			if (!p->item->util) {
> -				int *pointer = xmalloc(sizeof(int));
> -				p->item->util = pointer;
> -				*pointer =  cur_depth;
> +			int **depth_slot = commit_depth_at(&depths, p->item);
> +			if (!*depth_slot) {
> +				*depth_slot = xmalloc(sizeof(int));
> +				**depth_slot = cur_depth;
>  			} else {
> -				int *pointer = p->item->util;
> -				if (cur_depth >= *pointer)
> +				if (cur_depth >= **depth_slot)
>  					continue;
> -				*pointer = cur_depth;
> +				**depth_slot = cur_depth;
>  			}

Here we malloc again if it's not set. But we do behave slightly differently when we see NULL, in that we do not bother to even compare against cur_depth. So if we were to directly store ints, we'd see "0" as the sentinel depth here, which would not match our "cur_depth >= depth_slot" check.

So no, it wouldn't work to directly store depths with the code as written. I'm not sure if the depth can ever be 0. If not, then it would be a suitable sentinel as:

  int *slot = commit_depth_at(&depths, p->item);
  if (!*slot || cur_depth < *slot)
	*slot = cur_depth;

But somebody would have to dig into the possible values of cur_depth there (which would make sense to do as a separate patch anyway, since the point of this is to be a direct conversion).

-Peff
Previous: Nguyễn Thái Ngọc DuyNext: Jeff King
Message 25 of 82 in “Die commit->util, die!”
  1. 00/12 Die commit->util, die!Nguyễn Thái Ngọc Duy, May 12, 2018
  2. 02/12 describe: use commit-slab for commit names instead of commit->utilNguyễn Thái Ngọc Duy, May 12, 2018
  3. 05/12 sequencer.c: use commit-slab to associate todo items to commitsNguyễn Thái Ngọc Duy, May 12, 2018
  4. Jeff KingMay 12, 2018
  5. 04/12 sequencer.c: use commit-slab to mark seen commitsNguyễn Thái Ngọc Duy, May 12, 2018
  6. Jeff KingMay 12, 2018
  7. Junio C HamanoMay 12, 2018
  8. Duy NguyenMay 12, 2018
  9. 06/12 revision.c: use commit-slab for show_sourceNguyễn Thái Ngọc Duy, May 12, 2018
  10. Jeff KingMay 12, 2018
  11. Junio C HamanoMay 12, 2018
  12. Duy NguyenMay 12, 2018
  13. Jeff KingMay 12, 2018
  14. 10/12 log: use commit-slab in prepare_bases() instead of commit->utilNguyễn Thái Ngọc Duy, May 12, 2018
  15. 12/12 commit.h: delete 'util' field in struct commitNguyễn Thái Ngọc Duy, May 12, 2018
  16. 11/12 merge: use commit-slab in merge remote desc instead of commit->utilNguyễn Thái Ngọc Duy, May 12, 2018
  17. 09/12 show-branch: use commit-slab for commit-name instead of commit->utilNguyễn Thái Ngọc Duy, May 12, 2018
  18. 01/12 blame: use commit-slab for blame suspects instead of commit->utilNguyễn Thái Ngọc Duy, May 12, 2018
  19. Jeff KingMay 12, 2018
  20. Duy NguyenMay 12, 2018
  21. 07/12 bisect.c: use commit-slab for commit weight instead of commit->utilNguyễn Thái Ngọc Duy, May 12, 2018
  22. Junio C HamanoMay 12, 2018
  23. 08/12 name-rev: use commit-slab for rev-name instead of commit->utilNguyễn Thái Ngọc Duy, May 12, 2018
  24. 03/12 shallow.c: use commit-slab for commit depth instead of commit->utilNguyễn Thái Ngọc Duy, May 12, 2018
  25. Jeff KingMay 12, 2018
  26. Jeff KingMay 12, 2018
  27. Duy NguyenMay 12, 2018
  28. Jeff KingMay 12, 2018
  29. Duy NguyenMay 12, 2018
  30. Jeff KingMay 12, 2018
  31. Jakub NarebskiMay 12, 2018
  32. Duy NguyenMay 13, 2018
  33. 00/14 Die commit->util, die!Nguyễn Thái Ngọc Duy, May 13, 2018
  34. 01/14 commit-slab.h: code splitNguyễn Thái Ngọc Duy, May 13, 2018
  35. Junio C HamanoMay 13, 2018
  36. 02/14 commit-slab: support shared commit-slabNguyễn Thái Ngọc Duy, May 13, 2018
  37. Junio C HamanoMay 13, 2018
  38. 03/14 blame: use commit-slab for blame suspects instead of commit->utilNguyễn Thái Ngọc Duy, May 13, 2018
  39. Junio C HamanoMay 13, 2018
  40. 06/14 sequencer.c: use commit-slab to mark seen commitsNguyễn Thái Ngọc Duy, May 13, 2018
  41. Junio C HamanoMay 14, 2018
  42. 12/14 log: use commit-slab in prepare_bases() instead of commit->utilNguyễn Thái Ngọc Duy, May 13, 2018
  43. 07/14 sequencer.c: use commit-slab to associate todo items to commitsNguyễn Thái Ngọc Duy, May 13, 2018
  44. 14/14 commit.h: delete 'util' field in struct commitNguyễn Thái Ngọc Duy, May 13, 2018
  45. Junio C HamanoMay 14, 2018
  46. Duy NguyenMay 14, 2018
  47. Duy NguyenMay 14, 2018
  48. Derrick StoleeMay 14, 2018
  49. Duy NguyenMay 19, 2018
  50. 08/14 revision.c: use commit-slab for show_sourceNguyễn Thái Ngọc Duy, May 13, 2018
  51. Junio C HamanoMay 14, 2018
  52. Junio C HamanoMay 14, 2018
  53. 13/14 merge: use commit-slab in merge remote desc instead of commit->utilNguyễn Thái Ngọc Duy, May 13, 2018
  54. Junio C HamanoMay 18, 2018
  55. Ramsay JonesMay 18, 2018
  56. 11/14 show-branch: use commit-slab for commit-name instead of commit->utilNguyễn Thái Ngọc Duy, May 13, 2018
  57. Junio C HamanoMay 14, 2018
  58. Duy NguyenMay 19, 2018
  59. 09/14 bisect.c: use commit-slab for commit weight instead of commit->utilNguyễn Thái Ngọc Duy, May 13, 2018
  60. Junio C HamanoMay 14, 2018
  61. 05/14 shallow.c: use commit-slab for commit depth instead of commit->utilNguyễn Thái Ngọc Duy, May 13, 2018
  62. 10/14 name-rev: use commit-slab for rev-name instead of commit->utilNguyễn Thái Ngọc Duy, May 13, 2018
  63. 04/14 describe: use commit-slab for commit names instead of commit->utilNguyễn Thái Ngọc Duy, May 13, 2018
  64. Junio C HamanoMay 13, 2018
  65. 00/15 Die commit->util, die!Nguyễn Thái Ngọc Duy, May 19, 2018
  66. 01/15 commit-slab.h: code splitNguyễn Thái Ngọc Duy, May 19, 2018
  67. Junio C HamanoMay 21, 2018
  68. 02/15 commit-slab: support shared commit-slabNguyễn Thái Ngọc Duy, May 19, 2018
  69. 04/15 describe: use commit-slab for commit names instead of commit->utilNguyễn Thái Ngọc Duy, May 19, 2018
  70. 08/15 revision.c: use commit-slab for show_sourceNguyễn Thái Ngọc Duy, May 19, 2018
  71. 09/15 bisect.c: use commit-slab for commit weight instead of commit->utilNguyễn Thái Ngọc Duy, May 19, 2018
  72. 07/15 sequencer.c: use commit-slab to associate todo items to commitsNguyễn Thái Ngọc Duy, May 19, 2018
  73. 13/15 log: use commit-slab in prepare_bases() instead of commit->utilNguyễn Thái Ngọc Duy, May 19, 2018
  74. 15/15 commit.h: delete 'util' field in struct commitNguyễn Thái Ngọc Duy, May 19, 2018
  75. 14/15 merge: use commit-slab in merge remote desc instead of commit->utilNguyễn Thái Ngọc Duy, May 19, 2018
  76. 06/15 sequencer.c: use commit-slab to mark seen commitsNguyễn Thái Ngọc Duy, May 19, 2018
  77. 12/15 show-branch: note about its object flags usageNguyễn Thái Ngọc Duy, May 19, 2018
  78. 11/15 show-branch: use commit-slab for commit-name instead of commit->utilNguyễn Thái Ngọc Duy, May 19, 2018
  79. 10/15 name-rev: use commit-slab for rev-name instead of commit->utilNguyễn Thái Ngọc Duy, May 19, 2018
  80. 03/15 blame: use commit-slab for blame suspects instead of commit->utilNguyễn Thái Ngọc Duy, May 19, 2018
  81. 05/15 shallow.c: use commit-slab for commit depth instead of commit->utilNguyễn Thái Ngọc Duy, May 19, 2018
  82. Stefan BellerMay 22, 2018

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.