BitVector tweaks.

- Double the vector's capacity when growing to avoid unneeccesary reallocation.
- Do the reallocation with realloc(3) which can expand the memory in place.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120183 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Benjamin Kramer 2010-11-26 18:25:20 +00:00
parent 847d2f93ca
commit 28116c9f49

View File

@ -18,6 +18,7 @@
#include <algorithm> #include <algorithm>
#include <cassert> #include <cassert>
#include <climits> #include <climits>
#include <cstdlib>
#include <cstring> #include <cstring>
namespace llvm { namespace llvm {
@ -77,7 +78,7 @@ public:
/// bits are initialized to the specified value. /// bits are initialized to the specified value.
explicit BitVector(unsigned s, bool t = false) : Size(s) { explicit BitVector(unsigned s, bool t = false) : Size(s) {
Capacity = NumBitWords(s); Capacity = NumBitWords(s);
Bits = new BitWord[Capacity]; Bits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
init_words(Bits, Capacity, t); init_words(Bits, Capacity, t);
if (t) if (t)
clear_unused_bits(); clear_unused_bits();
@ -92,12 +93,12 @@ public:
} }
Capacity = NumBitWords(RHS.size()); Capacity = NumBitWords(RHS.size());
Bits = new BitWord[Capacity]; Bits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
std::copy(RHS.Bits, &RHS.Bits[Capacity], Bits); std::memcpy(Bits, RHS.Bits, Capacity * sizeof(BitWord));
} }
~BitVector() { ~BitVector() {
delete[] Bits; std::free(Bits);
} }
/// empty - Tests whether there are no bits in this bitvector. /// empty - Tests whether there are no bits in this bitvector.
@ -341,18 +342,18 @@ public:
unsigned RHSWords = NumBitWords(Size); unsigned RHSWords = NumBitWords(Size);
if (Size <= Capacity * BITWORD_SIZE) { if (Size <= Capacity * BITWORD_SIZE) {
if (Size) if (Size)
std::copy(RHS.Bits, &RHS.Bits[RHSWords], Bits); std::memcpy(Bits, RHS.Bits, RHSWords * sizeof(BitWord));
clear_unused_bits(); clear_unused_bits();
return *this; return *this;
} }
// Grow the bitvector to have enough elements. // Grow the bitvector to have enough elements.
Capacity = RHSWords; Capacity = RHSWords;
BitWord *NewBits = new BitWord[Capacity]; BitWord *NewBits = (BitWord *)std::malloc(Capacity * sizeof(BitWord));
std::copy(RHS.Bits, &RHS.Bits[RHSWords], NewBits); std::memcpy(NewBits, RHS.Bits, Capacity * sizeof(BitWord));
// Destroy the old bits. // Destroy the old bits.
delete[] Bits; std::free(Bits);
Bits = NewBits; Bits = NewBits;
return *this; return *this;
@ -390,17 +391,8 @@ private:
} }
void grow(unsigned NewSize) { void grow(unsigned NewSize) {
unsigned OldCapacity = Capacity; Capacity = std::max(NumBitWords(NewSize), Capacity * 2);
Capacity = NumBitWords(NewSize); Bits = (BitWord *)std::realloc(Bits, Capacity * sizeof(BitWord));
BitWord *NewBits = new BitWord[Capacity];
// Copy the old bits over.
if (OldCapacity != 0)
std::copy(Bits, &Bits[OldCapacity], NewBits);
// Destroy the old bits.
delete[] Bits;
Bits = NewBits;
clear_unused_bits(); clear_unused_bits();
} }