All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: David Kastrup <dak@gnu.org>
Cc: git@vger.kernel.org
Subject: Re: [PATCH] blame.c: prepare_lines should not call xrealloc for every line
Date: Tue, 04 Feb 2014 14:06:01 -0800	[thread overview]
Message-ID: <xmqqmwi67cty.fsf@gitster.dls.corp.google.com> (raw)
In-Reply-To: <87zjm6v99y.fsf@fencepost.gnu.org> (David Kastrup's message of "Tue, 04 Feb 2014 22:48:57 +0100")

David Kastrup <dak@gnu.org> writes:

>> so something like
>>
>> 	for (p = buf; p < end; p++) {
>>         	p = find the end of this line
>>                 if (!p)
>>                 	break;
>> 		num++;
>> 	}
>>
>> perhaps?
>
> Would crash on incomplete last line.

Hmph, even with "if !p"?   From your other message:

+	for (p = buf;; num++) {
+		p = memchr(p, '\n', end - p);
+		if (p) {
+			p++;
+			continue;
 		}
+		break;
+	}

If you flip the if statement around that would be the same as:

+	for (p = buf;; num++) {
+		p = memchr(p, '\n', end - p);
+		if (!p)
			break;
		p++;
+	}

And with "loop action not in the control part", that would be the
same as:

	for (p = buf; ; p++) {
		p = memchr...
                if (!p)
                	break;
		num++;
	}

no?  Would this crash on incomplete last line?

Ahh, "p < end" in "for (p = buf; p < end; p++) {" is not correct;
you depend on overrunning the end of the buffer to feed length 0 to
memchr and returning NULL inside the loop.  So to be equivalent to
your version, the contination condition needs to be

	for (p = buf; p <= end; p++) {

But that last round of the loop will be no-op, so "p < end" vs "p <=
end" does not make any difference.  It is not even strictly
necessary because memchr() limits the scan to end, but it would
still be a good belt-and-suspenders defensive coding practice, I
would think.

+	for (p = buf;;) {
+		*lineno++ = p - buf;
+		p = memchr(p, '\n', end - p);
+		if (p) {
+			p++;
+			continue;
 		}
+		break;
 	}

Can we do the same transformation here?

	for (p = buf;;) {
        	*lineno++ = p - buf;
                p = memchr...
                if (!p)
                	break;
		p++;
	}

which is the same as

	for (p = buf; ; p++) {
        	*lineno++ = p - buf;
                p = memchr...
                if (!p)
                	break;
	}

and the latter has the loop control p++ at where it belongs to. The
post-condition of one iteration of the body of the loop being "p
points at the terminating LF of this line", incrementing the pointer
is how the loop control advances the pointer to the beginning of the
next line.

If we wanted to have a belt-and-suspenders safety, we need "p <=
end" here, not "p < end", because of how p is used when it is past
the last LF.  As we want to make these two loops look similar, that
means we should use "p <= end" in the previous loop as well.

This was fun ;-)

  reply	other threads:[~2014-02-04 22:06 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-04 20:06 [PATCH] blame.c: prepare_lines should not call xrealloc for every line David Kastrup
2014-02-04 20:10 ` David Kastrup
2014-02-04 20:49   ` Junio C Hamano
2014-02-04 21:00     ` Junio C Hamano
2014-02-04 21:09       ` David Kastrup
2014-02-04 22:28         ` Philip Oakley
2014-02-04 22:48           ` Philip Oakley
2014-02-04 20:24 ` Junio C Hamano
2014-02-04 20:52   ` David Kastrup
2014-02-04 21:03     ` Junio C Hamano
2014-02-04 21:11       ` David Kastrup
2014-02-04 21:41         ` Junio C Hamano
2014-02-04 21:27   ` David Kastrup
2014-02-04 21:44     ` Junio C Hamano
2014-02-04 21:48       ` David Kastrup
2014-02-04 22:06         ` Junio C Hamano [this message]
2014-02-05  8:39           ` David Kastrup
2014-02-05 20:39             ` Junio C Hamano
2014-02-06  0:34               ` David Kastrup
2014-02-06 10:29               ` David Kastrup
2014-02-05  9:22   ` David Kastrup
2014-02-05 20:34     ` Junio C Hamano
2014-02-05 23:45       ` David Kastrup
  -- strict thread matches above, loose matches on Subject: below --
2014-02-04 21:40 David Kastrup
2014-02-04 21:46 David Kastrup
2014-02-12 14:27 David Kastrup
2014-02-12 19:36 ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqmwi67cty.fsf@gitster.dls.corp.google.com \
    --to=gitster@pobox.com \
    --cc=dak@gnu.org \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.