Skip to content

Commit 1662223

Browse files
committedMar 24, 2025
AK: Tweak ShortString bit layout slightly
Move the byte count one step to the left in order to make space for the JS::StringOrSymbol flag.
1 parent 53da889 commit 1662223

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed
 

‎AK/StringBase.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ ReadonlyBytes ShortString::bytes() const
1818

1919
size_t ShortString::byte_count() const
2020
{
21-
return byte_count_and_short_string_flag >> 1;
21+
return byte_count_and_short_string_flag >> StringBase::SHORT_STRING_BYTE_COUNT_SHIFT_COUNT;
2222
}
2323

2424
StringBase::StringBase(NonnullRefPtr<Detail::StringData const> data)
@@ -78,7 +78,7 @@ size_t StringBase::byte_count() const
7878
{
7979
ASSERT(!is_invalid());
8080
if (is_short_string())
81-
return m_short_string.byte_count_and_short_string_flag >> 1;
81+
return m_short_string.byte_count_and_short_string_flag >> StringBase::SHORT_STRING_BYTE_COUNT_SHIFT_COUNT;
8282
return m_data->byte_count();
8383
}
8484

‎AK/StringBase.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,11 @@ class StringBase {
103103
private:
104104
friend class ::AK::String;
105105
friend class ::AK::FlyString;
106+
friend struct ::AK::Detail::ShortString;
106107

107108
// NOTE: If the least significant bit of the pointer is set, this is a short string.
108109
static constexpr uintptr_t SHORT_STRING_FLAG = 1;
110+
static constexpr unsigned SHORT_STRING_BYTE_COUNT_SHIFT_COUNT = 2;
109111

110112
explicit StringBase(NonnullRefPtr<Detail::StringData const>);
111113

@@ -127,7 +129,7 @@ class StringBase {
127129
VERIFY(byte_count <= MAX_SHORT_STRING_BYTE_COUNT);
128130

129131
m_short_string = ShortString {};
130-
m_short_string.byte_count_and_short_string_flag = (byte_count << 1) | SHORT_STRING_FLAG;
132+
m_short_string.byte_count_and_short_string_flag = (byte_count << SHORT_STRING_BYTE_COUNT_SHIFT_COUNT) | SHORT_STRING_FLAG;
131133
return { m_short_string.storage, byte_count };
132134
}
133135

0 commit comments

Comments
 (0)
Please sign in to comment.