* [PATCH] diff: optimise parse_dirstat_params() to only compare strings when necessary
@ 2014-03-20 0:07 Dragos Foianu
2014-03-20 0:15 ` Dragos Foianu
2014-03-20 17:18 ` Junio C Hamano
0 siblings, 2 replies; 3+ messages in thread
From: Dragos Foianu @ 2014-03-20 0:07 UTC (permalink / raw)
To: git; +Cc: Dragos Foianu
parse_dirstat_params() goes through a chain of if statements using
strcmp to parse parameters. When the parameter is a digit, the
value must go through all comparisons before the function realises
it is a digit. Optimise this logic by only going through the chain
of string compares when the parameter is not a digit.
Signed-off-by: Dragos Foianu <dragos.foianu@gmail.com>
---
diff.c | 37 +++++++++++++++++++------------------
1 file changed, 19 insertions(+), 18 deletions(-)
diff --git a/diff.c b/diff.c
index e343191..733764e 100644
--- a/diff.c
+++ b/diff.c
@@ -84,20 +84,25 @@ static int parse_dirstat_params(struct diff_options *options, const char *params
string_list_split_in_place(¶ms, params_copy, ',', -1);
for (i = 0; i < params.nr; i++) {
const char *p = params.items[i].string;
- if (!strcmp(p, "changes")) {
- DIFF_OPT_CLR(options, DIRSTAT_BY_LINE);
- DIFF_OPT_CLR(options, DIRSTAT_BY_FILE);
- } else if (!strcmp(p, "lines")) {
- DIFF_OPT_SET(options, DIRSTAT_BY_LINE);
- DIFF_OPT_CLR(options, DIRSTAT_BY_FILE);
- } else if (!strcmp(p, "files")) {
- DIFF_OPT_CLR(options, DIRSTAT_BY_LINE);
- DIFF_OPT_SET(options, DIRSTAT_BY_FILE);
- } else if (!strcmp(p, "noncumulative")) {
- DIFF_OPT_CLR(options, DIRSTAT_CUMULATIVE);
- } else if (!strcmp(p, "cumulative")) {
- DIFF_OPT_SET(options, DIRSTAT_CUMULATIVE);
- } else if (isdigit(*p)) {
+ if (!isdigit(*p)) {
+ if (!strcmp(p, "changes")) {
+ DIFF_OPT_CLR(options, DIRSTAT_BY_LINE);
+ DIFF_OPT_CLR(options, DIRSTAT_BY_FILE);
+ } else if (!strcmp(p, "lines")) {
+ DIFF_OPT_SET(options, DIRSTAT_BY_LINE);
+ DIFF_OPT_CLR(options, DIRSTAT_BY_FILE);
+ } else if (!strcmp(p, "files")) {
+ DIFF_OPT_CLR(options, DIRSTAT_BY_LINE);
+ DIFF_OPT_SET(options, DIRSTAT_BY_FILE);
+ } else if (!strcmp(p, "noncumulative")) {
+ DIFF_OPT_CLR(options, DIRSTAT_CUMULATIVE);
+ } else if (!strcmp(p, "cumulative")) {
+ DIFF_OPT_SET(options, DIRSTAT_CUMULATIVE);
+ } else {
+ strbuf_addf(errmsg, _(" Unknown dirstat parameter '%s'\n"), p);
+ ret++;
+ }
+ } else {
char *end;
int permille = strtoul(p, &end, 10) * 10;
if (*end == '.' && isdigit(*++end)) {
@@ -114,11 +119,7 @@ static int parse_dirstat_params(struct diff_options *options, const char *params
p);
ret++;
}
- } else {
- strbuf_addf(errmsg, _(" Unknown dirstat parameter '%s'\n"), p);
- ret++;
}
-
}
string_list_clear(¶ms, 0);
free(params_copy);
--
1.8.3.2
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] diff: optimise parse_dirstat_params() to only compare strings when necessary
2014-03-20 0:07 [PATCH] diff: optimise parse_dirstat_params() to only compare strings when necessary Dragos Foianu
@ 2014-03-20 0:15 ` Dragos Foianu
2014-03-20 17:18 ` Junio C Hamano
1 sibling, 0 replies; 3+ messages in thread
From: Dragos Foianu @ 2014-03-20 0:15 UTC (permalink / raw)
To: git
I will send another version of this patch after review because there is an
extra whitespace following the else statement.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] diff: optimise parse_dirstat_params() to only compare strings when necessary
2014-03-20 0:07 [PATCH] diff: optimise parse_dirstat_params() to only compare strings when necessary Dragos Foianu
2014-03-20 0:15 ` Dragos Foianu
@ 2014-03-20 17:18 ` Junio C Hamano
1 sibling, 0 replies; 3+ messages in thread
From: Junio C Hamano @ 2014-03-20 17:18 UTC (permalink / raw)
To: Dragos Foianu; +Cc: git
Dragos Foianu <dragos.foianu@gmail.com> writes:
> parse_dirstat_params() goes through a chain of if statements using
> strcmp to parse parameters. When the parameter is a digit, the
> value must go through all comparisons before the function realises
> it is a digit. Optimise this logic by only going through the chain
> of string compares when the parameter is not a digit.
This change could be an optimization only if parse_dirstat_params()
is called with a param that begins with a digit a lot more often
than with other forms of params, but that is a mere assumption.
Unless that assumption is substantiated, this change can be a
pessimization.
Even if the assumption were true (which I doubt), a simpler solution
to optimize such a call pattern would be to simply tweak of the
order if/else cascade to check if the param begins with a digit
first before checking other keywords, wouldn't it? I am not sure
why you even need to change the structure into a nested if
statement.
> Signed-off-by: Dragos Foianu <dragos.foianu@gmail.com>
> ---
> diff.c | 37 +++++++++++++++++++------------------
> 1 file changed, 19 insertions(+), 18 deletions(-)
>
> diff --git a/diff.c b/diff.c
> index e343191..733764e 100644
> --- a/diff.c
> +++ b/diff.c
> @@ -84,20 +84,25 @@ static int parse_dirstat_params(struct diff_options *options, const char *params
> string_list_split_in_place(¶ms, params_copy, ',', -1);
> for (i = 0; i < params.nr; i++) {
> const char *p = params.items[i].string;
> - if (!strcmp(p, "changes")) {
> - DIFF_OPT_CLR(options, DIRSTAT_BY_LINE);
> - DIFF_OPT_CLR(options, DIRSTAT_BY_FILE);
> - } else if (!strcmp(p, "lines")) {
> - DIFF_OPT_SET(options, DIRSTAT_BY_LINE);
> - DIFF_OPT_CLR(options, DIRSTAT_BY_FILE);
> - } else if (!strcmp(p, "files")) {
> - DIFF_OPT_CLR(options, DIRSTAT_BY_LINE);
> - DIFF_OPT_SET(options, DIRSTAT_BY_FILE);
> - } else if (!strcmp(p, "noncumulative")) {
> - DIFF_OPT_CLR(options, DIRSTAT_CUMULATIVE);
> - } else if (!strcmp(p, "cumulative")) {
> - DIFF_OPT_SET(options, DIRSTAT_CUMULATIVE);
> - } else if (isdigit(*p)) {
> + if (!isdigit(*p)) {
> + if (!strcmp(p, "changes")) {
> + DIFF_OPT_CLR(options, DIRSTAT_BY_LINE);
> + DIFF_OPT_CLR(options, DIRSTAT_BY_FILE);
> + } else if (!strcmp(p, "lines")) {
> + DIFF_OPT_SET(options, DIRSTAT_BY_LINE);
> + DIFF_OPT_CLR(options, DIRSTAT_BY_FILE);
> + } else if (!strcmp(p, "files")) {
> + DIFF_OPT_CLR(options, DIRSTAT_BY_LINE);
> + DIFF_OPT_SET(options, DIRSTAT_BY_FILE);
> + } else if (!strcmp(p, "noncumulative")) {
> + DIFF_OPT_CLR(options, DIRSTAT_CUMULATIVE);
> + } else if (!strcmp(p, "cumulative")) {
> + DIFF_OPT_SET(options, DIRSTAT_CUMULATIVE);
> + } else {
> + strbuf_addf(errmsg, _(" Unknown dirstat parameter '%s'\n"), p);
> + ret++;
> + }
> + } else {
> char *end;
> int permille = strtoul(p, &end, 10) * 10;
> if (*end == '.' && isdigit(*++end)) {
> @@ -114,11 +119,7 @@ static int parse_dirstat_params(struct diff_options *options, const char *params
> p);
> ret++;
> }
> - } else {
> - strbuf_addf(errmsg, _(" Unknown dirstat parameter '%s'\n"), p);
> - ret++;
> }
> -
> }
> string_list_clear(¶ms, 0);
> free(params_copy);
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2014-03-20 17:19 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-03-20 0:07 [PATCH] diff: optimise parse_dirstat_params() to only compare strings when necessary Dragos Foianu
2014-03-20 0:15 ` Dragos Foianu
2014-03-20 17:18 ` Junio C Hamano
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 NNTP newsgroup(s).