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

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

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 3.2.0-rc0
    • Affects Version/s: None
    • Component/s: BSON

      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.

            Assignee:
            isabella.siu@mongodb.com Isabella Siu (Inactive)
            Reporter:
            backlog-cxx-driver [DO NOT USE] Backlog - CXX Driver Team
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: