Re: PATCH] Improve performance of memmem
Corinna Vinschen <[email protected]>
| Newsgroups | gmane.comp.lib.newlib |
|---|---|
| Message-ID | <[email protected]> |
On Dec 18 14:32, Eric Blake wrote: > On 12/18/18 2:23 PM, Wilco Dijkstra wrote: > > > ...this is the naive quadratic implementation. But it does certainly > > > qualify as the algorithm to use for someone wanting a compilation as > > > small as possible, and who doesn't care about speed (most likely because > > > their needles are unlikely to be large enough to notice the effects of > > > quadratic slowdowns). > > > > However the interesting thing is that this naive implementation is actually > > faster than the existing Two-Way implementation on all typical inputs: it is > > > 50% faster for any needle up to 32 bytes on any haystack size. So it > > would have been better if memmem used the naive implementation for > > small needles. The quadratic behaviour only becomes problematic when > > needles are like 4KBytes. > > Indeed - when I first submitted a non-quadratic strstr/memmem, I was so > caught up on benchmarking the large outlier cases for linear behavior that I > forgot to benchmark the overhead of the more common short cases. Algorithmic > complexity doesn't overcome sheer overhead of setup, so I have been glad to > watch over time as various consumers of my original Two-Way code have added > heuristics to avoid it for short needles. > > > > > > + memset (shift, 0, sizeof (shift)); > > > > > > I don't know if we have a preference for 'sizeof shift' rather than > > > 'sizeof (shift)'. > > > > Of the 1677 occurences of sizeof in newlib, 984 are "sizeof (x)", 539 are > > "sizeof(x)" (missing space) and 74 are sizeof x, so I guess parenthesis is > > the preferred spelling. > > There's also the fact that: > > sizeof (int) > > has to use parenthesis; grepping for the use of () is tricky if you don't > filter out the uses that are for types rather than for expressions (but I > _do_ prefer sizeof expression over sizeof (type), which is why I prefer > avoiding the () to make it easier to use grep to tell the two apart). Not a fixed rule since, afaik, BSD code always uses parens in sizeof expressions. Apart from that I think it's a good idea to use sizeof (type) vs. sizeof var but again, there's no strong rule. Corinna -- Corinna Vinschen Cygwin Maintainer Red Hat
signature.asc
(application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEoVYPmneWZnwT6kwF9TYGna5ET6AFAlwaGRIACgkQ9TYGna5E T6C4qxAAhmMnLXuQ6ka5j4fvOE7bPNXdACJ4Z+HbAmHxpEw/j0RoPFWckj7F95SD u/sH840zYb114HILAEag2LVzDeDchxnFUz4IAlhPGdRhqjv4KjmopoAu3RwMxFG7 UnB3gV4onb8D9efPg9vgmY0gJVKK/jwtJgJdncXcdNWDMO1UbPLjKDFWTt9nIN7h E1QgeSJL3KUZtBAK30DJhAillQUF5SeKcZv7chCSgXvsuyv0+24OOu3tooBgr2TA VlOYgn+nJPwMJ+nBYZE+j63+JdeQXGAnntINnOQTzsXItWhKPEn3OnCXji7Z1FCN ZyILbmesa/bM4eC2FaOJwn/ZRQ+xzQSvxpdY+I8EIw/xL7QqLshKhGHlO0yGcqGG yVLbtpN0KdJJxJY7XBQ5kapfVgEHtsQ8LqENQiKKvzkLxsANlbZuJp/s/IfPsXSP ju6fJSYmV/hSojOM9objle2zG0eIu/2yVTIpmMawdEXE+dDsJ1CjH7aoE1ikkP1m svIH5xeUnU+YFxVjv1nUIqEyLO+/e1xyuiJ9ZBhDHD0UidyZr9IOtotpymp7m8G7 r/QPIuOJ2vPuVmtABnmhFsbNxFVulv6m9rzYWWqdrPIKroi4zNXRHcXHxYhnki5Q k5wG1jp6uMvx3l+W84hrVUPGxnrSQ/ISG2ZRtCFGqqgz1+glv4U= =4a8O -----END PGP SIGNATURE-----