0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-10 10:52:31 -05:00

Merge #9505: Prevector Quick Destruct

45a5aaf Only call clear on prevector if it isn't trivially destructible and don't loop in clear (Jeremy Rubin)
aaa02e7 Add prevector destructor benchmark (Jeremy Rubin)

Tree-SHA512: 52bc8163b65b71310252f2d578349d0ddc364a6c23795c5e06e101f5449f04c96cbdca41c0cffb1974b984b8e33006471137d92b8dd4a81a98e922610a94132a
This commit is contained in:
Wladimir J. van der Laan 2017-03-14 10:42:17 +01:00
commit 67ed40ed82
No known key found for this signature in database
GPG key ID: 74810B012346C9A6
3 changed files with 50 additions and 6 deletions

View file

@ -25,7 +25,8 @@ bench_bench_bitcoin_SOURCES = \
bench/base58.cpp \ bench/base58.cpp \
bench/lockedpool.cpp \ bench/lockedpool.cpp \
bench/perf.cpp \ bench/perf.cpp \
bench/perf.h bench/perf.h \
bench/prevector_destructor.cpp
nodist_bench_bench_bitcoin_SOURCES = $(GENERATED_TEST_FILES) nodist_bench_bench_bitcoin_SOURCES = $(GENERATED_TEST_FILES)

View file

@ -0,0 +1,36 @@
// 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, unsigned char> t0;
prevector<28, unsigned char> 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, unsigned char> t0;
prevector<28, unsigned char> t1;
t0.resize(28);
t0.clear();
t1.resize(29);
t0.clear();
}
}
}
BENCHMARK(PrevectorDestructor);
BENCHMARK(PrevectorClear);

View file

@ -11,6 +11,7 @@
#include <string.h> #include <string.h>
#include <iterator> #include <iterator>
#include <type_traits>
#pragma pack(push, 1) #pragma pack(push, 1)
/** Implements a drop-in replacement for std::vector<T> which stores up to N /** Implements a drop-in replacement for std::vector<T> which stores up to N
@ -388,11 +389,15 @@ public:
iterator erase(iterator first, iterator last) { iterator erase(iterator first, iterator last) {
iterator p = first; iterator p = first;
char* endp = (char*)&(*end()); char* endp = (char*)&(*end());
if (!std::is_trivially_destructible<T>::value) {
while (p != last) { while (p != last) {
(*p).~T(); (*p).~T();
_size--; _size--;
++p; ++p;
} }
} else {
_size -= last - p;
}
memmove(&(*first), &(*last), endp - ((char*)(&(*last)))); memmove(&(*first), &(*last), endp - ((char*)(&(*last))));
return first; return first;
} }
@ -432,7 +437,9 @@ public:
} }
~prevector() { ~prevector() {
if (!std::is_trivially_destructible<T>::value) {
clear(); clear();
}
if (!is_direct()) { if (!is_direct()) {
free(_union.indirect); free(_union.indirect);
_union.indirect = NULL; _union.indirect = NULL;