forked from qt-creator/qt-creator
Utils: Don't allocate new memory if it is fitting in the short string
If you call reserve on a read only reference we always allocated on the heap which is not that smart. Change-Id: Ib9653c6fc87bc65716a966545c13f7ecb3712039 Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
@@ -258,6 +258,8 @@ public:
|
|||||||
m_data.allocated.data.pointer = Memory::reallocate(m_data.allocated.data.pointer,
|
m_data.allocated.data.pointer = Memory::reallocate(m_data.allocated.data.pointer,
|
||||||
newCapacity + 1);
|
newCapacity + 1);
|
||||||
m_data.allocated.data.capacity = newCapacity;
|
m_data.allocated.data.capacity = newCapacity;
|
||||||
|
} else if (newCapacity <= shortStringCapacity()) {
|
||||||
|
new (this) BasicSmallString{m_data.allocated.data.pointer, m_data.allocated.data.size};
|
||||||
} else {
|
} else {
|
||||||
const size_type oldSize = size();
|
const size_type oldSize = size();
|
||||||
newCapacity = std::max(newCapacity, oldSize);
|
newCapacity = std::max(newCapacity, oldSize);
|
||||||
|
|||||||
@@ -914,6 +914,15 @@ TEST(SmallString, ReserveSmallerThanShortStringCapacity)
|
|||||||
ASSERT_THAT(text.capacity(), AnyOf(30, 4));
|
ASSERT_THAT(text.capacity(), AnyOf(30, 4));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(SmallString, ReserveSmallerThanShortStringCapacityIsShortString)
|
||||||
|
{
|
||||||
|
SmallString text("text");
|
||||||
|
|
||||||
|
text.reserve(2);
|
||||||
|
|
||||||
|
ASSERT_TRUE(text.isShortString());
|
||||||
|
}
|
||||||
|
|
||||||
TEST(SmallString, ReserveSmallerThanReference)
|
TEST(SmallString, ReserveSmallerThanReference)
|
||||||
{
|
{
|
||||||
SmallString text("some very very very very very very very very very very very long string");
|
SmallString text("some very very very very very very very very very very very long string");
|
||||||
|
|||||||
Reference in New Issue
Block a user