[SERVER-2098] BSONArrayBuilder::subXStart, various forms of inefficiency Created: 16/Nov/10  Updated: 12/Jul/16  Resolved: 31/Mar/11

Status: Closed
Project: Core Server
Component/s: Internal Client
Affects Version/s: 1.6.2, 1.6.3, 1.6.4, 1.7.0, 1.7.1, 1.7.2
Fix Version/s: 1.9.0

Type: Improvement Priority: Major - P3
Reporter: Andrew Morrow (Inactive) Assignee: Mathias Stearn
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

BSONArrayBuilder has a few rather serious performance glitches around subarrayStart and subobjStart, specifically:

These functions require that you provide a 'name' for the element that you wish to start, as a string. As a result, you are forced into a strlen call (to populate StringData) and a strtol call (to convert the string to an integer), and to keep your own external integer counter to convert to a string. This is mostly useless: the common use case here is just to start the next element in the series, and BSONArrayBuilder already keeps an internal counter.

I think you could just have:

subarrayStart(); // auto-increment to next index
subobjStart(); // auto-increment to next index
subarrayStart(size_t index); // ok, you want to do something fancy, use 'fill', etc. Still no need for strtol
subobjStart(size_t index); // as above
subarrayStart( const StringData& strIndex); // do the whole complex dance with strtol, fill, etc.
subobjStart( const StringData& strIndex); // as above.

This would let most users never have to worry about this stuff at all (since they are starting array elements in order), let more advanced users use a more efficient mechanism that avoids string processing when using offsets by providing integer offsets, and finally provide a way forward for the more general case.

Finally, what should the behavior be if you specifiy a 'name' (index) for an element and you are already beyond that many elements in the array? It appears that the provided name'is just ignored in this case. Perhaps that should be an error?



 Comments   
Comment by Andrew Morrow (Inactive) [ 01/Apr/11 ]

Hey Mathias -

That looks great. It might be a few weeks until I can get around to building
1.9 and benchmarking this, but I'm pretty optimistic that it is a win for
building up an array. Thanks for getting it checked in.

Andrew

Comment by Mathias Stearn [ 31/Mar/11 ]

For now I left it ignoring numbers less than next, but at least it is doc'd now.

Comment by auto [ 31/Mar/11 ]

Author:

{u'login': u'RedBeard0531', u'name': u'Mathias Stearn', u'email': u'mathias@10gen.com'}

Message: Better BSONArrayBuilder::sub

{obj,array}

Start SERVER-2098
Branch: master
https://github.com/mongodb/mongo/commit/e59799f0790731f1615c8c369e20a66b651b58d0

Generated at Thu Feb 08 02:58:58 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.