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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

Signed-off-by: Nguyán ThÃi Ngác Duy <pclouds@xxxxxxxxx>
---
  2011/2/15 Junio C Hamano <gitster@xxxxxxxxx>:
  > 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

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]