Replace strstr with strncmp looking for path separator
Using strstr in a loop is O(n^2) with n being the length of the path,
whereas using strncmp is O(n*m) with n being the length of the path
and m is the length of the separator (which is always 1).
Effectively this turns the quadratic behavior linear.
Change-Id: If60d02c33eeca92ca2ff52e0df97d0bb80fe57e0
Reviewed-on: https://dart-review.googlesource.com/62144
Reviewed-by: Vyacheslav Egorov <vegorov@google.com>
Commit-Queue: Jens Johansen <jensj@google.com>
diff --git a/runtime/bin/dfe.cc b/runtime/bin/dfe.cc
index e9a4698..5f1721d 100644
--- a/runtime/bin/dfe.cc
+++ b/runtime/bin/dfe.cc
@@ -61,16 +61,18 @@
static char* GetDirectoryPrefixFromExeName() {
const char* name = Platform::GetExecutableName();
const char* sep = File::PathSeparator();
+ const intptr_t sep_length = strlen(sep);
+
for (intptr_t i = strlen(name) - 1; i >= 0; --i) {
const char* str = name + i;
- if (strstr(str, sep) == str
+ if (strncmp(str, sep, sep_length) == 0
#if defined(HOST_OS_WINDOWS)
// TODO(aam): GetExecutableName doesn't work reliably on Windows,
// the code below is a workaround for that (we would be using
// just single Platform::Separator instead of both slashes if it did).
|| *str == '/'
#endif
- ) {
+ ) {
return Utils::StrNDup(name, i + 1);
}
}