Uploaded image for project: 'C++ Driver'
  1. C++ Driver
  2. CXX-992

bsoncxx::document::view::find() should be rewritten to avoid string allocation

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 3.2.0-rc0
    • Component/s: BSON
    • Labels:

      Description

      The current version of bsoncxx::document::view::find() is implemented in terms of bson_iter_init_find(). This implementation requires a call to string_view::to_string() (which performs an allocation), in order to conform to the bson_iter_t API (which expects null-terminated strings). See snippet below:

         168	view::iterator view::find(stdx::string_view key) const {
         169	    bson_t b;
         170	    bson_iter_t iter;
         171
         172	    if (!bson_init_static(&b, _data, _length)) {
         173	        return end();
         174	    }
         175
         176	    if (bson_iter_init_find(&iter, &b, key.to_string().data())) {
         177	        return iterator(element(iter.raw, iter.len, iter.off));
         178	    }
         179
         180	    return end();
         181	}
      

      This method could be rewritten to avoid the allocation by manually iterating the bson_iter_t to find the desired element, instead of relying on bson_iter_init_find().

      Other calls to string_view::to_string() could possibly be audited to determine whether similar performance improvements could be gained elsewhere.

        Attachments

          Issue Links

            Activity

              People

              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: