Re: [PATCH] Optimize prefixcmp()
On Sun, Dec 30, 2007 at 01:55:57PM +0000, Pierre Habouzit wrote:
Show 40 quoted lines
> On Sun, Dec 30, 2007 at 01:02:28PM +0000, Marco Costalba wrote:
> > Subject: [PATCH] Certain codepaths (notably "git log --pretty=format...") use
> >
> > prefixcmp() extensively, with very short prefixes. In those cases,
> > calling strlen() is a wasteful operation, so avoid it.
> >
> > Initial patch by Johannes Schindelin.
> >
> > Signed-off-by: Marco Costalba <mcostalba@gmail.com>
> > ---
> > git-compat-util.h | 11 ++++++++++-
> > 1 files changed, 10 insertions(+), 1 deletions(-)
> >
> > diff --git a/git-compat-util.h b/git-compat-util.h
> > index 79eb10e..843a8f5 100644
> > --- a/git-compat-util.h
> > +++ b/git-compat-util.h
> > @@ -398,7 +398,16 @@ static inline int sane_case(int x, int high)
> >
> > static inline int prefixcmp(const char *str, const char *prefix)
> > {
> > - return strncmp(str, prefix, strlen(prefix));
> > + do {
> > + if (*str != *prefix)
> > + return *(unsigned const char *)prefix - *(unsigned const char *)str;
> > +
> > + if (!*(++prefix))
> > + return 0;
> > +
> > + str++;
> > +
> > + } while (1);
>
> This code doesn't work if prefix is "". You want something like:
>
> for (; *prefix; prefix++, str++) {
> if (*str != *prefix)
> return *(unsigned const char *)prefix - *(unsigned const char *)str;
> }
> return 0; Which happens to be basically the same than what Dscho wrote, though I
suppose the compiler can compile that more efficiently than his code.
--
·O· Pierre Habouzit
··O madcoder@debian.org
OOO http://www.madism.org