Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize performance of ndpi_strnstr() and possible bugfix #2494

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions example/ndpiReader.c
Original file line number Diff line number Diff line change
Expand Up @@ -5782,6 +5782,13 @@ void strnstrUnitTest(void) {

/* Test 13 */
assert(ndpi_strnstr("abcdef", "abc", 2) == NULL);

/* Test 14: zero length */
assert(strcmp(ndpi_strnstr("", "", 0), "") == 0);
assert(strcmp(ndpi_strnstr("string", "", 0), "string") == 0);
assert(ndpi_strnstr("", "str", 0) == NULL);
assert(ndpi_strnstr("string", "str", 0) == NULL);
assert(ndpi_strnstr("str", "string", 0) == NULL);
}

/* *********************************************** */
Expand Down
27 changes: 14 additions & 13 deletions src/lib/ndpi_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9737,47 +9737,48 @@ void ndpi_dump_risks_score(FILE *risk_out) {

char *ndpi_strnstr(const char *haystack, const char *needle, size_t len)
{
if (!haystack || !needle || len == 0)
if (!haystack || !needle)
{
return NULL;
}

size_t needle_len = strlen(needle);
size_t hs_real_len = strnlen(haystack, len);
const size_t needle_len = strlen(needle);

if (needle_len == 0)
{
return (char *)haystack;
}

if (needle_len > hs_real_len)
{
return NULL;
}
const size_t hs_real_len = strnlen(haystack, len);

if (needle_len == 1)
{
return (char *)memchr(haystack, *needle, hs_real_len);
}

const char *current = haystack;
const char *haystack_end = haystack + hs_real_len;
if (needle_len > hs_real_len)
{
return NULL;
}

while (current <= haystack_end - needle_len)
const char *const end_of_search = haystack + hs_real_len - needle_len + 1;

const char *current = haystack;
while (current < end_of_search)
{
current = (const char *)memchr(current, *needle, haystack_end - current);
current = (const char *)memchr(current, *needle, end_of_search - current);

if (!current)
{
return NULL;
}

if ((current + needle_len <= haystack_end) && memcmp(current, needle, needle_len) == 0)
if (memcmp(current, needle, needle_len) == 0)
{
return (char *)current;
}

current++;
++current;
}

return NULL;
Expand Down
43 changes: 43 additions & 0 deletions tests/performance/strnstr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,48 @@ char *ndpi_strnstr_opt(const char *haystack, const char *needle, size_t len) {
return NULL;
}

char* ndpi_strnstr_opt2(const char* haystack, const char* needle, size_t length_limit) {
if (!haystack || !needle) {
return nullptr;
}

const size_t needle_len = strlen(needle);

if (needle_len == 0) {
return (char*) haystack;
}

const size_t hs_real_len = strnlen(haystack, length_limit);

if (needle_len == 1) {
return (char *)memchr(haystack, *needle, hs_real_len);
}

if (needle_len > hs_real_len) {
return nullptr;
}

const char*const end_of_search = haystack + hs_real_len - needle_len + 1;

const char *current = haystack;
while (current < end_of_search) {

current = (const char*) memchr(current, *needle, end_of_search - current);

if (!current) {
return nullptr;
}

if (memcmp(current, needle, needle_len) == 0) {
return (char*) current;
}

++current;
}

return nullptr;
}

std::string random_string(size_t length, std::mt19937 &gen) {
std::uniform_int_distribution<> dis(0, 255);
std::string str(length, 0);
Expand Down Expand Up @@ -133,6 +175,7 @@ int main() {
strnstr_impls = {
{"ndpi_strnstr", ndpi_strnstr},
{"ndpi_strnstr_opt", ndpi_strnstr_opt},
{"ndpi_strnstr_opt2", ndpi_strnstr_opt2},
};

const int iterations = 100000;
Expand Down
Loading