AK: Make StringImpl a bit smaller.

There's no need for a member char* m_characters if we always store them
in the inline buffer. So with this patch, we now do.
After that, rearrange the members a bit for ideal packing. :^)
This commit is contained in:
Andreas Kling 2019-06-20 13:21:56 +02:00
parent 8c0ae711d8
commit a7dc1a7d6b
2 changed files with 30 additions and 31 deletions

View file

@ -29,15 +29,15 @@ static StringImpl* s_the_empty_stringimpl = nullptr;
StringImpl& StringImpl::the_empty_stringimpl() StringImpl& StringImpl::the_empty_stringimpl()
{ {
if (!s_the_empty_stringimpl) if (!s_the_empty_stringimpl) {
s_the_empty_stringimpl = new StringImpl(ConstructTheEmptyStringImpl); void* slot = kmalloc(sizeof(StringImpl) + sizeof(char));
; s_the_empty_stringimpl = new (slot) StringImpl(ConstructTheEmptyStringImpl);
}
return *s_the_empty_stringimpl; return *s_the_empty_stringimpl;
} }
StringImpl::StringImpl(ConstructWithInlineBufferTag, ssize_t length) StringImpl::StringImpl(ConstructWithInlineBufferTag, int length)
: m_length(length) : m_length(length)
, m_characters(m_inline_buffer)
{ {
#ifdef DEBUG_STRINGIMPL #ifdef DEBUG_STRINGIMPL
if (!g_all_live_stringimpls) if (!g_all_live_stringimpls)
@ -55,23 +55,23 @@ StringImpl::~StringImpl()
#endif #endif
} }
static inline ssize_t allocation_size_for_stringimpl(ssize_t length) static inline int allocation_size_for_stringimpl(int length)
{ {
return sizeof(StringImpl) + (sizeof(char) * length) + sizeof(char); return sizeof(StringImpl) + (sizeof(char) * length) + sizeof(char);
} }
Retained<StringImpl> StringImpl::create_uninitialized(ssize_t length, char*& buffer) Retained<StringImpl> StringImpl::create_uninitialized(int length, char*& buffer)
{ {
ASSERT(length); ASSERT(length);
void* slot = kmalloc(allocation_size_for_stringimpl(length)); void* slot = kmalloc(allocation_size_for_stringimpl(length));
ASSERT(slot); ASSERT(slot);
auto new_stringimpl = adopt(*new (slot) StringImpl(ConstructWithInlineBuffer, length)); auto new_stringimpl = adopt(*new (slot) StringImpl(ConstructWithInlineBuffer, length));
buffer = const_cast<char*>(new_stringimpl->m_characters); buffer = const_cast<char*>(new_stringimpl->characters());
buffer[length] = '\0'; buffer[length] = '\0';
return new_stringimpl; return new_stringimpl;
} }
RetainPtr<StringImpl> StringImpl::create(const char* cstring, ssize_t length, ShouldChomp should_chomp) RetainPtr<StringImpl> StringImpl::create(const char* cstring, int length, ShouldChomp should_chomp)
{ {
if (!cstring) if (!cstring)
return nullptr; return nullptr;
@ -133,8 +133,8 @@ static inline char to_ascii_uppercase(char c)
Retained<StringImpl> StringImpl::to_lowercase() const Retained<StringImpl> StringImpl::to_lowercase() const
{ {
for (ssize_t i = 0; i < m_length; ++i) { for (int i = 0; i < m_length; ++i) {
if (!is_ascii_lowercase(m_characters[i])) if (!is_ascii_lowercase(characters()[i]))
goto slow_path; goto slow_path;
} }
return const_cast<StringImpl&>(*this); return const_cast<StringImpl&>(*this);
@ -142,15 +142,15 @@ Retained<StringImpl> StringImpl::to_lowercase() const
slow_path: slow_path:
char* buffer; char* buffer;
auto lowercased = create_uninitialized(m_length, buffer); auto lowercased = create_uninitialized(m_length, buffer);
for (ssize_t i = 0; i < m_length; ++i) for (int i = 0; i < m_length; ++i)
buffer[i] = to_ascii_lowercase(m_characters[i]); buffer[i] = to_ascii_lowercase(characters()[i]);
return lowercased; return lowercased;
} }
Retained<StringImpl> StringImpl::to_uppercase() const Retained<StringImpl> StringImpl::to_uppercase() const
{ {
for (ssize_t i = 0; i < m_length; ++i) { for (int i = 0; i < m_length; ++i) {
if (!is_ascii_uppercase(m_characters[i])) if (!is_ascii_uppercase(characters()[i]))
goto slow_path; goto slow_path;
} }
return const_cast<StringImpl&>(*this); return const_cast<StringImpl&>(*this);
@ -158,8 +158,8 @@ Retained<StringImpl> StringImpl::to_uppercase() const
slow_path: slow_path:
char* buffer; char* buffer;
auto uppercased = create_uninitialized(m_length, buffer); auto uppercased = create_uninitialized(m_length, buffer);
for (ssize_t i = 0; i < m_length; ++i) for (int i = 0; i < m_length; ++i)
buffer[i] = to_ascii_uppercase(m_characters[i]); buffer[i] = to_ascii_uppercase(characters()[i]);
return uppercased; return uppercased;
} }
@ -168,8 +168,8 @@ void StringImpl::compute_hash() const
if (!length()) if (!length())
m_hash = 0; m_hash = 0;
else else
m_hash = string_hash(m_characters, m_length); m_hash = string_hash(characters(), m_length);
m_hasHash = true; m_has_hash = true;
} }
} }

