[systemd-devel] [PATCH] Replace hasprefix() with startswith()

WANG Chao chaowang at redhat.com
Wed Aug 21 10:24:53 PDT 2013


On 08/21/13 at 06:58pm, Kay Sievers wrote:
> On Wed, Aug 21, 2013 at 6:53 PM, WANG Chao <chaowang at redhat.com> wrote:
> > On 08/21/13 at 12:05pm, Kay Sievers wrote:
> >> On Wed, Aug 21, 2013 at 8:36 AM, WANG Chao <chaowang at redhat.com> wrote:
> >>
> >> > --- a/TODO
> >> > +++ b/TODO
> >> > @@ -96,8 +96,6 @@ Features:
> >> >
> >> > -* do we really need both hasprefix() and startswith()?
> >>
> >> It needs a little bit more:
> >>   - the open-coded startswith seems really slow, it should not get more users
> >>   - hasprefix can no longer be compile-time optimized, which is not good
> >
> > Apart from the overflow issue hasprefix has, may I ask why hasprefix
> > can't be complile-time optimized now? What changed?
> 
> Your patch replaced hasprefix with startswith. hasprefix was just a
> define, it operated on static strings with static length, which the
> compiler can optimize away. startswith is an open-coded function with
> a counter, which has to be called and cannot really be optimized-away
> by the compiler.

Thanks for clarify, I'll send two patches, one for macro startswith,
another for the replacement.

Thanks

> 
> >> We might want something like this:
> >>   http://lists.freedesktop.org/archives/systemd-devel/2013-July/011860.html
> >
> > It looks like Lennart has already figured out an optimized startswith:
> >
> > #define startswith(s, prefix) ({ \
> >         size_t _l; \
> >         _l = strlen(prefix); \
> >         strncmp(s, prefix, _l) == 0 ? s + _l : NULL; \
> > })
> >
> > IMHO, using strncmp() is fast enough. What do you think?
> 
> strncmp sounds fine instead of the open-coded loop in hasprefix, yes.
> 
> Kay


More information about the systemd-devel mailing list