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

Re: [PATCH 07/11] Add a function to determine unique prefixes for a list of strings

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
May 13, 2019, 12:48 UTC
Message-ID
<nycvar.QRO.7.76.6.1905131405210.44@tvgsbejvaqbjf.bet>
In-Reply-To
<1be9b470-6daa-a50e-ba3a-432520721b0f@jeffhostetler.com>
Hi Jeff,
On Thu, 18 Apr 2019, Jeff Hostetler wrote:
Show 86 quoted lines
> On 4/10/2019 1:37 PM, Slavica Djukic via GitGitGadget wrote:
> > From: Slavica Djukic <slawica92@hotmail.com>
> >
> > In the `git add -i` command, we show unique prefixes of the commands and
> > files, to give an indication what prefix would select them.
> >
> > Naturally, the C implementation looks a lot different than the Perl
> > implementation: in Perl, a trie is much easier implemented, while we
> > already have a pretty neat hashmap implementation in C that we use for
> > the purpose of storing (not necessarily unique) prefixes.
> >
> > The idea: for each item that we add, we generate prefixes starting with
> > the first letter, then the first two letters, then three, etc, until we
> > find a prefix that is unique (or until the prefix length would be
> > longer than we want). If we encounter a previously-unique prefix on the
> > way, we adjust that item's prefix to make it unique again (or we mark it
> > as having no unique prefix if we failed to find one). These partial
> > prefixes are stored in a hash map (for quick lookup times).
> >
> > To make sure that this function works as expected, we add a test using a
> > special-purpose test helper that was added for that purpose.
> >
> > Note: We expect the list of prefix items to be passed in as a list of
> > pointers rather than as regular list to avoid having to copy information
> > (the actual items will most likely contain more information than just
> > the name and the length of the unique prefix, but passing in `struct
> > prefix_item *` would not allow for that).
> >
> > Signed-off-by: Slavica Djukic <slawica92@hotmail.com>
> > Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
> > ---
> > diff --git a/prefix-map.c b/prefix-map.c
> > new file mode 100644
> > index 0000000000..3c5ae4ae0a
> > --- /dev/null
> > +++ b/prefix-map.c
> > @@ -0,0 +1,111 @@
> > +#include "cache.h"
> > +#include "prefix-map.h"
> > +
> > +static int map_cmp(const void *unused_cmp_data,
> > +		   const void *entry,
> > +		   const void *entry_or_key,
> > +		   const void *unused_keydata)
> > +{
> > +	const struct prefix_map_entry *a = entry;
> > +	const struct prefix_map_entry *b = entry_or_key;
> > +
> > +	return a->prefix_length != b->prefix_length ||
> > +		strncmp(a->name, b->name, a->prefix_length);
> > +}
> > +
> > +static void add_prefix_entry(struct hashmap *map, const char *name,
> > +			     size_t prefix_length, struct prefix_item *item)
> > +{
> > +	struct prefix_map_entry *result = xmalloc(sizeof(*result));
> > +	result->name = name;
> > +	result->prefix_length = prefix_length;
> > +	result->item = item;
> > +	hashmap_entry_init(result, memhash(name, prefix_length));
> > +	hashmap_add(map, result);
> > +}
> > +
> > +static void init_prefix_map(struct prefix_map *prefix_map,
> > +			    int min_prefix_length, int max_prefix_length)
> > +{
> > +	hashmap_init(&prefix_map->map, map_cmp, NULL, 0);
> > +	prefix_map->min_length = min_prefix_length;
> > +	prefix_map->max_length = max_prefix_length;
> > +}
> > +
> > +static void add_prefix_item(struct prefix_map *prefix_map,
> > +			    struct prefix_item *item)
> > +{
> > +	struct prefix_map_entry *e = xmalloc(sizeof(*e)), *e2;
> > +	int j;
> > +
> > +	e->item = item;
> > +	e->name = e->item->name;
> > +
> > +	for (j = prefix_map->min_length; j <= prefix_map->max_length; j++) {
> > +		if (!isascii(e->name[j])) {
>
> This feels odd, if I understand the intent.
>
> First, why "isascii()" rather than just non-zero?
That's to imitate `git-add--interactive.perl`'s
	if (ord($letters[0]) > 127 ||
	    ($soft_limit && $j + 1 > $soft_limit))

See https://github.com/git/git/blob/v2.21.0/git-add--interactive.perl#L410 for more complete context.

I think the main benefit here is that we avoid running into the trap of using incomplete UTF-8 multi-byte sequences in prefixes.

I guess we could throw in an extra safety on the C side by excluding control characters, too. But that would be a deviation from Perl, and I actually do not even feel strongly about excluding, say, a HT (horizontal tab) from the prefixes.

Show 7 quoted lines
> But mainly, can we walk off the end of the array and read
> potentially uninitialized memory?  Shouldn't we have something
> at the top of the function like:
>
>     len = strlen(item->name);
>     if (len < prefix_map->min_length)
>         return;

Ooops, you're right. But I would not use `strlen() here, we can easily just add `&& e->name[j]` to the loop condition.

> (And maybe avoid the xmalloc() too?)

Hmm. At first, I thought: no, we use `*e` *both* for lookup and for adding a new item once we did not find any existing for the current prefix length.

But it does indeed become a lot clearer when I separate those. It's not even performance or memory critical a code path.

Show 29 quoted lines
> And maybe do " j <= min(len, max_length) " in the loop?
> But I see you're modifying "j" down in the body of the loop,
> so I'll wait on suggesting that.
>
> > +			free(e);
> > +			break;
> > +		}
> > +
> > +		e->prefix_length = j;
> > +		hashmap_entry_init(e, memhash(e->name, j));
> > +		e2 = hashmap_get(&prefix_map->map, e, NULL);
> > +		if (!e2) {
> > +			/* prefix is unique so far */
> > +			e->item->prefix_length = j;
> > +			hashmap_add(&prefix_map->map, e);
> > +			break;
> > +		}
> > +
> > +		if (!e2->item)
> > +			continue; /* non-unique prefix */
> > +
> > +		if (j != e2->item->prefix_length)
> > +			BUG("unexpected prefix length: %d != %d",
> > +			    (int)j, (int)e2->item->prefix_length);
>
> IIUC, this assurance comes directly from map_cmp(), right?
> We could strengthen this to
>      (j != e2->item->prefix_length || strncmp(...))
> if we wanted to, right?
Right, I'll actually go for `memcmp()` here, but the idea is the same.
Show 7 quoted lines
> > +
> > +		/* skip common prefix */
> > +		for (; j < prefix_map->max_length && e->name[j]; j++) {
> > +			if (e->item->name[j] != e2->item->name[j])
> > +				break;
>
> Same comment here about walking off of the defined end of both arrays.

Actually, no, not here, as I already test for `e->name[j]` in the loop condition. If we reach the end of `e2->item->name`, the inner condition will break out of the loop.

> I'm going to stop here.  I'm getting confused.
Oh no ;-)
Thank you for your helpful comments!

Ciao, Dscho

Previous: Jeff HostetlerNext: Slavica Djukic via GitGitGadget
Message 19 of 124 in “git add -i: add a rudimentary version in C (supporting only status and help so far)”
  1. 00/11 git add -i: add a rudimentary version in C (supporting only status and help so far)Johannes Schindelin via GitGitGadget, Apr 10, 2019
  2. 01/11 Start to implement a built-in version of `git add --interactive`Johannes Schindelin via GitGitGadget, Apr 10, 2019
  3. Jeff HostetlerApr 18, 2019
  4. Jeff KingApr 18, 2019
  5. Johannes SchindelinApr 30, 2019
  6. Jeff KingMay 1, 2019
  7. Johannes SchindelinMay 13, 2019
  8. 02/11 diff: export diffstat interfaceDaniel Ferreira via GitGitGadget, Apr 10, 2019
  9. 04/11 built-in add -i: refresh the index before running `status`Johannes Schindelin via GitGitGadget, Apr 10, 2019
  10. 03/11 built-in add -i: implement the `status` commandDaniel Ferreira via GitGitGadget, Apr 10, 2019
  11. 05/11 built-in add -i: color the header in the `status` commandJohannes Schindelin via GitGitGadget, Apr 10, 2019
  12. 06/11 built-in add -i: implement the main loopJohannes Schindelin via GitGitGadget, Apr 10, 2019
  13. Jeff HostetlerApr 18, 2019
  14. Johannes SchindelinMay 13, 2019
  15. 09/11 built-in add -i: support `?` (prompt help)Johannes Schindelin via GitGitGadget, Apr 10, 2019
  16. 08/11 built-in add -i: show unique prefixes of the commandsSlavica Djukic via GitGitGadget, Apr 10, 2019
  17. 07/11 Add a function to determine unique prefixes for a list of stringsSlavica Djukic via GitGitGadget, Apr 10, 2019
  18. Jeff HostetlerApr 18, 2019
  19. Johannes SchindelinMay 13, 2019
  20. 10/11 built-in add -i: use color in the main loopSlavica Djukic via GitGitGadget, Apr 10, 2019
  21. 11/11 built-in add -i: implement the `help` commandJohannes Schindelin via GitGitGadget, Apr 10, 2019
  22. 00/11 git add -i: add a rudimentary version in C (supporting only status and help so far)Johannes Schindelin via GitGitGadget, May 13, 2019
  23. 02/11 diff: export diffstat interfaceDaniel Ferreira via GitGitGadget, May 13, 2019
  24. 04/11 built-in add -i: refresh the index before running `status`Johannes Schindelin via GitGitGadget, May 13, 2019
  25. 06/11 built-in add -i: implement the main loopJohannes Schindelin via GitGitGadget, May 13, 2019
  26. 08/11 built-in add -i: show unique prefixes of the commandsSlavica Djukic via GitGitGadget, May 13, 2019
  27. 11/11 built-in add -i: implement the `help` commandJohannes Schindelin via GitGitGadget, May 13, 2019
  28. 10/11 built-in add -i: use color in the main loopSlavica Djukic via GitGitGadget, May 13, 2019
  29. 07/11 Add a function to determine unique prefixes for a list of stringsSlavica Djukic via GitGitGadget, May 13, 2019
  30. 05/11 built-in add -i: color the header in the `status` commandJohannes Schindelin via GitGitGadget, May 13, 2019
  31. 09/11 built-in add -i: support `?` (prompt help)Johannes Schindelin via GitGitGadget, May 13, 2019
  32. 01/11 Start to implement a built-in version of `git add --interactive`Johannes Schindelin via GitGitGadget, May 13, 2019
  33. 03/11 built-in add -i: implement the `status` commandDaniel Ferreira via GitGitGadget, May 13, 2019
  34. 00/11 git add -i: add a rudimentary version in C (supporting only status and help so far)Johannes Schindelin via GitGitGadget, Jul 16, 2019
  35. 01/11 Start to implement a built-in version of `git add --interactive`Johannes Schindelin via GitGitGadget, Jul 16, 2019
  36. Junio C HamanoJul 31, 2019
  37. Johannes SchindelinAug 26, 2019
  38. Junio C HamanoAug 27, 2019
  39. Johannes SchindelinAug 28, 2019
  40. Junio C HamanoAug 28, 2019
  41. 02/11 diff: export diffstat interfaceDaniel Ferreira via GitGitGadget, Jul 16, 2019
  42. Junio C HamanoJul 31, 2019
  43. Johannes SchindelinAug 27, 2019
  44. 05/11 built-in add -i: color the header in the `status` commandJohannes Schindelin via GitGitGadget, Jul 16, 2019
  45. 04/11 built-in add -i: refresh the index before running `status`Johannes Schindelin via GitGitGadget, Jul 16, 2019
  46. 06/11 built-in add -i: implement the main loopJohannes Schindelin via GitGitGadget, Jul 16, 2019
  47. Junio C HamanoJul 31, 2019
  48. 08/11 built-in add -i: show unique prefixes of the commandsSlavica Djukic via GitGitGadget, Jul 16, 2019
  49. 07/11 Add a function to determine unique prefixes for a list of stringsSlavica Djukic via GitGitGadget, Jul 16, 2019
  50. Junio C HamanoJul 31, 2019
  51. SZEDER GáborAug 24, 2019
  52. Johannes SchindelinAug 27, 2019
  53. SZEDER GáborAug 28, 2019
  54. [PoC] A simpler find_unique_prefixes() implementationSZEDER Gábor, Aug 28, 2019
  55. Johannes SchindelinAug 30, 2019
  56. 09/11 built-in add -i: support `?` (prompt help)Johannes Schindelin via GitGitGadget, Jul 16, 2019
  57. 03/11 built-in add -i: implement the `status` commandDaniel Ferreira via GitGitGadget, Jul 16, 2019
  58. Junio C HamanoJul 31, 2019
  59. Johannes SchindelinAug 27, 2019
  60. 10/11 built-in add -i: use color in the main loopSlavica Djukic via GitGitGadget, Jul 16, 2019
  61. 11/11 built-in add -i: implement the `help` commandJohannes Schindelin via GitGitGadget, Jul 16, 2019
  62. Junio C HamanoAug 2, 2019
  63. Jeff KingAug 2, 2019
  64. Johannes SchindelinJul 16, 2019
  65. Junio C HamanoAug 2, 2019
  66. 00/11 git add -i: add a rudimentary version in C (supporting only status and help so far)Johannes Schindelin via GitGitGadget, Aug 27, 2019
  67. 01/11 Start to implement a built-in version of `git add --interactive`Johannes Schindelin via GitGitGadget, Aug 27, 2019
  68. 08/11 built-in add -i: show unique prefixes of the commandsSlavica Djukic via GitGitGadget, Aug 27, 2019
  69. 03/11 built-in add -i: implement the `status` commandDaniel Ferreira via GitGitGadget, Aug 27, 2019
  70. 06/11 built-in add -i: implement the main loopJohannes Schindelin via GitGitGadget, Aug 27, 2019
  71. 04/11 built-in add -i: refresh the index before running `status`Johannes Schindelin via GitGitGadget, Aug 27, 2019
  72. 09/11 built-in add -i: support `?` (prompt help)Johannes Schindelin via GitGitGadget, Aug 27, 2019
  73. 11/11 built-in add -i: implement the `help` commandJohannes Schindelin via GitGitGadget, Aug 27, 2019
  74. 10/11 built-in add -i: use color in the main loopSlavica Djukic via GitGitGadget, Aug 27, 2019
  75. 02/11 diff: export diffstat interfaceDaniel Ferreira via GitGitGadget, Aug 27, 2019
  76. 07/11 Add a function to determine unique prefixes for a list of stringsSlavica Djukic via GitGitGadget, Aug 27, 2019
  77. 05/11 built-in add -i: color the header in the `status` commandJohannes Schindelin via GitGitGadget, Aug 27, 2019
  78. 0/9 git add -i: add a rudimentary version in C (supporting only status and help so far)Johannes Schindelin via GitGitGadget, Nov 4, 2019
  79. 1/9 Start to implement a built-in version of `git add --interactive`Johannes Schindelin via GitGitGadget, Nov 4, 2019
  80. Junio C HamanoNov 8, 2019
  81. Johannes SchindelinNov 9, 2019
  82. Junio C HamanoNov 10, 2019
  83. Johannes SchindelinNov 11, 2019
  84. Junio C HamanoNov 11, 2019
  85. Johannes SchindelinNov 12, 2019
  86. Junio C HamanoNov 13, 2019
  87. Johannes SchindelinNov 13, 2019
  88. Junio C HamanoNov 13, 2019
  89. 4/9 built-in add -i: color the header in the `status` commandSlavica Đukić via GitGitGadget, Nov 4, 2019
  90. 3/9 built-in add -i: implement the `status` commandDaniel Ferreira via GitGitGadget, Nov 4, 2019
  91. Junio C HamanoNov 8, 2019
  92. 2/9 diff: export diffstat interfaceDaniel Ferreira via GitGitGadget, Nov 4, 2019
  93. Junio C HamanoNov 8, 2019
  94. 7/9 built-in add -i: support `?` (prompt help)Johannes Schindelin via GitGitGadget, Nov 4, 2019
  95. 5/9 built-in add -i: implement the main loopJohannes Schindelin via GitGitGadget, Nov 4, 2019
  96. Junio C HamanoNov 8, 2019
  97. Johannes SchindelinNov 9, 2019
  98. 8/9 built-in add -i: use color in the main loopSlavica Đukić via GitGitGadget, Nov 4, 2019
  99. 9/9 built-in add -i: implement the `help` commandSlavica Đukić via GitGitGadget, Nov 4, 2019
  100. 6/9 built-in add -i: show unique prefixes of the commandsJohannes Schindelin via GitGitGadget, Nov 4, 2019
  101. 0/9 git add -i: add a rudimentary version in C (supporting only status and help so far)Johannes Schindelin via GitGitGadget, Nov 13, 2019
  102. 2/9 diff: export diffstat interfaceDaniel Ferreira via GitGitGadget, Nov 13, 2019
  103. 1/9 Start to implement a built-in version of `git add --interactive`Johannes Schindelin via GitGitGadget, Nov 13, 2019
  104. Junio C HamanoNov 14, 2019
  105. Johannes SchindelinNov 14, 2019
  106. Junio C HamanoNov 15, 2019
  107. 3/9 built-in add -i: implement the `status` commandDaniel Ferreira via GitGitGadget, Nov 13, 2019
  108. 6/9 built-in add -i: show unique prefixes of the commandsJohannes Schindelin via GitGitGadget, Nov 13, 2019
  109. 7/9 built-in add -i: support `?` (prompt help)Johannes Schindelin via GitGitGadget, Nov 13, 2019
  110. 9/9 built-in add -i: implement the `help` commandSlavica Đukić via GitGitGadget, Nov 13, 2019
  111. 5/9 built-in add -i: implement the main loopJohannes Schindelin via GitGitGadget, Nov 13, 2019
  112. 8/9 built-in add -i: use color in the main loopSlavica Đukić via GitGitGadget, Nov 13, 2019
  113. 4/9 built-in add -i: color the header in the `status` commandSlavica Đukić via GitGitGadget, Nov 13, 2019
  114. Johannes SchindelinNov 13, 2019
  115. 0/9 git add -i: add a rudimentary version in C (supporting only status and help so far)Johannes Schindelin via GitGitGadget, Nov 15, 2019
  116. 1/9 Start to implement a built-in version of `git add --interactive`Johannes Schindelin via GitGitGadget, Nov 15, 2019
  117. 4/9 built-in add -i: color the header in the `status` commandSlavica Đukić via GitGitGadget, Nov 15, 2019
  118. 7/9 built-in add -i: support `?` (prompt help)Johannes Schindelin via GitGitGadget, Nov 15, 2019
  119. 5/9 built-in add -i: implement the main loopJohannes Schindelin via GitGitGadget, Nov 15, 2019
  120. 2/9 diff: export diffstat interfaceDaniel Ferreira via GitGitGadget, Nov 15, 2019
  121. 6/9 built-in add -i: show unique prefixes of the commandsJohannes Schindelin via GitGitGadget, Nov 15, 2019
  122. 3/9 built-in add -i: implement the `status` commandDaniel Ferreira via GitGitGadget, Nov 15, 2019
  123. 8/9 built-in add -i: use color in the main loopSlavica Đukić via GitGitGadget, Nov 15, 2019
  124. 9/9 built-in add -i: implement the `help` commandSlavica Đukić via GitGitGadget, Nov 15, 2019

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.