summaryrefslogtreecommitdiff
path: root/src/mongo/bson
diff options
context:
space:
mode:
authorGeert Bosch <bosch@gnat.com>2018-07-08 09:32:03 -0400
committerGeert Bosch <geert@mongodb.com>2018-08-05 09:03:08 -0400
commit27d839b2ab77ab822c578835b09f72129b80c68a (patch)
treef2938996d5550ea076e8f004635e994b528cb2d3 /src/mongo/bson
parentecb0b6c6dfb854f53c3e4fa05aa61bb9a1bed554 (diff)
downloadmongo-27d839b2ab77ab822c578835b09f72129b80c68a.tar.gz
SERVER-36108 speed up BSONArrayBuilder by counting in decimal
Now removed constexpr that broke Windows build.
Diffstat (limited to 'src/mongo/bson')
-rw-r--r--src/mongo/bson/SConscript10
-rw-r--r--src/mongo/bson/bsonobjbuilder.h70
-rw-r--r--src/mongo/bson/bsonobjbuilder_bm.cpp52
3 files changed, 96 insertions, 36 deletions
diff --git a/src/mongo/bson/SConscript b/src/mongo/bson/SConscript
index fbe9ef1dae0..f1d9ab122ea 100644
--- a/src/mongo/bson/SConscript
+++ b/src/mongo/bson/SConscript
@@ -65,6 +65,16 @@ env.CppUnitTest(
],
)
+env.Benchmark(
+ target='bsonobjbuilder_bm',
+ source=[
+ 'bsonobjbuilder_bm.cpp',
+ ],
+ LIBDEPS=[
+ '$BUILD_DIR/mongo/base',
+ ],
+)
+
env.CppUnitTest(
target='bsonelement_test',
source=[
diff --git a/src/mongo/bson/bsonobjbuilder.h b/src/mongo/bson/bsonobjbuilder.h
index 6162495c1cd..ef92065d9a0 100644
--- a/src/mongo/bson/bsonobjbuilder.h
+++ b/src/mongo/bson/bsonobjbuilder.h
@@ -48,7 +48,7 @@
#include "mongo/bson/util/builder.h"
#include "mongo/platform/decimal128.h"
#include "mongo/stdx/type_traits.h"
-#include "mongo/util/itoa.h"
+#include "mongo/util/decimal_counter.h"
namespace mongo {
@@ -810,20 +810,20 @@ private:
class BSONArrayBuilder {
public:
- BSONArrayBuilder() : _i(0), _b() {}
- BSONArrayBuilder(BufBuilder& _b) : _i(0), _b(_b) {}
- BSONArrayBuilder(int initialSize) : _i(0), _b(initialSize) {}
+ BSONArrayBuilder() {}
+ BSONArrayBuilder(BufBuilder& _b) : _b(_b) {}
+ BSONArrayBuilder(int initialSize) : _b(initialSize) {}
template <typename T>
BSONArrayBuilder& append(const T& x) {
- ItoA itoa(_i++);
- _b.append(itoa, x);
+ _b.append(_fieldCount, x);
+ ++_fieldCount;
return *this;
}
BSONArrayBuilder& append(const BSONElement& e) {
- ItoA itoa(_i++);
- _b.appendAs(e, itoa);
+ _b.appendAs(e, _fieldCount);
+ ++_fieldCount;
return *this;
}
@@ -833,19 +833,19 @@ public:
template <typename T>
BSONArrayBuilder& operator<<(const T& x) {
- ItoA itoa(_i++);
- _b << itoa << x;
+ _b << _fieldCount << x;
+ ++_fieldCount;
return *this;
}
void appendNull() {
- ItoA itoa(_i++);
- _b.appendNull(itoa);
+ _b.appendNull(_fieldCount);
+ ++_fieldCount;
}
void appendUndefined() {
- ItoA itoa(_i++);
- _b.appendUndefined(itoa);
+ _b.appendUndefined(_fieldCount);
+ ++_fieldCount;
}
/**
@@ -875,59 +875,57 @@ public:
// These two just use next position
BufBuilder& subobjStart() {
- ItoA itoa(_i++);
- return _b.subobjStart(itoa);
+ return _b.subobjStart(_fieldCount++);
}
BufBuilder& subarrayStart() {
- ItoA itoa(_i++);
- return _b.subarrayStart(itoa);
+ return _b.subarrayStart(_fieldCount++);
}
BSONArrayBuilder& appendRegex(StringData regex, StringData options = "") {
- ItoA itoa(_i++);
- _b.appendRegex(itoa, regex, options);
+ _b.appendRegex(_fieldCount, regex, options);
+ ++_fieldCount;
return *this;
}
BSONArrayBuilder& appendBinData(int len, BinDataType type, const void* data) {
- ItoA itoa(_i++);
- _b.appendBinData(itoa, len, type, data);
+ _b.appendBinData(_fieldCount, len, type, data);
+ ++_fieldCount;
return *this;
}
BSONArrayBuilder& appendCode(StringData code) {
- ItoA itoa(_i++);
- _b.appendCode(itoa, code);
+ _b.appendCode(_fieldCount, code);
+ ++_fieldCount;
return *this;
}
BSONArrayBuilder& appendCodeWScope(StringData code, const BSONObj& scope) {
- ItoA itoa(_i++);
- _b.appendCodeWScope(itoa, code, scope);
+ _b.appendCodeWScope(_fieldCount, code, scope);
+ ++_fieldCount;
return *this;
}
BSONArrayBuilder& appendTimeT(time_t dt) {
- ItoA itoa(_i++);
- _b.appendTimeT(itoa, dt);
+ _b.appendTimeT(_fieldCount, dt);
+ ++_fieldCount;
return *this;
}
BSONArrayBuilder& appendDate(Date_t dt) {
- ItoA itoa(_i++);
- _b.appendDate(itoa, dt);
+ _b.appendDate(_fieldCount, dt);
+ ++_fieldCount;
return *this;
}
BSONArrayBuilder& appendBool(bool val) {
- ItoA itoa(_i++);
- _b.appendBool(itoa, val);
+ _b.appendBool(_fieldCount, val);
+ ++_fieldCount;
return *this;
}
BSONArrayBuilder& appendTimestamp(unsigned long long ts) {
- ItoA itoa(_i++);
- _b.appendTimestamp(itoa, ts);
+ _b.appendTimestamp(_fieldCount, ts);
+ ++_fieldCount;
return *this;
}
@@ -939,7 +937,7 @@ public:
return _b.len();
}
int arrSize() const {
- return _i;
+ return _fieldCount;
}
BufBuilder& bb() {
@@ -947,7 +945,7 @@ public:
}
private:
- std::uint32_t _i;
+ DecimalCounter<uint32_t> _fieldCount;
BSONObjBuilder _b;
};
diff --git a/src/mongo/bson/bsonobjbuilder_bm.cpp b/src/mongo/bson/bsonobjbuilder_bm.cpp
new file mode 100644
index 00000000000..53410f72615
--- /dev/null
+++ b/src/mongo/bson/bsonobjbuilder_bm.cpp
@@ -0,0 +1,52 @@
+/**
+ * Copyright (C) 2018 MongoDB Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * As a special exception, the copyright holders give permission to link the
+ * code of portions of this program with the OpenSSL library under certain
+ * conditions as described in each individual source file and distribute
+ * linked combinations including the program with the OpenSSL library. You
+ * must comply with the GNU Affero General Public License in all respects for
+ * all of the code used other than as permitted herein. If you modify file(s)
+ * with this exception, you may extend this exception to your version of the
+ * file(s), but you are not obligated to do so. If you do not wish to do so,
+ * delete this exception statement from your version. If you delete this
+ * exception statement from all source files in the program, then also delete
+ * it in the license file.
+ */
+
+#include "mongo/platform/basic.h"
+
+#include <benchmark/benchmark.h>
+
+#include "mongo/bson/bsonobjbuilder.h"
+
+namespace mongo {
+
+void BM_arrayBuilder(benchmark::State& state) {
+ size_t totalBytes = 0;
+ for (auto _ : state) {
+ benchmark::ClobberMemory();
+ BSONArrayBuilder array;
+ for (auto j = 0; j < state.range(0); j++)
+ array.append(j);
+ totalBytes += array.len();
+ benchmark::DoNotOptimize(array.done());
+ }
+ state.SetBytesProcessed(totalBytes);
+}
+
+BENCHMARK(BM_arrayBuilder)->Ranges({{{1}, {100'000}}});
+
+} // namespace mongo