Skip to content

Commit

Permalink
Merge bitcoin#9505: Prevector Quick Destruct
Browse files Browse the repository at this point in the history
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
  • Loading branch information
laanwj authored and PastaPastaPasta committed Feb 26, 2019
1 parent c4a3cd6 commit ee6e565
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/Makefile.bench.include
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ bench_bench_dash_SOURCES = \
bench/lockedpool.cpp \
bench/perf.cpp \
bench/perf.h \
bench/prevector_destructor.cpp \
bench/string_cast.cpp

nodist_bench_bench_dash_SOURCES = $(GENERATED_TEST_FILES)
Expand Down
36 changes: 36 additions & 0 deletions src/bench/prevector_destructor.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 12 additions & 5 deletions src/prevector.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -388,10 +389,14 @@ class prevector {
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;
Expand Down Expand Up @@ -432,7 +437,9 @@ class prevector {
}

~prevector() {
clear();
if (!std::is_trivially_destructible<T>::value) {
clear();
}
if (!is_direct()) {
free(_union.indirect);
_union.indirect = NULL;
Expand Down

0 comments on commit ee6e565

Please sign in to comment.