Re: [PATCH v2 3/6] match_basename: use strncmp instead of strcmp
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Mar 10, 2013, 07:34 UTC
- Message-ID
- <7vy5dvd7yq.fsf@alter.siamese.dyndns.org>
- In-Reply-To
- <1362896070-17456-4-git-send-email-pclouds@gmail.com>
Nguyễn Thái Ngọc Duy <pclouds@gmail.com> writes:
> strncmp is provided length information which could be taken advantage > by the underlying implementation.
I may be missing something fundamental, but I somehow find the above does not make any sense.
strcmp(a, b) has to pay attention to NUL in these strings and stop comparison. strncmp(a, b, n) not only has to pay the same attention to NUL in the strings, but also needs to stop comparing at n bytes.
In what situation can the latter take advantage of that extra thing that it needs to keep track of and operate faster, when n is the length of shorter of the two strings?
Show 12 quoted lines
> diff --git a/dir.c b/dir.c
> index 9960a37..46b24db 100644
> --- a/dir.c
> +++ b/dir.c
> @@ -610,12 +610,14 @@ int match_basename(const char *basename, int basenamelen,
> int flags)
> {
> if (prefix == patternlen) {
> - if (!strcmp_icase(pattern, basename))
> + if (patternlen == basenamelen &&
> + !strncmp_icase(pattern, basename, patternlen))
> return 1;What happens if you replace this with
if (patternlen == baselen &&
!strcmp_icase(pattern, basename, patternlen))and drop the other hunk and run the benchmark again?
Show 10 quoted lines
> } else if (flags & EXC_FLAG_ENDSWITH) {
> if (patternlen - 1 <= basenamelen &&
> - !strcmp_icase(pattern + 1,
> - basename + basenamelen - patternlen + 1))
> + !strncmp_icase(pattern + 1,
> + basename + basenamelen - patternlen + 1,
> + patternlen - 1))
> return 1;
> } else {
> if (fnmatch_icase(pattern, basename, 0) == 0)