[systemd-devel] [PATCH] inline static endswith()

Zbigniew Jędrzejewski-Szmek zbyszek at in.waw.pl
Mon Aug 26 05:22:37 PDT 2013


On Mon, Aug 26, 2013 at 01:15:12PM +0800, WANG Chao wrote:
> > > +static inline char *endswith(const char *s, const char *postfix) {
> > > +        size_t sl = strlen(s);
> > > +        size_t pl = strlen(postfix);
> > > +
> > > +        if (sl > pl && strncmp(s + sl -pl, postfix, pl) == 0)
> > > +                return (char *) s + sl -pl;
> > > +        return NULL;
> > > +}
> > Hm, you're replacing a memcmp with strncmp. memcmp is actually faster,
> > because strncmp does check for '\0', which memcmp doesn't have to do,
> > and can thus e.g. compare from the end, which is faster on some
> > architectures.
> 
> So if I use memcmp instead of strncmp in this patch, it'd make sense to
> you?
Yes, that'd be better, but see below.

> How about the new startswith, memcmp can be used there as well.
I don't think so. There is not strlen(s) in startswith.

> > Also, endswith is not that trivial — three function calls and
> > some arithmetic, and it's used in many places. So in the end,
> > I don't think that this patch will *increase* our footprint.
s/*increase*/*decrease*/, sorry.

> Are you suggesting that it's better leave endswith as it is for now?
Yeah, it's just more complicated, enough to not be worth inlining.
The compiler can do it with -flto if it thinks it worth it.

Zbyszek


More information about the systemd-devel mailing list