[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