Skip to content

Commit a514ac3

Browse files
committed
Merge #10534: Clarify prevector::erase and avoid swap-to-clear
e241a63 Clarify prevector::erase and avoid swap-to-clear (Pieter Wuille) Tree-SHA512: fa7602038feb4417158df13ee7c0351673acf38f8a824e75889710344c46a9b8d5f6059faeb521f73e48b7ad3e1a238a9e433e4b44f7c3b9085ff08ef65271fa
2 parents 303c171 + e241a63 commit a514ac3

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/prevector.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,12 @@ class prevector {
387387
}
388388

389389
iterator erase(iterator first, iterator last) {
390+
// Erase is not allowed to the change the object's capacity. That means
391+
// that when starting with an indirectly allocated prevector with
392+
// size and capacity > N, the result may be a still indirectly allocated
393+
// prevector with size <= N and capacity > N. A shrink_to_fit() call is
394+
// necessary to switch to the (more efficient) directly allocated
395+
// representation (with capacity N and size <= N).
390396
iterator p = first;
391397
char* endp = (char*)&(*end());
392398
if (!std::is_trivially_destructible<T>::value) {

src/script/script.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,9 @@ class CScript : public CScriptBase
648648

649649
void clear()
650650
{
651-
// The default std::vector::clear() does not release memory.
652-
CScriptBase().swap(*this);
651+
// The default prevector::clear() does not release memory
652+
CScriptBase::clear();
653+
shrink_to_fit();
653654
}
654655
};
655656

0 commit comments

Comments
 (0)