Skip to content

Commit

Permalink
Clarify prevector::erase and avoid swap-to-clear
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed May 17, 2020
1 parent 28a8435 commit bbaa6e3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/prevector.h
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,12 @@ class prevector {
}

iterator erase(iterator first, iterator last) {
// Erase is not allowed to the change the object's capacity. That means
// that when starting with an indirectly allocated prevector with
// size and capacity > N, the result may be a still indirectly allocated
// prevector with size <= N and capacity > N. A shrink_to_fit() call is
// necessary to switch to the (more efficient) directly allocated
// representation (with capacity N and size <= N).
iterator p = first;
char* endp = (char*)&(*end());
if (!std::is_trivially_destructible<T>::value) {
Expand Down
5 changes: 3 additions & 2 deletions src/script/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,9 @@ class CScript : public CScriptBase
std::string ToString() const;
void clear()
{
// The default std::vector::clear() does not release memory.
CScriptBase().swap(*this);
// The default prevector::clear() does not release memory
CScriptBase::clear();
shrink_to_fit();
}
};

Expand Down

0 comments on commit bbaa6e3

Please sign in to comment.