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

[PATCH] diff: optimise parse_dirstat_params() to only compare strings when necessary

From
DFDragos Foianu <dragos.foianu@gmail.com>
Date
Mar 20, 2014, 00:07 UTC
Message-ID
<1395274076-6720-1-git-send-email-dragos.foianu@gmail.com>

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(&params, 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(&params, 0);
 	free(params_copy);
-- 
1.8.3.2
Next: Dragos Foianu
Message 1 of 3 in “diff: optimise parse_dirstat_params() to only compare strings when necessary”
  1. diff: optimise parse_dirstat_params() to only compare strings when necessaryDragos Foianu, Mar 20, 2014
  2. Dragos FoianuMar 20, 2014
  3. Junio C HamanoMar 20, 2014

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.