git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH 1/2] commit: be more precise when searching for headers
@ 2017-02-25 19:21 René Scharfe
  2017-02-25 19:27 ` [PATCH 2/2] commit: don't check for space twice when looking for header René Scharfe
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: René Scharfe @ 2017-02-25 19:21 UTC (permalink / raw)
  To: Git List; +Cc: Junio C Hamano

Search for a space character only within the current line in
read_commit_extra_header_lines() instead of searching in the whole
buffer (and possibly beyond, if it's not NUL-terminated) and then
discarding any results after the end of the current line.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 commit.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/commit.c b/commit.c
index 2cf85158b4..173c6d3818 100644
--- a/commit.c
+++ b/commit.c
@@ -1354,8 +1354,8 @@ static struct commit_extra_header *read_commit_extra_header_lines(
 		strbuf_reset(&buf);
 		it = NULL;
 
-		eof = strchr(line, ' ');
-		if (next <= eof)
+		eof = memchr(line, ' ', next - line);
+		if (!eof)
 			eof = next;
 
 		if (standard_header_field(line, eof - line) ||
-- 
2.12.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 2/2] commit: don't check for space twice when looking for header
  2017-02-25 19:21 [PATCH 1/2] commit: be more precise when searching for headers René Scharfe
@ 2017-02-25 19:27 ` René Scharfe
  2017-02-25 20:15   ` Jeff King
  2017-02-27 22:27   ` Jakub Narębski
  2017-02-25 20:12 ` [PATCH 1/2] commit: be more precise when searching for headers Jeff King
  2017-02-27 19:18 ` Junio C Hamano
  2 siblings, 2 replies; 9+ messages in thread
From: René Scharfe @ 2017-02-25 19:27 UTC (permalink / raw)
  To: Git List; +Cc: Junio C Hamano

Both standard_header_field() and excluded_header_field() check if
there's a space after the buffer that's handed to them.  We already
check in the caller if that space is present.  Don't bother calling
the functions if it's missing, as they are guaranteed to return 0 in
that case, and remove the now redundant checks from them.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 commit.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/commit.c b/commit.c
index 173c6d3818..fab8269731 100644
--- a/commit.c
+++ b/commit.c
@@ -1308,11 +1308,11 @@ void for_each_mergetag(each_mergetag_fn fn, struct commit *commit, void *data)
 
 static inline int standard_header_field(const char *field, size_t len)
 {
-	return ((len == 4 && !memcmp(field, "tree ", 5)) ||
-		(len == 6 && !memcmp(field, "parent ", 7)) ||
-		(len == 6 && !memcmp(field, "author ", 7)) ||
-		(len == 9 && !memcmp(field, "committer ", 10)) ||
-		(len == 8 && !memcmp(field, "encoding ", 9)));
+	return ((len == 4 && !memcmp(field, "tree", 4)) ||
+		(len == 6 && !memcmp(field, "parent", 6)) ||
+		(len == 6 && !memcmp(field, "author", 6)) ||
+		(len == 9 && !memcmp(field, "committer", 9)) ||
+		(len == 8 && !memcmp(field, "encoding", 8)));
 }
 
 static int excluded_header_field(const char *field, size_t len, const char **exclude)
