[CXX-992] bsoncxx::document::view::find() should be rewritten to avoid string allocation Created: 12/Aug/16  Updated: 02/Dec/17  Resolved: 05/Jun/17

Status: Closed
Project: C++ Driver
Component/s: BSON
Affects Version/s: None
Fix Version/s: 3.2.0-rc0

Type: Improvement Priority: Major - P3
Reporter: Backlog - CXX Driver Team Assignee: Isabella Siu (Inactive)
Resolution: Done Votes: 0
Labels: neweng
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Problem/Incident
causes CXX-1476 Element search stops prematurely due ... Closed
Related
related to CDRIVER-2414 bson_iter{_init}_find should have len... Closed

 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.



 Comments   
Comment by Andrew Morrow (Inactive) [ 02/Dec/17 ]

I believe the changes made above introduced a regression, see CXX-1476.

Comment by Githook User [ 07/Jun/17 ]

Author:

{u'username': u'iwysiu', u'name': u'Isabella Siu', u'email': u'sakurablossom@blueblueworld.com'}

Message: CXX-992 Rewrite bsoncxx::document::view::find() to avoid string allocation
Branch: master
https://github.com/mongodb/mongo-cxx-driver/commit/92bb4e603d00efef15ef86e1cdfda7475ac0bb73

Generated at Wed Feb 07 22:01:02 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.