Skip to content
Browse files
Avoid unnecessary calls to fastZeroedMalloc() in FastBitVector

Reviewed by Yusuke Suzuki.

We were calling fastZeroedMalloc() which would allocate the memory and memset it to 0,
only to then overwrite all (or most) of that memory with memcpy().

* Source/WTF/wtf/FastBitVector.cpp:

Canonical link:
git-svn-id: 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
cdumez committed May 24, 2022
1 parent edadb60 commit 197387a9ffd21dfddc4c5ad87ae165cc8276c8d5
Showing 1 changed file with 7 additions and 7 deletions.

void FastBitVectorWordOwner::setEqualsSlow(const FastBitVectorWordOwner& other)
uint32_t* newArray = static_cast<uint32_t*>(
FastBitVectorMalloc::zeroedMalloc(other.arrayLength() * sizeof(uint32_t)));
uint32_t* newArray = static_cast<uint32_t*>(FastBitVectorMalloc::malloc(other.arrayLength() * sizeof(uint32_t)));
memcpy(newArray, other.m_words, other.arrayLength() * sizeof(uint32_t));
if (m_words)
@@ -46,14 +45,15 @@ void FastBitVectorWordOwner::setEqualsSlow(const FastBitVectorWordOwner& other)
void FastBitVectorWordOwner::resizeSlow(size_t numBits)
size_t newLength = fastBitVectorArrayLength(numBits);

RELEASE_ASSERT(newLength >= arrayLength());
size_t oldLength = arrayLength();
RELEASE_ASSERT(newLength >= oldLength);

// Use fastCalloc instead of fastRealloc because we expect the common
// Use fastMalloc instead of fastRealloc because we expect the common
// use case for this method to be initializing the size of the bitvector.

uint32_t* newArray = static_cast<uint32_t*>(FastBitVectorMalloc::zeroedMalloc(newLength * sizeof(uint32_t)));
memcpy(newArray, m_words, arrayLength() * sizeof(uint32_t));
uint32_t* newArray = static_cast<uint32_t*>(FastBitVectorMalloc::malloc(newLength * sizeof(uint32_t)));
memcpy(newArray, m_words, oldLength * sizeof(uint32_t));
memset(newArray + oldLength, 0, (newLength - oldLength) * sizeof(uint32_t));
if (m_words)
m_words = newArray;

0 comments on commit 197387a

Please sign in to comment.