Browse Source

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
pull/1/head
Wladimir J. van der Laan 6 years ago
parent
commit
67ed40ed82
No known key found for this signature in database
GPG Key ID: 74810B012346C9A6
  1. 3
      src/Makefile.bench.include
  2. 36
      src/bench/prevector_destructor.cpp
  3. 17
      src/prevector.h

3
src/Makefile.bench.include

@ -25,7 +25,8 @@ bench_bench_bitcoin_SOURCES = \ @@ -25,7 +25,8 @@ bench_bench_bitcoin_SOURCES = \
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)

36
src/bench/prevector_destructor.cpp

@ -0,0 +1,36 @@ @@ -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);

17
src/prevector.h

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

Loading…
Cancel
Save