@@ -1322,8 +1322,7 @@ static int excluded_header_field(const char *field, size_t len, const char **exc
 
 	while (*exclude) {
 		size_t xlen = strlen(*exclude);
-		if (len == xlen &&
-		    !memcmp(field, *exclude, xlen) && field[xlen] == ' ')
+		if (len == xlen && !memcmp(field, *exclude, xlen))
 			return 1;
 		exclude++;
 	}
@@ -1357,9 +1356,8 @@ static struct commit_extra_header *read_commit_extra_header_lines(
 		eof = memchr(line, ' ', next - line);
 		if (!eof)
 			eof = next;
-
-		if (standard_header_field(line, eof - line) ||
-		    excluded_header_field(line, eof - line, exclude))
+		else if (standard_header_field(line, eof - line) ||
+			 excluded_header_field(line, eof - line, exclude))
 			continue;
 
 		it = xcalloc(1, sizeof(*it));
-- 
2.12.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/2] commit: be more precise when searching for headers
  2017-02-25 19:21 [PATCH 1/2] commit: be more precise when searching for headers René Scharfe
  2017-02-25 19:27 ` [PATCH 2/2] commit: don't check for space twice when looking for header René Scharfe
@ 2017-02-25 20:12 ` Jeff King
  2017-02-27 19:18 ` Junio C Hamano
  2 siblings, 0 replies; 9+ messages in thread
From: Jeff King @ 2017-02-25 20:12 UTC (permalink / raw)
  To: René Scharfe; +Cc: Git List, Junio C Hamano

On Sat, Feb 25, 2017 at 08:21:52PM +0100, René Scharfe wrote:

> Search for a space character only within the current line in
> read_commit_extra_header_lines() instead of searching in the whole
> buffer (and possibly beyond, if it's not NUL-terminated) and then
> discarding any results after the end of the current line.
> [...]
> -		eof = strchr(line, ' ');
> -		if (next <= eof)
> +		eof = memchr(line, ' ', next - line);
> +		if (!eof)
>  			eof = next;

Nice. More efficient, and I think the intent is more clear.

-Peff

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2/2] commit: don't check for space twice when looking for header
  2017-02-25 19:27 ` [PATCH 2/2] commit: don't check for space twice when looking for header René Scharfe
@ 2017-02-25 20:15   ` Jeff King
  2017-02-25 21:39     ` René Scharfe
  2017-02-27 22:27   ` Jakub Narębski
  1 sibling, 1 reply; 9+ messages in thread
From: Jeff King @ 2017-02-25 20:15 UTC (permalink / raw)
  To: René Scharfe; +Cc: Git List, Junio C Hamano

On Sat, Feb 25, 2017 at 08:27:40PM +0100, René Scharfe wrote:

> Both standard_header_field() and excluded_header_field() check if
> there's a space after the buffer that's handed to them.  We already
> check in the caller if that space is present.  Don't bother calling
> the functions if it's missing, as they are guaranteed to return 0 in
> that case, and remove the now redundant checks from them.

Makes sense, and I couldn't spot any errors in your logic or in the
code.

>  static inline int standard_header_field(const char *field, size_t len)
>  {
> -	return ((len == 4 && !memcmp(field, "tree ", 5)) ||
> -		(len == 6 && !memcmp(field, "parent ", 7)) ||
> -		(len == 6 && !memcmp(field, "author ", 7)) ||
> -		(len == 9 && !memcmp(field, "committer ", 10)) ||
> -		(len == 8 && !memcmp(field, "encoding ", 9)));
> +	return ((len == 4 && !memcmp(field, "tree", 4)) ||
> +		(len == 6 && !memcmp(field, "parent", 6)) ||
> +		(len == 6 && !memcmp(field, "author", 6)) ||
> +		(len == 9 && !memcmp(field, "committer", 9)) ||
> +		(len == 8 && !memcmp(field, "encoding", 8)));

Unrelated, but this could probably be spelled with a macro and strlen()
to avoid the magic numbers. It would probably be measurably slower for a
compiler which doesn't pre-compute strlen() on a string literal, though.

-Peff

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2/2] commit: don't check for space twice when looking for header
  2017-02-25 20:15   ` Jeff King
@ 2017-02-25 21:39     ` René Scharfe
  2017-02-25 21:51       ` Jeff King
  0 siblings, 1 reply; 9+ messages in thread
From: René Scharfe @ 2017-02-25 21:39 UTC (permalink / raw)
  To: Jeff King; +Cc: Git List, Junio C Hamano

Am 25.02.2017 um 21:15 schrieb Jeff King:
> On Sat, Feb 25, 2017 at 08:27:40PM +0100, René Scharfe wrote:
>
>> Both standard_header_field() and excluded_header_field() check if
>> there's a space after the buffer that's handed to them.  We already
>> check in the caller if that space is present.  Don't bother calling
>> the functions if it's missing, as they are guaranteed to return 0 in
>> that case, and remove the now redundant checks from them.
>
> Makes sense, and I couldn't spot any errors in your logic or in the
> code.

Thanks for checking!

>>  static inline int standard_header_field(const char *field, size_t len)
>>  {
>> -	return ((len == 4 && !memcmp(field, "tree ", 5)) ||
>> -		(len == 6 && !memcmp(field, "parent ", 7)) ||
>> -		(len == 6 && !memcmp(field, "author ", 7)) ||
>> -		(len == 9 && !memcmp(field, "committer ", 10)) ||
>> -		(len == 8 && !memcmp(field, "encoding ", 9)));
>> +	return ((len == 4 && !memcmp(field, "tree", 4)) ||
>> +		(len == 6 && !memcmp(field, "parent", 6)) ||
>> +		(len == 6 && !memcmp(field, "author", 6)) ||
>> +		(len == 9 && !memcmp(field, "committer", 9)) ||
>> +		(len == 8 && !memcmp(field, "encoding", 8)));
>
> Unrelated, but this could probably be spelled with a macro and strlen()
> to avoid the magic numbers. It would probably be measurably slower for a
> compiler which doesn't pre-compute strlen() on a string literal, though.

sizeof(string_constant) - 1 might be a better choice here than strlen().

René

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2/2] commit: don't check for space twice when looking for header
  2017-02-25 21:39     ` René Scharfe
@ 2017-02-25 21:51       ` Jeff King
  0 siblings, 0 replies; 9+ messages in thread
From: Jeff King @ 2017-02-25 21:51 UTC (permalink / raw)
  To: René Scharfe; +Cc: Git List, Junio C Hamano

On Sat, Feb 25, 2017 at 10:39:29PM +0100, René Scharfe wrote:

> > > +		(len == 8 && !memcmp(field, "encoding", 8)));
> > 
> > Unrelated, but this could probably be spelled with a macro and strlen()
> > to avoid the magic numbers. It would probably be measurably slower for a
> > compiler which doesn't pre-compute strlen() on a string literal, though.
> 
> sizeof(string_constant) - 1 might be a better choice here than strlen().

