[systemd-devel] [PATCH v2] shared: rename hasprefix() to startswith(), which is functionally identical and removed

Lennart Poettering lennart at poettering.net
Fri Jul 12 09:59:13 PDT 2013


On Fri, 12.07.13 18:38, Zbigniew Jędrzejewski-Szmek (zbyszek at in.waw.pl) wrote:

> 
> On Fri, Jul 12, 2013 at 06:00:38PM +0200, Lennart Poettering wrote:
> > On Fri, 12.07.13 08:42, Shawn Landden (shawnlandden at gmail.com) wrote:
> > 
> > > as most (if not all) of the prefix strings are static, these will get
> > > forward constant propagation optimized into single memcmp() calls, which
> > > should be much better than the non-SIMD hand-rolled version.
> > 
> > They are functionally close, but not the same. I added this to the TODO
> > list because I'd like to see them merged one day, but it's not trivial
> > as your patch.
> I agree that hasprefix should go.
> 
> > startswith() is not particularly optimized, but it has one major
> > benefit: it returns a pointer to the first char after the passed prefix,
> > which is actually hugely useful and used at various places. hasprefix()
> > is more efficient for static+const prefix strings.
> > 
> > (startswith() is also nicer if prefix is potentially large. But I figure
> > this property doesn't matter and is somethign we can get rid of)
> > 
> > It should be possible to merge both calls in one macro, and that's how
> > I'd like to see this fixed.
> I think that renaming startswith() to _startswith() and 
> adding 
> 
> static inline startswith(s, prefix) {
>        if (_builtin_constant_p(prefix)) {
>              if (strncmp(s, prefix, strlen(prefix)) == 0)
> 	         return s + strlen(prefix);
>              else
>                  return NULL;
>        } else
>              return _strcmp(s, prefix);
> }

Maybe 

#define startswith(s, prefix) ({ \
        size_t _l; \
        _l = strlen(prefix); \
        strncmp(s, prefix, _l) == 0 ? s + _l : NULL; \
})

Or so? And then drop the old startswith() entirely? This stuff should be
good for static+const strings (assuming the compiler is smart enough to
replace strlen()), and probably faster than the old implementation of
startswith() for the non-static+const cases too...

Lennart

-- 
Lennart Poettering - Red Hat, Inc.


More information about the systemd-devel mailing list