git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>
Cc: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH 2/2] git.c: binary-search builtin commands
Date: Tue, 15 Feb 2011 10:09:04 +0700	[thread overview]
Message-ID: <1297739344-28961-2-git-send-email-pclouds@gmail.com> (raw)
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

  reply	other threads:[~2011-02-15  3:06 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-02-14 13:26 [PATCH] git.c: reorder builtin command list Nguyễn Thái Ngọc Duy
2011-02-14 19:20 ` Junio C Hamano
2011-02-15  3:09   ` [PATCH 1/2] " Nguyễn Thái Ngọc Duy
2011-02-15  3:09     ` Nguyễn Thái Ngọc Duy [this message]
2011-02-15 20:12       ` [PATCH 2/2] git.c: binary-search builtin commands Junio C Hamano
2011-02-16  3:46         ` Nguyen Thai Ngoc Duy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1297739344-28961-2-git-send-email-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).