Skip to contentSkip to conversation

builtin/apply.c: fuzzy_matchlines:trying to fix some inefficiencies

PATCH In review George Papanikolaou 2 participants 3 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 builtin/apply.c: fuzzy_matchlines:trying to fix some inefficiencies

Hi fellows,
I'm planning on applying on GSOC 2014...

I tried my luck with that kinda weird microproject about inefficiencies, and I think I've discovered some.

(also on a totally different mood, there are some warning about empty format strings during compilation that could easily be silenced with some #pragma calls on "-Wformat-zero-length". Is there a way you're not adding this?)

The empty buffers check could happen at the beggining. Leading whitespace check was unnecessary. Some style changes

Thanks.

---
 builtin/apply.c | 25 +++++++++----------------
 1 file changed, 9 insertions(+), 16 deletions(-)
builtin/apply.c +9 -16
index b0d0986..df2435f 100644
@@ -294,20 +294,16 @@ static int fuzzy_matchlines(const char *s1, size_t n1,
 	const char *last2 = s2 + n2 - 1;
 	int result = 0;
 
+	/* early return if both lines are empty */
+	if ((s1 > last1) && (s2 > last2))
+		return 1;
+
 	/* ignore line endings */
 	while ((*last1 == '\r') || (*last1 == '\n'))
 		last1--;
 	while ((*last2 == '\r') || (*last2 == '\n'))
 		last2--;
 
-	/* skip leading whitespace */
-	while (isspace(*s1) && (s1 <= last1))
-		s1++;
-	while (isspace(*s2) && (s2 <= last2))
-		s2++;
-	/* early return if both lines are empty */
-	if ((s1 > last1) && (s2 > last2))
-		return 1;
 	while (!result) {
 		result = *s1++ - *s2++;
 		/*
@@ -315,18 +311,15 @@ static int fuzzy_matchlines(const char *s1, size_t n1,
 		 * both buffers because we don't want "a b" to match
 		 * "ab"
 		 */
-		if (isspace(*s1) && isspace(*s2)) {
-			while (isspace(*s1) && s1 <= last1)
-				s1++;
-			while (isspace(*s2) && s2 <= last2)
-				s2++;
-		}
+		while (isspace(*s1) && s1 <= last1)
+			s1++;
+		while (isspace(*s2) && s2 <= last2)
+			s2++;
 		/*
 		 * If we reached the end on one side only,
 		 * lines don't match
 		 */
-		if (
-		    ((s2 > last2) && (s1 <= last1)) ||
+		if (((s2 > last2) && (s1 <= last1)) ||
 		    ((s1 > last1) && (s2 <= last2)))
 			return 0;
 		if ((s1 > last1) && (s2 > last2))
Signature · 1 line
1.9.0
#1 lore raw

Hello and welcome. See the comments inline.

Quoted 9 lines · George Papanikolaou

Hi fellows,
I'm planning on applying on GSOC 2014...

I tried my luck with that kinda weird microproject about inefficiencies, and I think I've discovered some.

(also on a totally different mood, there are some warning about empty format strings during compilation that could easily be silenced with some #pragma calls on "-Wformat-zero-length". Is there a way you're not adding this?)

The main reason is probably that #pragmas are compiler-specific. It is undesirable to clutter up the source code with ugly #pragmas that only benefit people using gcc.

I think that most people who use gcc compile with -Wno-format-zero-length. FWIW, the options that I use are

    O = 2
    CFLAGS = -g -O$(O) -Wall -Werror -Wdeclaration-after-statement

-Wno-format-zero-length -Wno-format-security $(EXTRA_CFLAGS)

, which you can put in your config.mak.

Quoted 1 line

The empty buffers check could happen at the beggining.

s/beggining/beginning/

Quoted 5 lines

Leading whitespace check was unnecessary.
Some style changes

Thanks.
---

Please pay attention to how patches have to be formatted:

The subject of the email and everything above the "---" line is used as the commit's log message. This should only include information that belongs in the Git project's permanent history, not incidental information like the fact that you are applying for GSoC.

The commit message *should* include an explanation of *why* you are making a change, any tradeoffs that might be involved, etc.

The commit message also *must* include a Signed-off-by line.

Other discussion, not intended for the commit message, should be placed directly *under* the "---" line.

Quoted 8 lines
 builtin/apply.c | 25 +++++++++----------------
 1 file changed, 9 insertions(+), 16 deletions(-)
builtin/apply.c +0 -0
index b0d0986..df2435f 100644

builtin/apply.c static int fuzzy_matchlines(const char *s1, size_t n1, · line 300

Quoted 7 lines
builtin/apply.c +4 -0
@@ -294,20 +294,16 @@ static int fuzzy_matchlines(const char *s1, size_t n1,
 	const char *last2 = s2 + n2 - 1;
 	int result = 0;
 
+	/* early return if both lines are empty */
+	if ((s1 > last1) && (s2 > last2))
+		return 1;
+

Why is this an improvement? Do you expect this function to be called often for empty lines (as opposed, for example, to lines consisting solely of whitespace characters)?

Quoted 21 lines
builtin/apply.c +0 -8
 	/* ignore line endings */
 	while ((*last1 == '\r') || (*last1 == '\n'))
 		last1--;
 	while ((*last2 == '\r') || (*last2 == '\n'))
 		last2--;
 
-	/* skip leading whitespace */
-	while (isspace(*s1) && (s1 <= last1))
-		s1++;
-	while (isspace(*s2) && (s2 <= last2))
-		s2++;
-	/* early return if both lines are empty */
-	if ((s1 > last1) && (s2 > last2))
-		return 1;
 	while (!result) {
 		result = *s1++ - *s2++;
 		/*
@@ -315,18 +311,15 @@ static int fuzzy_matchlines(const char *s1, size_t n1,
 		 * both buffers because we don't want "a b" to match
 		 * "ab"
 		 */

builtin/apply.c static int fuzzy_matchlines(const char *s1, size_t n1, · line 317

Quoted 10 lines
builtin/apply.c +4 -6
-		if (isspace(*s1) && isspace(*s2)) {
-			while (isspace(*s1) && s1 <= last1)
-				s1++;
-			while (isspace(*s2) && s2 <= last2)
-				s2++;
-		}
+		while (isspace(*s1) && s1 <= last1)
+			s1++;
+		while (isspace(*s2) && s2 <= last2)
+			s2++;

The comment just above this change gives a justification for putting an "if" statement surrounding the "while" statements. Do you think the comment's argument is incorrect? If so, please explain why, and remove or change the comment.

builtin/apply.c static int fuzzy_matchlines(const char *s1, size_t n1, · line 322

Quoted 7 lines
builtin/apply.c +1 -2
 		/*
 		 * If we reached the end on one side only,
 		 * lines don't match
 		 */
-		if (
-		    ((s2 > last2) && (s1 <= last1)) ||
+		if (((s2 > last2) && (s1 <= last1)) ||

This reformatting doesn't have anything to do with the rest of your patch. If it were important enough to make this change, then it should be submitted as a separate patch. But it is probably not important enough, and is only code churn, so it should probably be omitted entirely.

Quoted 4 lines
builtin/apply.c +0 -0
 		    ((s1 > last1) && (s2 <= last2)))
 			return 0;
 		if ((s1 > last1) && (s2 > last2))
Signature · 3 lines
Michael Haggerty
[email protected]
http://softwareswirl.blogspot.com/
#2 lore raw

Hi,
Thanks for the feedback,

Quoted 5 lines · Michael Haggerty

Why is this an improvement? Do you expect this function to be called often for empty lines (as opposed, for example, to lines consisting solely of whitespace characters)?

Yes, you are probably right, we are not gonna get much (if any) completely empty lines

Quoted 6 lines

The comment just above this change gives a justification for putting an "if" statement surrounding the "while" statements. Do you think the comment's argument is incorrect? If so, please explain why, and remove or change the comment.

I see what I did wrong. I thought since that the if-condition is double checked (from the while clause) so I removed it.

Also this lead me to see that since the while clause is now unconditioned, there is no point of it being replicated exactly the same above, so I removed that too. =(

I'm trying to find other inefficiencies/irregularities on that function. I'm currently
thinking on merging the first checks with a call to iswspace() or something similar.

Also thanks for clarifying the way patches/mails work.

Cheers.

---
papanikge's surrogate email.
I may reply back.
http://www.5slingshots.com/