-
Type: Improvement
-
Resolution: Fixed
-
Priority: Critical - P2
-
Affects Version/s: None
-
Component/s: None
-
Storage Engines
-
5
-
2024-02-06 tapioooooooooooooca
WT_STRING_MATCH is defined as:
#define WT_STRING_MATCH(str, bytes, len) __wt_string_match(str, bytes, len) static inline bool __wt_string_match(const char *str, const char *bytes, size_t len) { return (len > 0 && (strlen(str) == len) && (strncmp(str, bytes, len) == 0)); }
Consider changing the function to:
static inline bool __wt_string_match(const char *str, const char *bytes, size_t len) { return (len > 0 && strncmp(str, bytes, len) == 0 && str[len] == '\0'); }
That should be equivalent and avoids walking the str string twice.
Also, why do we need the len > 0 check? It will only make a difference if the length of string is 0, like this:
WT_STRING_MATCH("", bytes, 0)
I'd argue that the current code gives the wrong answer (false - should be true). Usually the first arg is a literal, and never "", so the right/wrong-ness is perhaps moot, but if it gives the wrong answer and is slower, well then...
So the final proposed better solution is:
static inline bool __wt_string_match(const char *str, const char *bytes, size_t len) { return (strncmp(str, bytes, len) == 0 && str[len] == '\0'); }