]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/commit
libc: optimize memmem two-way bad character shift
authoremaste <emaste@FreeBSD.org>
Thu, 19 Nov 2020 00:02:12 +0000 (00:02 +0000)
committeremaste <emaste@FreeBSD.org>
Thu, 19 Nov 2020 00:02:12 +0000 (00:02 +0000)
commit213257c45860d387a4c71d3395fc060d07667337
tree3d61133a9e8857756df782388b2c583dddfd9faf
parentce62a77e6e943eab13cd3ae1b3dd9c0ab4a3c567
libc: optimize memmem two-way bad character shift

first, the condition (mem && k < p) is redundant, because mem being
nonzero implies the needle is periodic with period exactly p, in which
case any byte that appears in the needle must appear in the last p
bytes of the needle, bounding the shift (k) by p.

second, the whole point of replacing the shift k by mem (=l-p) is to
prevent shifting by less than mem when discarding the memory on shift,
in which case linear time could not be guaranteed. but as written, the
check also replaced shifts greater than mem by mem, reducing the
benefit of the shift. there is no possible benefit to this reduction of
the shift; since mem is being cleared, the full shift is valid and
more optimal. so only replace the shift by mem when it would be less
than mem.

musl commits:
8f5a820d147da36bcdbddd201b35d293699dacd8
122d67f846cb0be2c9e1c3880db9eb9545bbe38c

Obtained from: musl
MFC after: 2 weeks
lib/libc/string/memmem.c