Browse Source

Reduce strcmp() calls in hashtable lookup

Sam Lantinga 11 months ago
parent
commit
56fc4b790c
1 changed files with 6 additions and 1 deletions
  1. 6 1
      src/SDL_hashtable.c

+ 6 - 1
src/SDL_hashtable.c

@@ -294,12 +294,17 @@ Uint32 SDL_HashString(const void *key, void *data)
 
 bool SDL_KeyMatchString(const void *a, const void *b, void *data)
 {
+    const char *a_string = (const char *)a;
+    const char *b_string = (const char *)b;
+
     if (a == b) {
         return true;  // same pointer, must match.
     } else if (!a || !b) {
         return false;  // one pointer is NULL (and first test shows they aren't the same pointer), must not match.
+    } else if (a_string[0] != b_string[0]) {
+        return false;  // we know they don't match
     }
-    return (SDL_strcmp((const char *)a, (const char *)b) == 0);  // Check against actual string contents.
+    return (SDL_strcmp(a_string, b_string) == 0);  // Check against actual string contents.
 }
 
 // We assume we can fit the ID in the key directly