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

[PATCH 2/2] git.c: binary-search builtin commands

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Feb 15, 2011, 03:09 UTC
Message-ID
<1297739344-28961-2-git-send-email-pclouds@gmail.com>
In-Reply-To
<1297739344-28961-1-git-send-email-pclouds@gmail.com>

An obvious implication of this patch: commands array must be in correct order.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
  2011/2/15 Junio C Hamano <gitster@pobox.com>:
  > I did this myself the other day, as I think it simply is a good project
  > hygiene.  If this were 1/2 of a series followed by 2/2 that runs binary
  > search in the table, that would make it make more sense ;-)
  
  I did think the array was binary-searched and nearly claimed "git-stage
  won't work because it's in wrong order".
  This patch won't give any performance gain, but it would force
  people to keep the array in order :-)
 git.c |   15 ++++++++++-----
 1 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/git.c b/git.c
index 57701e3..c36117a 100644
--- a/git.c
+++ b/git.c
@@ -308,6 +308,13 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
 	return 0;
 }
 
+static int cmd_cmp(const void *a, const void *b)
+{
+	const char *key = a;
+	const struct cmd_struct *cmd = b;
+	return strcmp(key, cmd->cmd);
+}
+
 static void handle_internal_command(int argc, const char **argv)
 {
 	const char *cmd = argv[0];
@@ -423,6 +430,7 @@ static void handle_internal_command(int argc, const char **argv)
 		{ "whatchanged", cmd_whatchanged, RUN_SETUP },
 		{ "write-tree", cmd_write_tree, RUN_SETUP },
 	};
+	struct cmd_struct *p;
 	int i;
 	static const char ext[] = STRIP_EXTENSION;
 
@@ -441,12 +449,9 @@ static void handle_internal_command(int argc, const char **argv)
 		argv[0] = cmd = "help";
 	}
 
-	for (i = 0; i < ARRAY_SIZE(commands); i++) {
-		struct cmd_struct *p = commands+i;
-		if (strcmp(p->cmd, cmd))
-			continue;
+	p = bsearch(cmd, commands, ARRAY_SIZE(commands), sizeof(*commands), cmd_cmp);
+	if (p)
 		exit(run_builtin(p, argc, argv));
-	}
 }
 
 static void execv_dashed_external(const char **argv)
-- 
1.7.4.74.g639db
Previous: Nguyễn Thái Ngọc DuyNext: Junio C Hamano
Message 4 of 6 in “git.c: reorder builtin command list”
  1. git.c: reorder builtin command listNguyễn Thái Ngọc Duy, Feb 14, 2011
  2. Junio C HamanoFeb 14, 2011
  3. 1/2 git.c: reorder builtin command listNguyễn Thái Ngọc Duy, Feb 15, 2011
  4. 2/2 git.c: binary-search builtin commandsNguyễn Thái Ngọc Duy, Feb 15, 2011
  5. Junio C HamanoFeb 15, 2011
  6. Nguyen Thai Ngoc DuyFeb 16, 2011

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.