Skip to content

Commit cc691ed

Browse files
fanquakevijaydasmp
authored andcommitted
Merge bitcoin#26749: refactor: Use move semantics instead of custom swap functions
95ad70a test: Default initialize `should_freeze` to `true` (Hennadii Stepanov) cea5052 refactor: Drop no longer used `swap` member functions (Hennadii Stepanov) a87fb6b clang-tidy: Fix modernize-use-default-member-init in `CScriptCheck` (Hennadii Stepanov) b4bed5c refactor: Drop no longer used `CScriptCheck()` default constructor (Hennadii Stepanov) d8427cc refactor: Use move semantics in `CCheckQueue::Loop` (Hennadii Stepanov) 9a0b524 clang-tidy, test: Fix bugprone-use-after-move in `Correct_Queue_range()` (Hennadii Stepanov) 04831fe refactor: Make move semantics explicit for callers (Hennadii Stepanov) 6c2d597 refactor: Use move semantics in `CCheckQueue::Add` (Hennadii Stepanov) 0682003 test, refactor: Avoid `CScriptCheck::swap` in `transaction_tests` (Hennadii Stepanov) 15209d9 consensus, refactor: Avoid `CScriptCheck::swap` in `CheckInputScripts` (Hennadii Stepanov) Pull request description: This PR makes code more succinct and readable by using move semantics. ACKs for top commit: martinus: re-ACK 95ad70a achow101: ACK 95ad70a TheCharlatan: re-ACK bitcoin@95ad70a MarcoFalke: re-ACK 95ad70a 🚥 Tree-SHA512: adda760891b12d252dc9b823fe7c41eed660364b6fb1a69f17607d7a31eb0bbb82a80d154a7acfaa241b5de37d42a293c2b6e059f26a8e92d88d3a87c99768fb
1 parent 04718ab commit cc691ed

6 files changed

Lines changed: 43 additions & 78 deletions

File tree

src/bench/checkqueue.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,13 @@ static void CCheckQueueSpeedPrevectorJob(benchmark::Bench& bench)
2929

3030
struct PrevectorJob {
3131
prevector<PREVECTOR_SIZE, uint8_t> p;
32-
PrevectorJob() = default;
3332
explicit PrevectorJob(FastRandomContext& insecure_rand){
3433
p.resize(insecure_rand.randrange(PREVECTOR_SIZE*2));
3534
}
3635
bool operator()()
3736
{
3837
return true;
3938
}
40-
void swap(PrevectorJob& x) noexcept
41-
{
42-
p.swap(x.p);
43-
};
4439
};
4540
CCheckQueue<PrevectorJob> queue {QUEUE_BATCH_SIZE};
4641

@@ -61,7 +56,7 @@ static void CCheckQueueSpeedPrevectorJob(benchmark::Bench& bench)
6156
// Make insecure_rand here so that each iteration is identical.
6257
CCheckQueueControl<PrevectorJob> control(&queue);
6358
for (auto vChecks : vBatches) {
64-
control.Add(vChecks);
59+
control.Add(std::move(vChecks));
6560
}
6661
// control waits for completion by RAII, but
6762
// it is done explicitly here for clarity

src/checkqueue.h

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <util/threadnames.h>
1111

1212
#include <algorithm>
13+
#include <iterator>
1314
#include <vector>
1415

1516
template <typename T>
@@ -110,13 +111,9 @@ class CCheckQueue
110111
// * Try to account for idle jobs which will instantly start helping.
111112
// * Don't do batches smaller than 1 (duh), or larger than nBatchSize.
112113
nNow = std::max(1U, std::min(nBatchSize, (unsigned int)queue.size() / (nTotal + nIdle + 1)));
113-
vChecks.resize(nNow);
114-
for (unsigned int i = 0; i < nNow; i++) {
115-
// We want the lock on the m_mutex to be as short as possible, so swap jobs from the global
116-
// queue to the local batch vector instead of copying.
117-
vChecks[i].swap(queue.back());
118-
queue.pop_back();
119-
}
114+
auto start_it = queue.end() - nNow;
115+
vChecks.assign(std::make_move_iterator(start_it), std::make_move_iterator(queue.end()));
116+
queue.erase(start_it, queue.end());
120117
// Check whether we need to do work at all
121118
fOk = fAllOk;
122119
}
@@ -163,18 +160,15 @@ class CCheckQueue
163160
}
164161