View file

@ -14,9 +14,9 @@ enum ShouldChomp {
class StringImpl : public Retainable<StringImpl> { class StringImpl : public Retainable<StringImpl> {
public: public:
static Retained<StringImpl> create_uninitialized(ssize_t length, char*& buffer); static Retained<StringImpl> create_uninitialized(int length, char*& buffer);
static RetainPtr<StringImpl> create(const char* cstring, ShouldChomp = NoChomp); static RetainPtr<StringImpl> create(const char* cstring, ShouldChomp = NoChomp);
static RetainPtr<StringImpl> create(const char* cstring, ssize_t length, ShouldChomp = NoChomp); static RetainPtr<StringImpl> create(const char* cstring, int length, ShouldChomp = NoChomp);
Retained<StringImpl> to_lowercase() const; Retained<StringImpl> to_lowercase() const;
Retained<StringImpl> to_uppercase() const; Retained<StringImpl> to_uppercase() const;
@ -29,17 +29,17 @@ public:
~StringImpl(); ~StringImpl();
ssize_t length() const { return m_length; } int length() const { return m_length; }
const char* characters() const { return m_characters; } const char* characters() const { return &m_inline_buffer[0]; }
char operator[](ssize_t i) const char operator[](int i) const
{ {
ASSERT(i >= 0 && i < m_length); ASSERT(i >= 0 && i < m_length);
return m_characters[i]; return characters()[i];
} }
unsigned hash() const unsigned hash() const
{ {
if (!m_hasHash) if (!m_has_hash)
compute_hash(); compute_hash();
return m_hash; return m_hash;
} }
@ -49,21 +49,20 @@ private:
ConstructTheEmptyStringImpl ConstructTheEmptyStringImpl
}; };
explicit StringImpl(ConstructTheEmptyStringImplTag) explicit StringImpl(ConstructTheEmptyStringImplTag)
: m_characters("")
{ {
m_inline_buffer[0] = '\0';
} }
enum ConstructWithInlineBufferTag { enum ConstructWithInlineBufferTag {
ConstructWithInlineBuffer ConstructWithInlineBuffer
}; };
StringImpl(ConstructWithInlineBufferTag, ssize_t length); StringImpl(ConstructWithInlineBufferTag, int length);
void compute_hash() const; void compute_hash() const;
ssize_t m_length { 0 }; int m_length { 0 };
mutable bool m_hasHash { false };
const char* m_characters { nullptr };
mutable unsigned m_hash { 0 }; mutable unsigned m_hash { 0 };
mutable bool m_has_hash { false };
char m_inline_buffer[0]; char m_inline_buffer[0];
}; };