threads / patch / 41525

patchstrbuf_write: omit system call when length is zero

Subject: [PATCH] strbuf_write: omit system call when length is zero

## tl;dr

5 messages between Feb 25, 2016 and Feb 26, 2016. Diffs are folded; open one to read it.

replies: 4people: 3as markdown or json

Stefan Beller· Feb 25, 2016, 22:34 UTC · lore

In case the length of the buffer is zero, we do not need to call the fwrite system call as a performance improvement.

Signed-off-by: Stefan Beller <sbeller@google.com>
---
 This applies on top of v17 for origin/sb/submodule-parallel-update.
 
 In case there are other reasons for origin/sb/submodule-parallel-update
 to need a reroll I'll squash it in. But as this is a pure performance
 optimization in a case we are not running into with that series and that
 series is clashing with Davids refs backend series, I figure we may not
 want to have a reroll for this fix alone.
 
 Thanks,
 Stefan
  
 strbuf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Show changes to strbuf.c +1 −1
diff --git a/strbuf.c b/strbuf.c
index 71345cd..5f6da82 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -397,7 +397,7 @@ ssize_t strbuf_read_once(struct strbuf *sb, int fd, size_t hint)
 
 ssize_t strbuf_write(struct strbuf *sb, FILE *f)
 {
-	return fwrite(sb->buf, 1, sb->len, f);
+	return sb->len ? fwrite(sb->buf, 1, sb->len, f) : 0;
 }
 
 
-- 
2.7.2.374.ga5f0819.dirty
Junio C Hamano· Feb 25, 2016, 23:04 UTC · re: Stefan Beller · lore

Re: [PATCH] strbuf_write: omit system call when length is zero

Stefan Beller <sbeller@google.com> writes:
Show 13 quoted lines
> In case the length of the buffer is zero, we do not need to call the
> fwrite system call as a performance improvement.
>
> Signed-off-by: Stefan Beller <sbeller@google.com>
> ---
>
>  This applies on top of v17 for origin/sb/submodule-parallel-update.
>  
>  In case there are other reasons for origin/sb/submodule-parallel-update
>  to need a reroll I'll squash it in. But as this is a pure performance
>  optimization in a case we are not running into with that series and that
>  series is clashing with Davids refs backend series, I figure we may not
>  want to have a reroll for this fix alone.

Yeah, I tend to agree that this change by itself is probably not worth much.

While I agree with the idea of passing the output from children thru with as little modification as possible, I think that goal does not have to be incompatible with the idea you and Jonathan had to ensure that the output is safe in the presence of incomplete last line.

I think the output made from pp_collect_finished() for "other finished processes" is known to have the very last part from finished child, so it would be easy to make sure by calling strbuf_complete_line() that pp->children[i].err is terminated.

I haven't thought it through how the output from CP_WORKING process in pp_output() should be kept track of, though.

It might be just a matter of adding a bit "incomplete_last_line" to the parallel_process.children struct and maintain it every time pp_output() writes something out (we cannot add an extra newline in pp_output() because we don't know if the process will have further output, but we can keep track of the fact that the last output did not have the final newline). In pp_collect_finished(), we can use the bit and the contents of pp->children[i].err to decide if we need to call strbuf_complete_line() if we did so.

Or something like that ;-)
Duy Nguyen· Feb 26, 2016, 00:47 UTC · re: Stefan Beller · lore

Re: [PATCH] strbuf_write: omit system call when length is zero

On Fri, Feb 26, 2016 at 5:34 AM, Stefan Beller <sbeller@google.com> wrote:
> In case the length of the buffer is zero, we do not need to call the
> fwrite system call as a performance improvement.

fwrite is a libc call, not system call. Are you sure it always calls write() (assuming buffering is off)?

