From 598d7f412730cdec08510d551aabab93c8c7685e Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Tue, 18 May 2021 21:27:07 +0200 Subject: [PATCH] AK: StringBuilder should prefer to use its inline capacity first Previously StringBuilder would start allocating an external buffer once the caller has used up more than half of the inline buffer's capacity. Instead we should prefer to use the inline buffer until it is full and only then start to allocate an external buffer. --- AK/StringBuilder.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/AK/StringBuilder.cpp b/AK/StringBuilder.cpp index 5678d1de79..53a72f04de 100644 --- a/AK/StringBuilder.cpp +++ b/AK/StringBuilder.cpp @@ -22,7 +22,9 @@ inline void StringBuilder::will_append(size_t size) needed_capacity += size; VERIFY(!needed_capacity.has_overflow()); Checked expanded_capacity = needed_capacity; - expanded_capacity *= 2; + // Prefer to completely use the inline buffer first + if (needed_capacity > inline_capacity) + expanded_capacity *= 2; VERIFY(!expanded_capacity.has_overflow()); m_buffer.grow(expanded_capacity.value()); }