Bug 1880692, part 1 - Make StringBuilder::Unit::mLength specific to literals. r=smaug, a=RyanVM

That is the only case that uses it. This might save a bit of space
and avoids some useless stores. Idea by peterv.

Differential Revision: https://phabricator.services.mozilla.com/D202344
This commit is contained in:
Andrew McCreight 2024-02-22 16:57:13 +00:00
parent 08afc648ed
commit ee4172a5bc

View File

@ -8951,13 +8951,19 @@ class StringBuilder {
TextFragmentWithEncode,
};
struct LiteralSpan {
const char16_t* mData;
uint32_t mLength;
Span<const char16_t> AsSpan() { return Span(mData, mLength); }
};
union {
nsAtom* mAtom;
const char16_t* mLiteral;
LiteralSpan mLiteral;
nsString mString;
const nsTextFragment* mTextFragment;
};
uint32_t mLength = 0;
Type mType = Type::Unknown;
};
@ -8987,17 +8993,15 @@ class StringBuilder {
u->mAtom = aAtom;
u->mType = Unit::Type::Atom;
uint32_t len = aAtom->GetLength();
u->mLength = len;
mLength += len;
}
template <int N>
void Append(const char16_t (&aLiteral)[N]) {
constexpr uint32_t len = N - 1;
Unit* u = AddUnit();
u->mLiteral = aLiteral;
u->mLiteral = {aLiteral, len};
u->mType = Unit::Type::Literal;
uint32_t len = N - 1;
u->mLength = len;
mLength += len;
}
@ -9006,7 +9010,6 @@ class StringBuilder {
uint32_t len = aString.Length();
new (&u->mString) nsString(std::move(aString));
u->mType = Unit::Type::String;
u->mLength = len;
mLength += len;
}
@ -9014,7 +9017,6 @@ class StringBuilder {
Unit* u = AddUnit();
new (&u->mString) nsString(std::move(aString));
u->mType = Unit::Type::StringWithEncode;
u->mLength = aLen;
mLength += aLen;
}
@ -9023,7 +9025,6 @@ class StringBuilder {
u->mTextFragment = aTextFragment;
u->mType = Unit::Type::TextFragment;
uint32_t len = aTextFragment->GetLength();
u->mLength = len;
mLength += len;
}
@ -9031,7 +9032,6 @@ class StringBuilder {
Unit* u = AddUnit();
u->mTextFragment = aTextFragment;
u->mType = Unit::Type::TextFragmentWithEncode;
u->mLength = aLen;
mLength += aLen;
}
@ -9062,7 +9062,7 @@ class StringBuilder {
EncodeAttrString(u.mString, appender);
break;
case Unit::Type::Literal:
appender.Append(Span(u.mLiteral, u.mLength));
appender.Append(u.mLiteral.AsSpan());
break;
case Unit::Type::TextFragment:
if (u.mTextFragment->Is2b()) {