Uploaded image for project: 'WiredTiger'
  1. WiredTiger
  2. WT-12245

WT_STRING_MATCH perf improvement

    • 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');
      }
      

        1. Screenshot 2024-01-24 at 11.09.05 am.png
          241 kB
          Chenhao Qu
        2. Screenshot 2024-01-24 at 3.01.36 pm.png
          250 kB
          Chenhao Qu

            Assignee:
            chenhao.qu@mongodb.com Chenhao Qu
            Reporter:
            donald.anderson@mongodb.com Donald Anderson
            Votes:
            0 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated:
              Resolved: