From e508ddcb51eec941ae50d9c2efb06b601811dc19 Mon Sep 17 00:00:00 2001 From: Mathias Stearn Date: Mon, 6 Jun 2016 14:08:29 -0400 Subject: SERVER-24418 Make Message and BufBuilder use SharedBuffer for memory management This makes it possible to get owned BSONObj out of a Message without copying. Hooked up to the following places: - Anything using the Fetcher (including oplog fetching on secondaries) - Anything using DBClientInterface::findOne() - Anything using CursorResponse (including Sharded queries) As a simplification, Messages no longer support non-contiguous buffers, or non-owning buffers. The former wasn't used by anything, and the latter was only used by mongosniff only for messages that fit in a single packet. --- src/mongo/bson/bsonobjbuilder.h | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) (limited to 'src/mongo/bson/bsonobjbuilder.h') diff --git a/src/mongo/bson/bsonobjbuilder.h b/src/mongo/bson/bsonobjbuilder.h index 10b8a93b80e..224deb1ee6d 100644 --- a/src/mongo/bson/bsonobjbuilder.h +++ b/src/mongo/bson/bsonobjbuilder.h @@ -65,15 +65,8 @@ class BSONObjBuilder { public: /** @param initsize this is just a hint as to the final size of the object */ BSONObjBuilder(int initsize = 512) - : _b(_buf), - _buf(sizeof(BSONObj::Holder) + initsize), - _offset(sizeof(BSONObj::Holder)), - _s(this), - _tracker(0), - _doneCalled(false) { - // Skip over space for a holder object at the beginning of the buffer, followed by - // space for the object length. The length is filled in by _done. - _b.skip(sizeof(BSONObj::Holder)); + : _b(_buf), _buf(initsize), _offset(0), _s(this), _tracker(0), _doneCalled(false) { + // Skip over space for the object length. The length is filled in by _done. _b.skip(sizeof(int)); // Reserve space for the EOO byte. This means _done() can't fail. @@ -119,13 +112,12 @@ public: BSONObjBuilder(const BSONSizeTracker& tracker) : _b(_buf), - _buf(sizeof(BSONObj::Holder) + tracker.getSize()), - _offset(sizeof(BSONObj::Holder)), + _buf(tracker.getSize()), + _offset(0), _s(this), _tracker(const_cast(&tracker)), _doneCalled(false) { // See the comments in the first constructor for details. - _b.skip(sizeof(BSONObj::Holder)); _b.skip(sizeof(int)); // Reserve space for the EOO byte. This means _done() can't fail. @@ -639,9 +631,7 @@ public: BSONObj obj() { massert(10335, "builder does not own memory", owned()); doneFast(); - char* buf = _b.buf(); - decouple(); - return BSONObj::takeOwnership(buf); + return BSONObj(_b.release()); } /** Fetch the object we have built. @@ -681,10 +671,6 @@ public: _doneCalled = true; } - void decouple() { - _b.decouple(); // post done() call version. be sure jsobj frees... - } - void appendKeys(const BSONObj& keyPattern, const BSONObj& values); static std::string numStr(int i) { -- cgit v1.2.1