Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-1270

BSON library uses strlen to get the length of std::string objects

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 1.5.6
    • Affects Version/s: 1.5.3
    • Component/s: Internal Client
    • None
    • Environment:
      All

      std::string objects have known lengths. In the current implementation of the BSON library, std::string objects used as keys or values are converted to const char*'s with std::string::c_str(), and then passed to the const char* overload:

      /** add a subobject as a member */
      BSONObjBuilder& append(const string& fieldName , BSONObj subObj) {
      return append( fieldName.c_str() , subObj );
      }

      That eventually makes its way to BufBuilder::append, where it calls strlen, unnecessarily recomputing the length:

      void append(const char *str) {
      append((void*) str, strlen(str)+1);
      }

      Given that std::string objects have known lengths, the call to strlen is a complete waste of time.

            Assignee:
            alerner Alberto Lerner
            Reporter:
            andrew.morrow@mongodb.com Andrew Morrow (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: