diff --git a/src/Makefile.bench.include b/src/Makefile.bench.include --- a/src/Makefile.bench.include +++ b/src/Makefile.bench.include @@ -24,7 +24,8 @@ bench/base58.cpp \ bench/lockedpool.cpp \ bench/perf.cpp \ - bench/perf.h + bench/perf.h \ + bench/prevector_destructor.cpp nodist_bench_bench_bitcoin_SOURCES = $(GENERATED_TEST_FILES) diff --git a/src/bench/prevector_destructor.cpp b/src/bench/prevector_destructor.cpp new file mode 100644 --- /dev/null +++ b/src/bench/prevector_destructor.cpp @@ -0,0 +1,33 @@ +// Copyright (c) 2015-2017 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include "bench.h" +#include "prevector.h" + +static void PrevectorDestructor(benchmark::State &state) { + while (state.KeepRunning()) { + for (auto x = 0; x < 1000; ++x) { + prevector<28, uint8_t> t0; + prevector<28, uint8_t> t1; + t0.resize(28); + t1.resize(29); + } + } +} + +static void PrevectorClear(benchmark::State &state) { + while (state.KeepRunning()) { + for (auto x = 0; x < 1000; ++x) { + prevector<28, uint8_t> t0; + prevector<28, uint8_t> t1; + t0.resize(28); + t0.clear(); + t1.resize(29); + t1.clear(); + } + } +} + +BENCHMARK(PrevectorDestructor); +BENCHMARK(PrevectorClear); diff --git a/src/prevector.h b/src/prevector.h --- a/src/prevector.h +++ b/src/prevector.h @@ -11,6 +11,7 @@ #include #include +#include #pragma pack(push, 1) /** @@ -476,10 +477,14 @@ iterator erase(iterator first, iterator last) { iterator p = first; char *endp = (char *)&(*end()); - while (p != last) { - (*p).~T(); - _size--; - ++p; + if (!std::is_trivially_destructible::value) { + while (p != last) { + (*p).~T(); + _size--; + ++p; + } + } else { + _size -= last - p; } memmove(&(*first), &(*last), endp - ((char *)(&(*last)))); return first; @@ -510,7 +515,9 @@ } ~prevector() { - clear(); + if (!std::is_trivially_destructible::value) { + clear(); + } if (!is_direct()) { free(_union.indirect); _union.indirect = nullptr;