From: David Kastrup <dak@gnu.org>
To: Duy Nguyen <pclouds@gmail.com>
Cc: Junio C Hamano <gitster@pobox.com>,
Siddharth Goel <siddharth98391@gmail.com>,
Git Mailing List <git@vger.kernel.org>,
Eric Sunshine <sunshine@sunshineco.com>
Subject: Re: [PATCH v3] skip_prefix: rewrite so that prefix is scanned once
Date: Tue, 04 Mar 2014 01:09:39 +0100 [thread overview]
Message-ID: <877g8akenw.fsf@fencepost.gnu.org> (raw)
In-Reply-To: <CACsJy8ASBeravdk67pbOJbrFUbwg21JwYcLtSbDDMJOu9-F=yA@mail.gmail.com> (Duy Nguyen's message of "Tue, 4 Mar 2014 06:37:25 +0700")
Duy Nguyen <pclouds@gmail.com> writes:
> On Tue, Mar 4, 2014 at 5:43 AM, Junio C Hamano <gitster@pobox.com> wrote:
>> diff --git a/git-compat-util.h b/git-compat-util.h
>> index cbd86c3..68ffaef 100644
>> --- a/git-compat-util.h
>> +++ b/git-compat-util.h
>> @@ -357,8 +357,14 @@ extern int suffixcmp(const char *str, const char *suffix);
>>
>> static inline const char *skip_prefix(const char *str, const char *prefix)
>> {
>> - size_t len = strlen(prefix);
>> - return strncmp(str, prefix, len) ? NULL : str + len;
>
> Just a note. gcc does optimize strlen("abcdef") to 6, and with that
> information at compile time built-in strncmp might do better.
Indeed, most (but not all) of the calls have a constant string as
prefix. However, strncmp in each iteration checks for both *str as well
as *prefix to be different from '\0' independently (and it appears
unlikely to me that the optimizer will figure out that it's unnecessary
for either) _and_ compares them for equality so it's not likely to be
faster than the open-coded loop.
One could, however, use memcmp instead of strncmp. I'm just not sure
whether memcmp is guaranteed not to peek beyond the first mismatching
byte even if the count would allow for more. It could lead to undefined
behavior if the first mismatching byte would be the ending NUL byte of
str.
--
David Kastrup
next prev parent reply other threads:[~2014-03-04 0:09 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-03-03 3:13 [PATCH v3] skip_prefix: rewrite so that prefix is scanned once Siddharth Goel
2014-03-03 19:05 ` Junio C Hamano
2014-03-03 22:43 ` Junio C Hamano
2014-03-03 23:22 ` David Kastrup
2014-03-03 23:35 ` Junio C Hamano
2014-03-03 23:37 ` Duy Nguyen
2014-03-04 0:09 ` David Kastrup [this message]
2014-03-04 1:58 ` Duy Nguyen
2014-03-04 9:18 ` David Kastrup
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=877g8akenw.fsf@fencepost.gnu.org \
--to=dak@gnu.org \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=pclouds@gmail.com \
--cc=siddharth98391@gmail.com \
--cc=sunshine@sunshineco.com \
/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.