165162
//! Add a batch of checks to the queue
166-
void Add(std::vector<T>& vChecks) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
163+
void Add(std::vector<T>&& vChecks) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
167164
{
168165
if (vChecks.empty()) {
169166
return;
170167
}
171168

172169
{
173170
LOCK(m_mutex);
174-
for (T& check : vChecks) {
175-
queue.emplace_back();
176-
check.swap(queue.back());
177-
}
171+
queue.insert(queue.end(), std::make_move_iterator(vChecks.begin()), std::make_move_iterator(vChecks.end()));
178172
nTodo += vChecks.size();
179173
}
180174

@@ -236,10 +230,11 @@ class CCheckQueueControl
236230
return fRet;
237231
}
238232

239-
void Add(std::vector<T>& vChecks)
233+
void Add(std::vector<T>&& vChecks)
240234
{
241-
if (pqueue != nullptr)
242-
pqueue->Add(vChecks);
235+
if (pqueue != nullptr) {
236+
pqueue->Add(std::move(vChecks));
237+
}
243238
}
244239

245240
~CCheckQueueControl()

src/test/checkqueue_tests.cpp

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ struct FakeCheck {
4242
{
4343
return true;
4444
}
45-
void swap(FakeCheck& x) noexcept {};
4645
};
4746

4847
struct FakeCheckCheckCompletion {
@@ -52,7 +51,6 @@ struct FakeCheckCheckCompletion {
5251
n_calls.fetch_add(1, std::memory_order_relaxed);
5352
return true;
5453
}
55-
void swap(FakeCheckCheckCompletion& x) noexcept {};
5654
};
5755

5856
struct FailingCheck {
@@ -62,10 +60,6 @@ struct FailingCheck {
6260
{
6361
return !fails;
6462
}
65-
void swap(FailingCheck& x) noexcept
66-
{
67-
std::swap(fails, x.fails);
68-
};
6963
};
7064

7165
struct UniqueCheck {
@@ -79,10 +73,6 @@ struct UniqueCheck {
7973
results.insert(check_id);
8074
return true;
8175
}
82-
void swap(UniqueCheck& x) noexcept
83-
{
84-
std::swap(x.check_id, check_id);
85-
};
8676
};
8777

8878

@@ -109,19 +99,13 @@ struct MemoryCheck {
10999
{
110100
fake_allocated_memory.fetch_sub(b, std::memory_order_relaxed);
111101
};
112-
void swap(MemoryCheck& x) noexcept
113-
{
114-
std::swap(b, x.b);
115-
};
116102
};
117103

118104
struct FrozenCleanupCheck {
119105
static std::atomic<uint64_t> nFrozen;
120106
static std::condition_variable cv;
121107
static std::mutex m;
122-
// Freezing can't be the default initialized behavior given how the queue
123-
// swaps in default initialized Checks.
124-
bool should_freeze {false};
108+
bool should_freeze{true};
125109
bool operator()() const
126110
{
127111
return true;
@@ -136,10 +120,17 @@ struct FrozenCleanupCheck {
136120
cv.wait(l, []{ return nFrozen.load(std::memory_order_relaxed) == 0;});
137121
}
138122
}
139-
void swap(FrozenCleanupCheck& x) noexcept
123+
FrozenCleanupCheck(FrozenCleanupCheck&& other) noexcept
140124
{
141-
std::swap(should_freeze, x.should_freeze);
142-
};
125+
should_freeze = other.should_freeze;
126+
other.should_freeze = false;
127+
}
128+
FrozenCleanupCheck& operator=(FrozenCleanupCheck&& other) noexcept
129+
{
130+
should_freeze = other.should_freeze;
131+
other.should_freeze = false;
132+
return *this;
133+
}
143134
};
144135

