summaryrefslogtreecommitdiff
path: root/src/mongo
diff options
context:
space:
mode:
authorGeert Bosch <geert@mongodb.com>2018-08-03 19:20:05 -0400
committerGeert Bosch <geert@mongodb.com>2018-08-03 19:20:05 -0400
commitecb0b6c6dfb854f53c3e4fa05aa61bb9a1bed554 (patch)
tree6dbc24d26c39bc70f3c1fae5cd35b2b757e5bcd5 /src/mongo
parent806b9c32dff674b72110f2acd8604e950afab102 (diff)
downloadmongo-ecb0b6c6dfb854f53c3e4fa05aa61bb9a1bed554.tar.gz
Revert "SERVER-36108 speed up BSONArrayBuilder by counting in decimal"
This reverts commit ce9968b484b95fe3f08ffe1d15791e8fadc9e854.
Diffstat (limited to 'src/mongo')
-rw-r--r--src/mongo/bson/SConscript10
-rw-r--r--src/mongo/bson/bsonobjbuilder.h70
-rw-r--r--src/mongo/bson/bsonobjbuilder_bm.cpp52
-rw-r--r--src/mongo/util/SConscript16
-rw-r--r--src/mongo/util/decimal_counter.h103
-rw-r--r--src/mongo/util/decimal_counter_bm.cpp76
-rw-r--r--src/mongo/util/decimal_counter_test.cpp53
7 files changed, 36 insertions, 344 deletions
diff --git a/src/mongo/bson/SConscript b/src/mongo/bson/SConscript
index f1d9ab122ea..fbe9ef1dae0 100644
--- a/src/mongo/bson/SConscript
+++ b/src/mongo/bson/SConscript
@@ -65,16 +65,6 @@ 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 ef92065d9a0..6162495c1cd 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/decimal_counter.h"
+#include "mongo/util/itoa.h"
namespace mongo {
@@ -810,20 +810,20 @@ private:
class BSONArrayBuilder {
public:
- BSONArrayBuilder() {}
- BSONArrayBuilder(BufBuilder& _b) : _b(_b) {}
- BSONArrayBuilder(int initialSize) : _b(initialSize) {}
+ BSONArrayBuilder() : _i(0), _b() {}
+ BSONArrayBuilder(BufBuilder& _b) : _i(0), _b(_b) {}
+ BSONArrayBuilder(int initialSize) : _i(0), _b(initialSize) {}
template <typename T>
BSONArrayBuilder& append(const T& x) {
- _b.append(_fieldCount, x);
- ++_fieldCount;
+ ItoA itoa(_i++);
+ _b.append(itoa, x);
return *this;
}
BSONArrayBuilder& append(const BSONElement& e) {
- _b.appendAs(e, _fieldCount);
- ++_fieldCount;
+ ItoA itoa(_i++);
+ _b.appendAs(e, itoa);
return *this;
}
@@ -833,19 +833,19 @@ public:
template <typename T>
BSONArrayBuilder& operator<<(const T& x) {
- _b << _fieldCount << x;
- ++_fieldCount;
+ ItoA itoa(_i++);
+ _b << itoa << x;
return *this;
}
void appendNull() {
- _b.appendNull(_fieldCount);
- ++_fieldCount;
+ ItoA itoa(_i++);
+ _b.appendNull(itoa);
}
void appendUndefined() {
- _b.appendUndefined(_fieldCount);
- ++_fieldCount;
+ ItoA itoa(_i++);
+ _b.appendUndefined(itoa);
}
/**
@@ -875,57 +875,59 @@ public:
// These two just use next position
BufBuilder& subobjStart() {
- return _b.subobjStart(_fieldCount++);
+ ItoA itoa(_i++);
+ return _b.subobjStart(itoa);
}
BufBuilder& subarrayStart() {
- return _b.subarrayStart(_fieldCount++);
+ ItoA itoa(_i++);
+ return _b.subarrayStart(itoa);
}
BSONArrayBuilder& appendRegex(StringData regex, StringData options = "") {
- _b.appendRegex(_fieldCount, regex, options);
- ++_fieldCount;
+ ItoA itoa(_i++);
+ _b.appendRegex(itoa, regex, options);
return *this;
}
BSONArrayBuilder& appendBinData(int len, BinDataType type, const void* data) {
- _b.appendBinData(_fieldCount, len, type, data);
- ++_fieldCount;
+ ItoA itoa(_i++);
+ _b.appendBinData(itoa, len, type, data);
return *this;
}
BSONArrayBuilder& appendCode(StringData code) {
- _b.appendCode(_fieldCount, code);
- ++_fieldCount;
+ ItoA itoa(_i++);
+ _b.appendCode(itoa, code);
return *this;
}
BSONArrayBuilder& appendCodeWScope(StringData code, const BSONObj& scope) {
- _b.appendCodeWScope(_fieldCount, code, scope);
- ++_fieldCount;
+ ItoA itoa(_i++);
+ _b.appendCodeWScope(itoa, code, scope);
return *this;
}
BSONArrayBuilder& appendTimeT(time_t dt) {
- _b.appendTimeT(_fieldCount, dt);
- ++_fieldCount;
+ ItoA itoa(_i++);
+ _b.appendTimeT(itoa, dt);
return *this;
}
BSONArrayBuilder& appendDate(Date_t dt) {
- _b.appendDate(_fieldCount, dt);
- ++_fieldCount;
+ ItoA itoa(_i++);
+ _b.appendDate(itoa, dt);
return *this;
}
BSONArrayBuilder& appendBool(bool val) {
- _b.appendBool(_fieldCount, val);
- ++_fieldCount;
+ ItoA itoa(_i++);
+ _b.appendBool(itoa, val);
return *this;
}
BSONArrayBuilder& appendTimestamp(unsigned long long ts) {
- _b.appendTimestamp(_fieldCount, ts);
- ++_fieldCount;
+ ItoA itoa(_i++);
+ _b.appendTimestamp(itoa, ts);
return *this;
}
@@ -937,7 +939,7 @@ public:
return _b.len();
}
int arrSize() const {
- return _fieldCount;
+ return _i;
}
BufBuilder& bb() {
@@ -945,7 +947,7 @@ public:
}
private:
- DecimalCounter<uint32_t> _fieldCount;
+ std::uint32_t _i;
BSONObjBuilder _b;
};
diff --git a/src/mongo/bson/bsonobjbuilder_bm.cpp b/src/mongo/bson/bsonobjbuilder_bm.cpp
deleted file mode 100644
index 53410f72615..00000000000
--- a/src/mongo/bson/bsonobjbuilder_bm.cpp
+++ /dev/null
@@ -1,52 +0,0 @@
-/**
- * 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
diff --git a/src/mongo/util/SConscript b/src/mongo/util/SConscript
index 7935d4930ec..ffca101f781 100644
--- a/src/mongo/util/SConscript
+++ b/src/mongo/util/SConscript
@@ -608,13 +608,6 @@ env.Library(
)
env.CppUnitTest(
- target='decimal_counter_test',
- source=[
- 'decimal_counter_test.cpp',
- ],
-)
-
-env.CppUnitTest(
target='itoa_test',
source=[
'itoa_test.cpp',
@@ -692,15 +685,6 @@ env.CppUnitTest(
)
env.Benchmark(
- target='decimal_counter_bm',
- source=[
- 'decimal_counter_bm.cpp',
- ],
- LIBDEPS=[
- ],
-)
-
-env.Benchmark(
target='future_bm',
source=[
'future_bm.cpp',
diff --git a/src/mongo/util/decimal_counter.h b/src/mongo/util/decimal_counter.h
deleted file mode 100644
index 13b9f886127..00000000000
--- a/src/mongo/util/decimal_counter.h
+++ /dev/null
@@ -1,103 +0,0 @@
-/**
- * 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.
- */
-
-#pragma once
-
-#include <cstdint>
-#include <limits>
-
-#include "mongo/base/string_data.h"
-
-namespace mongo {
-/**
- * Stores unsigned counter as well as its decimal ASCII representation, avoiding the need for
- * separate binary to decimal conversions. This speeds up code that needs string representations
- * for all counter values.
- */
-template <typename T>
-class DecimalCounter {
-public:
- static_assert(std::is_unsigned<T>::value, "DecimalCounter requires an unsigned type");
- constexpr operator StringData() const {
- return {_digits, static_cast<size_t>(_lastDigitIndex + 1)};
- }
- constexpr operator uint32_t() const {
- return _counter;
- }
-
- /**
- * Increments the counter and its decimal representation. The decimal representation wraps
- * independently of the binary counter.
- */
- constexpr DecimalCounter<T>& operator++() {
- // Common case: just increment the last digit and we're done with the string part.
- char* lastPtr = _digits + _lastDigitIndex;
- if (MONGO_unlikely((*lastPtr)++ == '9')) {
- // Let zeroPtr point at the first char in the string, such that it and all digits
- // after need to change to zeroes.
- char* zeroPtr = lastPtr;
- while (zeroPtr > _digits && zeroPtr[-1] == '9')
- --zeroPtr;
-
- // If digits wasn't all nines, increment the first non-nine.
- if (zeroPtr > _digits) {
- zeroPtr[-1]++;
- } else if (lastPtr < _digits + sizeof(_digits) - 2) {
- // Rare case: new power of 10 increases string length, so start with a one.
- *zeroPtr++ = '1';
- _lastDigitIndex++;
- lastPtr++;
- }
- // Zero out the rest.
- do {
- *zeroPtr++ = '0';
- } while (zeroPtr <= lastPtr);
- }
- if (MONGO_unlikely(++_counter == 0))
- *this = {};
- return *this;
- }
-
- /**
- * Post-inrement version of operator++. Typically slower than pre-increment due to the need
- * to return the pre-image by value.
- */
- constexpr DecimalCounter<T> operator++(int) {
- auto pre = *this;
- operator++();
- return pre;
- }
-
-private:
- // Add 1, because digit10 is 1 less than the maximum number of digits, and 1 for the final '\0'.
- static constexpr size_t kBufSize = std::numeric_limits<T>::digits10 + 2;
- char _digits[kBufSize] = {'0'}; // Remainder is zero-initialized.
- uint8_t _lastDigitIndex = 0; // Indicates the last digit in _digits.
- T _counter = 0;
-};
-}
diff --git a/src/mongo/util/decimal_counter_bm.cpp b/src/mongo/util/decimal_counter_bm.cpp
deleted file mode 100644
index 4dad2e17940..00000000000
--- a/src/mongo/util/decimal_counter_bm.cpp
+++ /dev/null
@@ -1,76 +0,0 @@
-/**
- * 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/util/decimal_counter.h"
-#include "mongo/util/itoa.h"
-
-namespace mongo {
-
-void BM_decimalCounterPreInc(benchmark::State& state) {
- DecimalCounter<uint32_t> count;
- for (auto _ : state) {
- benchmark::ClobberMemory();
- benchmark::DoNotOptimize(StringData(++count));
- }
-}
-
-void BM_decimalCounterPostInc(benchmark::State& state) {
- DecimalCounter<uint32_t> count;
- for (auto _ : state) {
- benchmark::ClobberMemory();
- benchmark::DoNotOptimize(StringData(count++));
- }
-}
-
-void BM_ItoACounter(benchmark::State& state) {
- uint32_t count = 0;
- for (auto _ : state) {
- benchmark::ClobberMemory();
- benchmark::DoNotOptimize(StringData(ItoA(++count)));
- }
-}
-
-void BM_to_stringCounter(benchmark::State& state) {
- uint32_t count = 0;
- std::string str;
- for (auto _ : state) {
- benchmark::ClobberMemory();
- benchmark::DoNotOptimize(std::to_string(++count));
- }
-}
-
-BENCHMARK(BM_decimalCounterPreInc);
-BENCHMARK(BM_decimalCounterPostInc);
-BENCHMARK(BM_ItoACounter);
-BENCHMARK(BM_to_stringCounter);
-
-} // namespace mongo
diff --git a/src/mongo/util/decimal_counter_test.cpp b/src/mongo/util/decimal_counter_test.cpp
deleted file mode 100644
index cff11c768a5..00000000000
--- a/src/mongo/util/decimal_counter_test.cpp
+++ /dev/null
@@ -1,53 +0,0 @@
-/**
- * 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 <array>
-#include <cstdint>
-#include <limits>
-
-#include "mongo/base/string_data.h"
-#include "mongo/unittest/unittest.h"
-#include "mongo/util/decimal_counter.h"
-
-namespace {
-using namespace mongo;
-
-TEST(DecimalCounter, CountUntilWrapAround) {
- DecimalCounter<uint16_t> counter;
- uint16_t check = 0;
- do {
- StringData str = counter;
- ASSERT_EQ(std::to_string(check), str.toString());
- ASSERT_EQ(str.rawData()[str.size()], '\0');
- ASSERT_EQ(uint16_t(++counter), ++check);
- } while (check);
- ASSERT_EQ(StringData(counter), "0"_sd);
-}
-} // namespace