1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 04:37:34 +00:00

AK: Add some inline capacity to StringBuilder

This patch adds a 128-byte inline buffer that we use before switching
to using a dynamically growing ByteBuffer.

This allows us to avoid heap allocations in many cases, and totally
incidentally also speeds up @nico's favorite test, "disasm /bin/id"
more than 2x. :^)
This commit is contained in:
Andreas Kling 2020-11-24 22:04:22 +01:00
parent a43aa82d69
commit 54ade31d84
2 changed files with 29 additions and 11 deletions

View file

@ -38,7 +38,7 @@ class StringBuilder {
public:
using OutputType = String;
explicit StringBuilder(size_t initial_capacity = 16);
explicit StringBuilder(size_t initial_capacity = inline_capacity);
~StringBuilder() { }
void append(const StringView&);
@ -83,7 +83,12 @@ public:
private:
void will_append(size_t);
u8* data() { return m_buffer.is_null() ? m_inline_buffer : m_buffer.data(); }
const u8* data() const { return m_buffer.is_null() ? m_inline_buffer : m_buffer.data(); }
bool using_inline_buffer() const { return m_buffer.is_null(); }
static constexpr size_t inline_capacity = 128;
u8 m_inline_buffer[inline_capacity];
ByteBuffer m_buffer;
size_t m_length { 0 };
};