Skip to content

Commit ee6e565

Browse files
laanwjPastaPastaPasta
authored andcommitted
Merge bitcoin#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
1 parent c4a3cd6 commit ee6e565

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

src/Makefile.bench.include

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ bench_bench_dash_SOURCES = \
2929
bench/lockedpool.cpp \
3030
bench/perf.cpp \
3131
bench/perf.h \
32+
bench/prevector_destructor.cpp \
3233
bench/string_cast.cpp
3334

3435
nodist_bench_bench_dash_SOURCES = $(GENERATED_TEST_FILES)

src/bench/prevector_destructor.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2015-2017 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include "bench.h"
6+
#include "prevector.h"
7+
8+
static void PrevectorDestructor(benchmark::State& state)
9+
{
10+
while (state.KeepRunning()) {
11+
for (auto x = 0; x < 1000; ++x) {
12+
prevector<28, unsigned char> t0;
13+
prevector<28, unsigned char> t1;
14+
t0.resize(28);
15+
t1.resize(29);
16+
}
17+
}
18+
}
19+
20+
static void PrevectorClear(benchmark::State& state)
21+
{
22+
23+
while (state.KeepRunning()) {
24+
for (auto x = 0; x < 1000; ++x) {
25+
prevector<28, unsigned char> t0;
26+
prevector<28, unsigned char> t1;
27+
t0.resize(28);
28+
t0.clear();
29+
t1.resize(29);
30+
t0.clear();
31+
}
32+
}
33+
}
34+
35+
BENCHMARK(PrevectorDestructor);
36+
BENCHMARK(PrevectorClear);

src/prevector.h

+12-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <string.h>
1212

1313
#include <iterator>
14+
#include <type_traits>
1415

1516
#pragma pack(push, 1)
1617
/** Implements a drop-in replacement for std::vector<T> which stores up to N
@@ -388,10 +389,14 @@ class prevector {
388389
iterator erase(iterator first, iterator last) {
389390
iterator p = first;
390391
char* endp = (char*)&(*end());
391-
while (p != last) {
392-
(*p).~T();
393-
_size--;
394-
++p;
392+
if (!std::is_trivially_destructible<T>::value) {
393+
while (p != last) {
394+
(*p).~T();
395+
_size--;
396+
++p;
397+
}
398+
} else {
399+
_size -= last - p;
395400
}
396401
memmove(&(*first), &(*last), endp - ((char*)(&(*last))));
397402
return first;
@@ -432,7 +437,9 @@ class prevector {
432437
}
433438

434439
~prevector() {
435-
clear();
440+
if (!std::is_trivially_destructible<T>::value) {
441+
clear();
442+
}
436443
if (!is_direct()) {
437444
free(_union.indirect);
438445
_union.indirect = NULL;

0 commit comments

Comments
 (0)