commit 30a753a79987a1fe772973a11e192b3e349dc126
parent 3e9d7d74f26d01c0067ee7a0562c9144b6a7f723
Author: Jonathan Kew <jkew@mozilla.com>
Date: Mon, 24 Nov 2025 15:15:54 +0000
Bug 1979049 - Reverse the array of FontFaceRecords in oldRecords, so that we will most commonly remove them from the end rather than the beginning of the array. r=layout-reviewers,firefox-style-system-reviewers,emilio
Differential Revision: https://phabricator.services.mozilla.com/D273775
Diffstat:
1 file changed, 14 insertions(+), 5 deletions(-)
diff --git a/layout/style/FontFaceSetDocumentImpl.cpp b/layout/style/FontFaceSetDocumentImpl.cpp
@@ -412,6 +412,12 @@ bool FontFaceSetDocumentImpl::UpdateRules(
// same rules are still present.
nsTArray<FontFaceRecord> oldRecords = std::move(mRuleFaces);
+ // We reverse the oldRecords array because we will most likely be using the
+ // entries in the order they were originally added, and constantly removing
+ // the first element is inefficient if the array is large; it's better if
+ // we're most often removing elements from the end.
+ oldRecords.Reverse();
+
// Remove faces from the font family records; we need to re-insert them
// because we might end up with faces in a different order even if they're
// the same font entries as before. (The order can affect font selection
@@ -524,8 +530,9 @@ bool FontFaceSetDocumentImpl::InsertRuleFontFace(
// This is a rule backed FontFace. First, we check in aOldRecords; if
// the FontFace for the rule exists there, just move it to the new record
// list, and put the entry into the appropriate family.
- for (size_t i = 0; i < aOldRecords.Length(); ++i) {
- FontFaceRecord& rec = aOldRecords[i];
+ // Note that aOldRecords was reversed, so we search it from the end.
+ for (size_t i = aOldRecords.Length(); i > 0;) {
+ FontFaceRecord& rec = aOldRecords[--i];
const bool matches =
rec.mOrigin == Some(aSheetType) &&
@@ -572,9 +579,11 @@ bool FontFaceSetDocumentImpl::InsertRuleFontFace(
mOwner->InsertRuleFontFace(owner, aSheetType);
}
- // note the set has been modified if an old rule was skipped to find
- // this one - something has been dropped, or ordering changed
- return i > 0;
+ // Return that the set has been modified if an old rule was skipped to find
+ // this one: something has been dropped, or ordering changed.
+ // Note that the record at index i has been removed, so Length() is now the
+ // original last-element index.
+ return i < aOldRecords.Length();
}
RefPtr<FontFace> fontFace =