aboutsummaryrefslogtreecommitdiff
path: root/tests/performance/strnstr.cpp
diff options
context:
space:
mode:
authorPetr <30545094+pasabanov@users.noreply.github.com>2024-07-15 09:34:08 +0300
committerGitHub <noreply@github.com>2024-07-15 08:34:08 +0200
commite059daa0f16f73f27dbdb232ede037d1a43f1fee (patch)
treefd3a68af41ed75cccb92c3a424ce42dfd411efb2 /tests/performance/strnstr.cpp
parentf8e32bc75b3274daf3d9024449bbf0574436eda7 (diff)
Optimize performance of ndpi_strnstr() and possible bugfix (#2494)
Diffstat (limited to 'tests/performance/strnstr.cpp')
-rw-r--r--tests/performance/strnstr.cpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/performance/strnstr.cpp b/tests/performance/strnstr.cpp
index 7c3b3ba7d..e9b01dced 100644
--- a/tests/performance/strnstr.cpp
+++ b/tests/performance/strnstr.cpp
@@ -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);
@@ -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;