-
Notifications
You must be signed in to change notification settings - Fork 918
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
Conversation
Actually,
can be removed too because, in that case, the loop won't perform any iterations and the function will return NULL anyways. But I'm nut sure if this will increase or decrease performance in your case. |
Could you add a unit test about that case in |
@IvanNardi, I've added unit test cases for zero length in |
@IvanNardi, what do you think about removing the checks |
Actually there are some cases where mgcp.c
ethereum.c
xiaomi.c
tivoconnect.c
|
Thanks! In that case, I think I should add that check back. What do you think about the second check |
I dunno, I guess you could keep it. Usually the compiler manages skipping redundant checks. |
Ok. I've returned the removed check By the way, the compiler doesn't remove the check |
Alright, it looks fine. You can take a look at |
I took a look at |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@pasabanov, thank you for this contribution |
Please sign (check) the below before submitting the Pull Request:
#2433 #2439 #2447
Describe changes:
I made some minor changes to the current optimized substring search.
The main changes are:
len == 0
check from the firstif
statement. Now the function doesn't immediately returnNULL
iflen
is zero.I believe that the previous behavior was a bug, because if
needle_len
is zero, the function should returnhaystack
, because the empty substring does reside at the start of the empty string.hs_real_len
computation andif (needle_len == 0)
check.(needle_len == 1)
check because it only provides a performance benefit in specific cases. If you need to frequently search for single-character substrings, you might want to consider reverting this change.haystack_end
with variableend_of_search
(the byte after the last interesting byte).Using
end_of_search
inside thememchr
function instead ofhaystack_end
eliminates the need for thecurrent + needle_len <= haystack_end
check, as it is always positive ifcurrent
is notNULL
.I also added the new function to the performance tests.
In practice
ndpi_strnstr_opt2
is slightly faster thanndpi_strnstr_opt
in most runs, though sometimesndpi_strnstr_opt
appears to be faster.