145136
// Static Allocations
@@ -169,14 +160,16 @@ static void Correct_Queue_range(std::vector<size_t> range)
169160
small_queue->StartWorkerThreads(SCRIPT_CHECK_THREADS);
170161
// Make vChecks here to save on malloc (this test can be slow...)
171162
std::vector<FakeCheckCheckCompletion> vChecks;
163+
vChecks.reserve(9);
172164
for (const size_t i : range) {
173165
size_t total = i;
174166
FakeCheckCheckCompletion::n_calls = 0;
175167
CCheckQueueControl<FakeCheckCheckCompletion> control(small_queue.get());
176168
while (total) {
177-
vChecks.resize(std::min(total, (size_t) InsecureRandRange(10)));
169+
vChecks.clear();
170+
vChecks.resize(std::min<size_t>(total, InsecureRandRange(10)));
178171
total -= vChecks.size();
179-
control.Add(vChecks);
172+
control.Add(std::move(vChecks));
180173
}
181174
BOOST_REQUIRE(control.Wait());
182175
BOOST_REQUIRE_EQUAL(FakeCheckCheckCompletion::n_calls, i);
@@ -236,7 +229,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Catches_Failure)
236229
vChecks.reserve(r);
237230
for (size_t k = 0; k < r && remaining; k++, remaining--)
238231
vChecks.emplace_back(remaining == 1);
239-
control.Add(vChecks);
232+
control.Add(std::move(vChecks));
240233
}
241234
bool success = control.Wait();
242235
if (i > 0) {
@@ -261,7 +254,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Recovers_From_Failure)
261254
std::vector<FailingCheck> vChecks;
262255
vChecks.resize(100, false);
263256
vChecks[99] = end_fails;
264-
control.Add(vChecks);
257+
control.Add(std::move(vChecks));
265258
}
266259
bool r =control.Wait();
267260
BOOST_REQUIRE(r != end_fails);
@@ -287,7 +280,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_UniqueCheck)
287280
std::vector<UniqueCheck> vChecks;
288281
for (size_t k = 0; k < r && total; k++)
289282
vChecks.emplace_back(--total);
290-
control.Add(vChecks);
283+
control.Add(std::move(vChecks));
291284
}
292285
}
293286
{
@@ -325,7 +318,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_Memory)
325318
// to catch any sort of deallocation failure
326319
vChecks.emplace_back(total == 0 || total == i || total == i/2);
327320
}
328-
control.Add(vChecks);
321+
control.Add(std::move(vChecks));
329322
}
330323
}
331324
BOOST_REQUIRE_EQUAL(MemoryCheck::fake_allocated_memory, 0U);
@@ -343,11 +336,7 @@ BOOST_AUTO_TEST_CASE(test_CheckQueue_FrozenCleanup)
343336
std::thread t0([&]() {
344337
CCheckQueueControl<FrozenCleanupCheck> control(queue.get());
345338
std::vector<FrozenCleanupCheck> vChecks(1);
346-
// Freezing can't be the default initialized behavior given how the queue
347-
// swaps in default initialized Checks (otherwise freezing destructor
348-
// would get called twice).
349-
vChecks[0].should_freeze = true;
350-
control.Add(vChecks);
339+
control.Add(std::move(vChecks));
351340
bool waitResult = control.Wait(); // Hangs here
352341
assert(waitResult);
353342
});

src/test/fuzz/checkqueue.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313