Show 39 quoted lines
>
> Signed-off-by: Stefan Beller <sbeller@google.com>
> ---
>
>  This applies on top of v17 for origin/sb/submodule-parallel-update.
>
>  In case there are other reasons for origin/sb/submodule-parallel-update
>  to need a reroll I'll squash it in. But as this is a pure performance
>  optimization in a case we are not running into with that series and that
>  series is clashing with Davids refs backend series, I figure we may not
>  want to have a reroll for this fix alone.
>
>  Thanks,
>  Stefan
>
>
>  strbuf.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/strbuf.c b/strbuf.c
> index 71345cd..5f6da82 100644
> --- a/strbuf.c
> +++ b/strbuf.c
> @@ -397,7 +397,7 @@ ssize_t strbuf_read_once(struct strbuf *sb, int fd, size_t hint)
>
>  ssize_t strbuf_write(struct strbuf *sb, FILE *f)
>  {
> -       return fwrite(sb->buf, 1, sb->len, f);
> +       return sb->len ? fwrite(sb->buf, 1, sb->len, f) : 0;
>  }
>
>
> --
> 2.7.2.374.ga5f0819.dirty
>
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
-- 
Duy
Junio C Hamano· Feb 26, 2016, 01:40 UTC · re: Duy Nguyen · lore

Re: [PATCH] strbuf_write: omit system call when length is zero

Duy Nguyen <pclouds@gmail.com> writes:
Show 6 quoted lines
> On Fri, Feb 26, 2016 at 5:34 AM, Stefan Beller <sbeller@google.com> wrote:
>> In case the length of the buffer is zero, we do not need to call the
>> fwrite system call as a performance improvement.
>
> fwrite is a libc call, not system call. Are you sure it always calls
> write() (assuming buffering is off)?

I do not think so, but I suspect that the patch misstates its rationale (I said I get uncomfortable every time I see a function that takes size and nelem separately used by a caller that can potentially pass nleme=0, when I wondered it it is OK that no caller of this funtion checks its return value).

Show 40 quoted lines
>
>>
>> Signed-off-by: Stefan Beller <sbeller@google.com>
>> ---
>>
>>  This applies on top of v17 for origin/sb/submodule-parallel-update.
>>
>>  In case there are other reasons for origin/sb/submodule-parallel-update
>>  to need a reroll I'll squash it in. But as this is a pure performance
>>  optimization in a case we are not running into with that series and that
>>  series is clashing with Davids refs backend series, I figure we may not
>>  want to have a reroll for this fix alone.
>>
>>  Thanks,
>>  Stefan
>>
>>
>>  strbuf.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/strbuf.c b/strbuf.c
>> index 71345cd..5f6da82 100644
>> --- a/strbuf.c
>> +++ b/strbuf.c
>> @@ -397,7 +397,7 @@ ssize_t strbuf_read_once(struct strbuf *sb, int fd, size_t hint)
>>
>>  ssize_t strbuf_write(struct strbuf *sb, FILE *f)
>>  {
>> -       return fwrite(sb->buf, 1, sb->len, f);
>> +       return sb->len ? fwrite(sb->buf, 1, sb->len, f) : 0;
>>  }
>>
>>
>> --
>> 2.7.2.374.ga5f0819.dirty
>>
>> --
>> To unsubscribe from this list: send the line "unsubscribe git" in
>> the body of a message to majordomo@vger.kernel.org
>> More majordomo info at  http://vger.kernel.org/majordomo-info.html
Stefan Beller· Feb 26, 2016, 17:09 UTC · re: Junio C Hamano · lore

Re: [PATCH] strbuf_write: omit system call when length is zero

On Thu, Feb 25, 2016 at 5:40 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 15 quoted lines
> Duy Nguyen <pclouds@gmail.com> writes:
>
>> On Fri, Feb 26, 2016 at 5:34 AM, Stefan Beller <sbeller@google.com> wrote:
>>> In case the length of the buffer is zero, we do not need to call the
>>> fwrite system call as a performance improvement.
>>
>> fwrite is a libc call, not system call. Are you sure it always calls
>> write() (assuming buffering is off)?
>
> I do not think so, but I suspect that the patch misstates its
> rationale (I said I get uncomfortable every time I see a function
> that takes size and nelem separately used by a caller that can
> potentially pass nleme=0, when I wondered it it is OK that no caller
> of this funtion checks its return value).
>

This is exactly what happened. I assumed fwrite would be a system call eventually.

← back to recent threads