D943: chg: move only first time relevant if condition out of loop

singhsrb (Saurabh Singh) phabricator at mercurial-scm.org
Wed Oct 4 23:22:37 UTC 2017


singhsrb created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  The loop needlessly checks the condition after the first iteration.
  Therefore, moving this condition outside the loop. Compiler can potentially
  optimize this but not depending on it.

TEST PLAN
  Ran 'test-chg.t' with '--chg' option.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D943

AFFECTED FILES
  contrib/chg/chg.c

CHANGE DETAILS

diff --git a/contrib/chg/chg.c b/contrib/chg/chg.c
--- a/contrib/chg/chg.c
+++ b/contrib/chg/chg.c
@@ -372,14 +372,20 @@
 		TIME = 4,
 	};
 	unsigned int state = 0;
-	int i;
-	for (i = 0; i < argc; ++i) {
+	int i = 0;
+
+	if (argc > 0) {
+		const char *const firstarg = argv[i];
+		if (strcmp("serve", firstarg) == 0) {
+			state |= SERVE;
+			++i;
+		}
+	}
+
+	for (; i < argc; ++i) {
 		if (strcmp(argv[i], "--") == 0)
 			break;
-		if (i == 0 && strcmp("serve", argv[i]) == 0)
-			state |= SERVE;
-		else if (strcmp("-d", argv[i]) == 0 ||
-			 strcmp("--daemon", argv[i]) == 0)
+		if (strcmp("-d", argv[i]) == 0 || strcmp("--daemon", argv[i]) == 0)
 			state |= DAEMON;
 		else if (strcmp("--time", argv[i]) == 0)
 			state |= TIME;



To: singhsrb, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list