Yeah. If you use a macro, that works. If it's an inline function you'd
need strlen(). That's a tradeoff we've already made in skip_prefix_mem()
and strip_suffix(), but it's not like we expect this list to grow much,
so it may not be worth fussing with.

-Peff

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/2] commit: be more precise when searching for headers
  2017-02-25 19:21 [PATCH 1/2] commit: be more precise when searching for headers René Scharfe
  2017-02-25 19:27 ` [PATCH 2/2] commit: don't check for space twice when looking for header René Scharfe
  2017-02-25 20:12 ` [PATCH 1/2] commit: be more precise when searching for headers Jeff King
@ 2017-02-27 19:18 ` Junio C Hamano
  2 siblings, 0 replies; 9+ messages in thread
From: Junio C Hamano @ 2017-02-27 19:18 UTC (permalink / raw)
  To: René Scharfe; +Cc: Git List

René Scharfe <l.s.r@web.de> writes:

> Search for a space character only within the current line in
> read_commit_extra_header_lines() instead of searching in the whole
> buffer (and possibly beyond, if it's not NUL-terminated) and then
> discarding any results after the end of the current line.
>
> Signed-off-by: Rene Scharfe <l.s.r@web.de>
> ---
>  commit.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)

Makes sense.

> diff --git a/commit.c b/commit.c
> index 2cf85158b4..173c6d3818 100644
> --- a/commit.c
> +++ b/commit.c
> @@ -1354,8 +1354,8 @@ static struct commit_extra_header *read_commit_extra_header_lines(
>  		strbuf_reset(&buf);
>  		it = NULL;
>  
> -		eof = strchr(line, ' ');
> -		if (next <= eof)
> +		eof = memchr(line, ' ', next - line);
> +		if (!eof)
>  			eof = next;
>  
>  		if (standard_header_field(line, eof - line) ||

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2/2] commit: don't check for space twice when looking for header
  2017-02-25 19:27 ` [PATCH 2/2] commit: don't check for space twice when looking for header René Scharfe
  2017-02-25 20:15   ` Jeff King
@ 2017-02-27 22:27   ` Jakub Narębski
  2017-02-27 22:54     ` René Scharfe
  1 sibling, 1 reply; 9+ messages in thread
From: Jakub Narębski @ 2017-02-27 22:27 UTC (permalink / raw)
  To: René Scharfe, Git List; +Cc: Junio C Hamano

W dniu 25.02.2017 o 20:27, René Scharfe pisze:
> Both standard_header_field() and excluded_header_field() check if
> there's a space after the buffer that's handed to them.  We already
> check in the caller if that space is present.  Don't bother calling
> the functions if it's missing, as they are guaranteed to return 0 in
> that case, and remove the now redundant checks from them.
> 
> Signed-off-by: Rene Scharfe <l.s.r@web.de>
> ---
>  commit.c | 18 ++++++++----------
>  1 file changed, 8 insertions(+), 10 deletions(-)
> 
> diff --git a/commit.c b/commit.c
> index 173c6d3818..fab8269731 100644
> --- a/commit.c
> +++ b/commit.c
> @@ -1308,11 +1308,11 @@ void for_each_mergetag(each_mergetag_fn fn, struct commit *commit, void *data)
>  
>  static inline int standard_header_field(const char *field, size_t len)
>  {
> -	return ((len == 4 && !memcmp(field, "tree ", 5)) ||
> -		(len == 6 && !memcmp(field, "parent ", 7)) ||
> -		(len == 6 && !memcmp(field, "author ", 7)) ||
> -		(len == 9 && !memcmp(field, "committer ", 10)) ||
> -		(len == 8 && !memcmp(field, "encoding ", 9)));
> +	return ((len == 4 && !memcmp(field, "tree", 4)) ||
> +		(len == 6 && !memcmp(field, "parent", 6)) ||
> +		(len == 6 && !memcmp(field, "author", 6)) ||
> +		(len == 9 && !memcmp(field, "committer", 9)) ||
> +		(len == 8 && !memcmp(field, "encoding", 8)));

I agree (for what it is worth from me) with the rest of changes,
but I think current code is better self-documenting for this
function.

>  }
>  
>  static int excluded_header_field(const char *field, size_t len, const char **exclude)
> @@ -1322,8 +1322,7 @@ static int excluded_header_field(const char *field, size_t len, const char **exc
>  
>  	while (*exclude) {
>  		size_t xlen = strlen(*exclude);
> -		if (len == xlen &&
> -		    !memcmp(field, *exclude, xlen) && field[xlen] == ' ')
> +		if (len == xlen && !memcmp(field, *exclude, xlen))
>  			return 1;
>  		exclude++;
>  	}
> @@ -1357,9 +1356,8 @@ static struct commit_extra_header *read_commit_extra_header_lines(
>  		eof = memchr(line, ' ', next - line);
>  		if (!eof)
>  			eof = next;
> -
> -		if (standard_header_field(line, eof - line) ||
> -		    excluded_header_field(line, eof - line, exclude))
> +		else if (standard_header_field(line, eof - line) ||
> +			 excluded_header_field(line, eof - line, exclude))
>  			continue;
>  
>  		it = xcalloc(1, sizeof(*it));
> 


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2/2] commit: don't check for space twice when looking for header
  2017-02-27 22:27   ` Jakub Narębski
