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

[PATCH] Reuse cmdname->len to store pre-calculated similarity indexes

From
Alex Riesen <raa.lkml@gmail.com>
Date
Aug 30, 2008, 17:13 UTC
Message-ID
<20080830171331.GA26932@steel.home>
In-Reply-To
<81b0412b0808300944p29199600ie95c65404b6cb380@mail.gmail.com>
To avoid doing that while sorting
Signed-off-by: Alex Riesen <raa.lkml@gmail.com>
---
Alex Riesen, Sat, Aug 30, 2008 18:44:15 +0200:
Show 11 quoted lines
> 2008/8/30 Junio C Hamano <gitster@pobox.com>:
> > I wonder if it makes sense to give an otherwise unused "score" member to
> 
> Hmm, it is a _non-existing_ member of cmdname, isn't it?
> 
> > the "struct cmdname", compute the distance only once per each command, and
> > use that as the sort key (alternatively you can have a separate int[N]
> > array to store similarity values for each item in the cmdnames list, only
> > used inside this codepath).
> 
> I think I'll take the struct cmdname->len over.
 help.c |   12 +++++++-----
 1 files changed, 7 insertions(+), 5 deletions(-)
diff --git a/help.c b/help.c
index 7bfbbcd..70d57a3 100644
--- a/help.c
+++ b/help.c
@@ -287,8 +287,8 @@ static int levenshtein_compare(const void *p1, const void *p2)
 {
 	const struct cmdname *const *c1 = p1, *const *c2 = p2;
 	const char *s1 = (*c1)->name, *s2 = (*c2)->name;
-	int l1 = similarity(s1);
-	int l2 = similarity(s2);
+	int l1 = (*c1)->len;
+	int l2 = (*c2)->len;
 	return l1 != l2 ? l1 - l2 : strcmp(s1, s2);
 }
 
@@ -312,6 +312,9 @@ const char *help_unknown_cmd(const char *cmd)
 	memcpy(main_cmds.names + main_cmds.cnt, other_cmds.names,
 		other_cmds.cnt * sizeof(other_cmds.names[0]));
 	main_cmds.cnt += other_cmds.cnt;
+	/* This reuses cmdname->len for similarity index */
+	for (i = 0; i < main_cmds.cnt; ++i)
+		main_cmds.names[i]->len = similarity(main_cmds.names[i]->name);
 
 	levenshtein_cmd = cmd;
 	qsort(main_cmds.names, main_cmds.cnt,
@@ -319,10 +322,9 @@ const char *help_unknown_cmd(const char *cmd)
 
 	if (!main_cmds.cnt)
 		die ("Uh oh.  Your system reports no Git commands at all.");
-	best_similarity = similarity(main_cmds.names[0]->name);
+	best_similarity = main_cmds.names[0]->len;
 	n = 1;
-	while (n < main_cmds.cnt &&
-		best_similarity == similarity(main_cmds.names[n]->name))
+	while (n < main_cmds.cnt && best_similarity == main_cmds.names[n]->len)
 		++n;
 	if (autocorrect && n == 1) {
 		const char *assumed;
-- 
1.6.0.1.149.g9ecb0
Previous: Alex RiesenNext: Junio C Hamano
Message 12 of 19 in “Remove calculation of the longest command name from where it is not used”
  1. Remove calculation of the longest command name from where it is not usedAlex Riesen, Aug 28, 2008
  2. git wrapper: DWIM mistyped commandsAlex Riesen, Aug 28, 2008
  3. Add help.autocorrect to enable/disable autocorrectingAlex Riesen, Aug 28, 2008
  4. Andreas EricssonAug 29, 2008
  5. Junio C HamanoSep 8, 2008
  6. Mikael MagnussonAug 29, 2008
  7. Alex RiesenAug 30, 2008
  8. Mikael MagnussonAug 30, 2008
  9. Alex RiesenAug 31, 2008
  10. Junio C HamanoAug 30, 2008
  11. Alex RiesenAug 30, 2008
  12. Reuse cmdname->len to store pre-calculated similarity indexesAlex Riesen, Aug 30, 2008
  13. Junio C HamanoAug 30, 2008
  14. git wrapper: DWIM mistyped commandsAlex Riesen, Aug 31, 2008
  15. Add help.autocorrect to enable/disable autocorrectingAlex Riesen, Aug 31, 2008
  16. Matthieu MoyAug 31, 2008
  17. Junio C HamanoAug 31, 2008
  18. Mikael MagnussonSep 1, 2008
  19. Felipe Carvalho OliveiraAug 30, 2008

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.