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

Zbigniew Jędrzejewski-Szmek zbyszek at in.waw.pl
Fri Jul 12 09:38:35 PDT 2013


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);
}

and making sure that the generated assembly properly replace the
strlen with a constant, and then using startswith instead of
hasprefix, should work. Both gcc and clang optimize strlen("string")
away, even with -O0, so it *should* work.

Zbyszek


More information about the systemd-devel mailing list