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

WANG Chao chaowang at redhat.com
Mon Aug 26 05:33:49 PDT 2013


On 08/26/13 at 02:22pm, Zbigniew Jędrzejewski-Szmek wrote:
> 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.

We can introudce strlen(s) with memcmp in startswith. But never mind if
the change is not necessary...
> 
> > > 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.
I was confused actually :(
> 
> > 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.

Understood. My intention at the first place was to put endswith code in
the same place with startswith and improve performance if possible. I
think it's fine to me to leave endswith still.

Thanks
WANG Chao


More information about the systemd-devel mailing list