summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorEd Maste <emaste@FreeBSD.org>2020-11-19 00:02:12 +0000
committerEd Maste <emaste@FreeBSD.org>2020-11-19 00:02:12 +0000
commit7dbcd06e63101d51e6a777f7315cfde794411e53 (patch)
tree3d61133a9e8857756df782388b2c583dddfd9faf /lib
parent4874ddfd373c582d9cd7fe915d2259295d75d1e5 (diff)
downloadsrc-test2-7dbcd06e63101d51e6a777f7315cfde794411e53.tar.gz
src-test2-7dbcd06e63101d51e6a777f7315cfde794411e53.zip
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
Notes
Notes: svn path=/head/; revision=367822
Diffstat (limited to 'lib')
-rw-r--r--lib/libc/string/memmem.c4
1 files changed, 2 insertions, 2 deletions
diff --git a/lib/libc/string/memmem.c b/lib/libc/string/memmem.c
index 27863b9db623..9e7bf94b1464 100644
--- a/lib/libc/string/memmem.c
+++ b/lib/libc/string/memmem.c
@@ -153,8 +153,8 @@ twoway_memmem(const unsigned char *h, const unsigned char *z,
if (BITOP(byteset, h[l - 1], &)) {
k = l - shift[h[l - 1]];
if (k) {
- if (mem0 && mem && k < p)
- k = l - p;
+ if (k < mem)
+ k = mem;
h += k;
mem = 0;
continue;