1414
namespace {
1515
struct DumbCheck {
16-
const bool result = false;
17-
18-
DumbCheck() = default;
16+
bool result = false;
1917

2018
explicit DumbCheck(const bool _result) : result(_result)
2119
{
@@ -25,10 +23,6 @@ struct DumbCheck {
2523
{
2624
return result;
2725
}
28-
29-
void swap(DumbCheck& x) noexcept
30-
{
31-
}
3226
};
3327
} // namespace
3428

@@ -48,15 +42,15 @@ FUZZ_TARGET(checkqueue)
4842
checks_2.emplace_back(result);
4943
}
5044
if (fuzzed_data_provider.ConsumeBool()) {
51-
check_queue_1.Add(checks_1);
45+
check_queue_1.Add(std::move(checks_1));
5246
}
5347
if (fuzzed_data_provider.ConsumeBool()) {
5448
(void)check_queue_1.Wait();
5549
}
5650

5751
CCheckQueueControl<DumbCheck> check_queue_control{&check_queue_2};
5852
if (fuzzed_data_provider.ConsumeBool()) {
59-
check_queue_control.Add(checks_2);
53+
check_queue_control.Add(std::move(checks_2));
6054
}
6155
if (fuzzed_data_provider.ConsumeBool()) {
6256
(void)check_queue_control.Wait();

src/validation.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,8 +1907,7 @@ bool CheckInputScripts(const CTransaction& tx, TxValidationState& state,
19071907
// Verify signature
19081908
CScriptCheck check(coin.out, tx, i, flags, cacheSigStore, &txdata);
19091909
if (pvChecks) {
1910-
pvChecks->push_back(CScriptCheck());
1911-
check.swap(pvChecks->back());
1910+
pvChecks->emplace_back(std::move(check));
19121911
} else if (!check()) {
19131912
const bool hasNonMandatoryFlags = (flags & STANDARD_NOT_MANDATORY_VERIFY_FLAGS) != 0;
19141913

@@ -2422,7 +2421,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
24222421
tx.GetHash().ToString(), state.ToString());
24232422
return false;
24242423
}
2425-
control.Add(vChecks);
2424+
control.Add(std::move(vChecks));
24262425
}
24272426

24282427
CTxUndo undoDummy;

src/validation.h

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -336,26 +336,19 @@ class CScriptCheck
336336
unsigned int nIn;
337337
unsigned int nFlags;
338338
bool cacheStore;
339-
ScriptError error;
339+
ScriptError error{SCRIPT_ERR_UNKNOWN_ERROR};
340340
PrecomputedTransactionData *txdata;
341341

342342
public:
343-
CScriptCheck(): ptxTo(nullptr), nIn(0), nFlags(0), cacheStore(false), error(SCRIPT_ERR_UNKNOWN_ERROR) {}
344343
CScriptCheck(const CTxOut& outIn, const CTransaction& txToIn, unsigned int nInIn, unsigned int nFlagsIn, bool cacheIn, PrecomputedTransactionData* txdataIn) :
345-
m_tx_out(outIn), ptxTo(&txToIn), nIn(nInIn), nFlags(nFlagsIn), cacheStore(cacheIn), error(SCRIPT_ERR_UNKNOWN_ERROR), txdata(txdataIn) { }
344+
m_tx_out(outIn), ptxTo(&txToIn), nIn(nInIn), nFlags(nFlagsIn), cacheStore(cacheIn), txdata(txdataIn) { }
346345

347-
bool operator()();
346+
CScriptCheck(const CScriptCheck&) = delete;
347+
CScriptCheck& operator=(const CScriptCheck&) = delete;
348+
CScriptCheck(CScriptCheck&&) = default;
349+
CScriptCheck& operator=(CScriptCheck&&) = default;
348350

349-
void swap(CScriptCheck& check) noexcept
350-
{
351-
std::swap(ptxTo, check.ptxTo);
352-
std::swap(m_tx_out, check.m_tx_out);
353-
std::swap(nIn, check.nIn);
354-
std::swap(nFlags, check.nFlags);
355-
std::swap(cacheStore, check.cacheStore);
356-
std::swap(error, check.error);
357-
std::swap(txdata, check.txdata);
358-
}
351+
bool operator()();
359352

360353
ScriptError GetScriptError() const { return error; }
361354
};

0 commit comments

Comments
 (0)