Skip to content

Commit 122d67f

Browse files
committed
optimize two-way strstr and memmem 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.
1 parent 04e18b6 commit 122d67f

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

src/string/memmem.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ static char *twoway_memmem(const unsigned char *h, const unsigned char *z, const
100100
if (BITOP(byteset, h[l-1], &)) {
101101
k = l-shift[h[l-1]];
102102
if (k) {
103-
if (mem && k < p) k = l-p;
103+
if (k < mem) k = mem;
104104
h += k;
105105
mem = 0;
106106
continue;

src/string/strstr.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ static char *twoway_strstr(const unsigned char *h, const unsigned char *n)
109109
if (BITOP(byteset, h[l-1], &)) {
110110
k = l-shift[h[l-1]];
111111
if (k) {
112-
if (mem && k < p) k = l-p;
112+
if (k < mem) k = mem;
113113
h += k;
114114
mem = 0;
115115
continue;

0 commit comments

Comments
 (0)