@ 2017-02-27 22:54     ` René Scharfe
  0 siblings, 0 replies; 9+ messages in thread
From: René Scharfe @ 2017-02-27 22:54 UTC (permalink / raw)
  To: Jakub Narębski, Git List; +Cc: Junio C Hamano

Am 27.02.2017 um 23:27 schrieb Jakub Narębski:
> W dniu 25.02.2017 o 20:27, René Scharfe pisze:
>> Both standard_header_field() and excluded_header_field() check if
>> there's a space after the buffer that's handed to them.  We already
>> check in the caller if that space is present.  Don't bother calling
>> the functions if it's missing, as they are guaranteed to return 0 in
>> that case, and remove the now redundant checks from them.
>>
>> Signed-off-by: Rene Scharfe <l.s.r@web.de>
>> ---
>>  commit.c | 18 ++++++++----------
>>  1 file changed, 8 insertions(+), 10 deletions(-)
>>
>> diff --git a/commit.c b/commit.c
>> index 173c6d3818..fab8269731 100644
>> --- a/commit.c
>> +++ b/commit.c
>> @@ -1308,11 +1308,11 @@ void for_each_mergetag(each_mergetag_fn fn, struct commit *commit, void *data)
>>
>>  static inline int standard_header_field(const char *field, size_t len)
>>  {
>> -	return ((len == 4 && !memcmp(field, "tree ", 5)) ||
>> -		(len == 6 && !memcmp(field, "parent ", 7)) ||
>> -		(len == 6 && !memcmp(field, "author ", 7)) ||
>> -		(len == 9 && !memcmp(field, "committer ", 10)) ||
>> -		(len == 8 && !memcmp(field, "encoding ", 9)));
>> +	return ((len == 4 && !memcmp(field, "tree", 4)) ||
>> +		(len == 6 && !memcmp(field, "parent", 6)) ||
>> +		(len == 6 && !memcmp(field, "author", 6)) ||
>> +		(len == 9 && !memcmp(field, "committer", 9)) ||
>> +		(len == 8 && !memcmp(field, "encoding", 8)));
>
> I agree (for what it is worth from me) with the rest of changes,
> but I think current code is better self-documenting for this
> function.

Having a function that is given a buffer/length pair and accessing the 
byte after it raises questions, though. :)

Nicer than keeping the space would be to use excluded_header_field() for 
standard headers as well as a next step, I think -- but that would be a 
bit slower.

>
>>  }
>>
>>  static int excluded_header_field(const char *field, size_t len, const char **exclude)
>> @@ -1322,8 +1322,7 @@ static int excluded_header_field(const char *field, size_t len, const char **exc
>>
>>  	while (*exclude) {
>>  		size_t xlen = strlen(*exclude);
>> -		if (len == xlen &&
>> -		    !memcmp(field, *exclude, xlen) && field[xlen] == ' ')
>> +		if (len == xlen && !memcmp(field, *exclude, xlen))
>>  			return 1;
>>  		exclude++;
>>  	}
>> @@ -1357,9 +1356,8 @@ static struct commit_extra_header *read_commit_extra_header_lines(
>>  		eof = memchr(line, ' ', next - line);
>>  		if (!eof)
>>  			eof = next;
>> -
>> -		if (standard_header_field(line, eof - line) ||
>> -		    excluded_header_field(line, eof - line, exclude))
>> +		else if (standard_header_field(line, eof - line) ||
>> +			 excluded_header_field(line, eof - line, exclude))
>>  			continue;
>>
>>  		it = xcalloc(1, sizeof(*it));
>>
>

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2017-02-27 22:57 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-02-25 19:21 [PATCH 1/2] commit: be more precise when searching for headers René Scharfe
2017-02-25 19:27 ` [PATCH 2/2] commit: don't check for space twice when looking for header René Scharfe
2017-02-25 20:15   ` Jeff King
2017-02-25 21:39     ` René Scharfe
2017-02-25 21:51       ` Jeff King
2017-02-27 22:27   ` Jakub Narębski
2017-02-27 22:54     ` René Scharfe
2017-02-25 20:12 ` [PATCH 1/2] commit: be more precise when searching for headers Jeff King
2017-02-27 19:18 ` Junio C Hamano

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).