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

Avoid unnecessary string copies

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 8.2.0-rc0
    • Affects Version/s: None
    • Component/s: None
    • None
    • Query Execution
    • Fully Compatible
    • QE 2025-04-28
    • None
    • 0
    • None
    • None
    • None
    • None
    • None
    • None

      In SBE, there is a function sbe::value::makeNewString(StringData) to create new string values.
      As StringData can be implicitly created from an std::string, it is allowed to call the function is called with an std::string parameter as well.

      There were some places in the code that unnecessarily call this function with an std::string parameter where this isn't necessary, for example:

                  std::string str = str::stream() << value::bitcastTo<int32_t>(operandVal);
                  auto [strTag, strVal] = value::makeNewString(str);
      

      Here we are creating a StringBuilder instance to build the stringified version of the number, and then copy the StringBuilder 's contents into an std::string, which is then passed into sbe::value::makeNewString().
      We could instead use a StringData object to point into the StringBuilder 's internal buffer, and thus avoid creating the std::string altogether:

                  str::stream str;
                  str << value::bitcastTo<int32_t>(operandVal);
                  auto [strTag, strVal] = value::makeNewString(StringData(str));
      

      Similarly, objects of the Value class can also be constructed from StringData and std::string inputs alike. The Value constructor that takes an std::string input simply casts it to a StringData and delegates to the StringData parameter constructor.
      Creating a Value object from a temporary std::string input provides no benefit, and we can instead create the Value object directly from a StringData object to avoid the potentially expensive std::string creation.

            Assignee:
            jan.steemann@mongodb.com Jan Steemann
            Reporter:
            jan.steemann@mongodb.com Jan Steemann
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: