Skip to content

Commit

Permalink
Bug 1682006 - Don't call Truncate() on the return string in GetProper…
Browse files Browse the repository at this point in the history
…tyValue. r=xidorn

Turns out this causes a bit of overhead when appending to an AutoString,
because Truncate() calls SetToEmptyBuffer(), which makes it go from
inline-string to buffer string, and next time we append we need to turn
the string from a buffer-based string to an inline string again, which
is some minor, but measurable, work, while profiling some
micro-benchmarks.

Probably we should make Truncate() a no-op for a zero-length string, but
I'll send that separately.

Differential Revision: https://phabricator.services.mozilla.com/D99493
  • Loading branch information
emilio committed Dec 12, 2020
1 parent 0473b8d commit b1964fa
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
6 changes: 4 additions & 2 deletions editor/libeditor/CSSEditUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1248,9 +1248,9 @@ bool CSSEditUtils::DoStyledElementsHaveSameStyle(
return true;
}

nsAutoCString propertyNameString;
nsAutoString firstValue, otherValue;
for (uint32_t i = 0; i < firstLength; i++) {
nsAutoString firstValue, otherValue;
nsAutoCString propertyNameString;
firstCSSDecl->Item(i, propertyNameString);
DebugOnly<nsresult> rvIgnored =
firstCSSDecl->GetPropertyValue(propertyNameString, firstValue);
Expand All @@ -1266,6 +1266,8 @@ bool CSSEditUtils::DoStyledElementsHaveSameStyle(
}
}
for (uint32_t i = 0; i < otherLength; i++) {
nsAutoString firstValue, otherValue;
nsAutoCString propertyNameString;
otherCSSDecl->Item(i, propertyNameString);
DebugOnly<nsresult> rvIgnored =
otherCSSDecl->GetPropertyValue(propertyNameString, otherValue);
Expand Down
2 changes: 1 addition & 1 deletion layout/style/nsComputedDOMStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ css::Rule* nsComputedDOMStyle::GetParentRule() { return nullptr; }
NS_IMETHODIMP
nsComputedDOMStyle::GetPropertyValue(const nsACString& aPropertyName,
nsAString& aReturn) {
aReturn.Truncate();
MOZ_ASSERT(aReturn.IsEmpty());

nsCSSPropertyID prop = nsCSSProps::LookupProperty(aPropertyName);

Expand Down
15 changes: 6 additions & 9 deletions layout/style/nsDOMCSSDeclaration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ nsresult nsDOMCSSDeclaration::GetPropertyValue(const nsCSSPropertyID aPropID,
nsAString& aValue) {
MOZ_ASSERT(aPropID != eCSSProperty_UNKNOWN,
"Should never pass eCSSProperty_UNKNOWN around");
MOZ_ASSERT(aValue.IsEmpty());

aValue.Truncate();
if (DeclarationBlock* decl =
GetOrCreateCSSDeclaration(eOperation_Read, nullptr)) {
decl->GetPropertyValueByID(aPropID, aValue);
Expand Down Expand Up @@ -91,10 +91,9 @@ void nsDOMCSSDeclaration::SetPropertyValue(const nsCSSPropertyID aPropID,
}

void nsDOMCSSDeclaration::GetCssText(nsAString& aCssText) {
DeclarationBlock* decl = GetOrCreateCSSDeclaration(eOperation_Read, nullptr);
aCssText.Truncate();
MOZ_ASSERT(aCssText.IsEmpty());

if (decl) {
if (auto* decl = GetOrCreateCSSDeclaration(eOperation_Read, nullptr)) {
decl->ToString(aCssText);
}
}
Expand Down Expand Up @@ -165,19 +164,17 @@ void nsDOMCSSDeclaration::IndexedGetter(uint32_t aIndex, bool& aFound,
NS_IMETHODIMP
nsDOMCSSDeclaration::GetPropertyValue(const nsACString& aPropertyName,
nsAString& aReturn) {
aReturn.Truncate();
if (DeclarationBlock* decl =
GetOrCreateCSSDeclaration(eOperation_Read, nullptr)) {
MOZ_ASSERT(aReturn.IsEmpty());
if (auto* decl = GetOrCreateCSSDeclaration(eOperation_Read, nullptr)) {
decl->GetPropertyValue(aPropertyName, aReturn);
}
return NS_OK;
}

void nsDOMCSSDeclaration::GetPropertyPriority(const nsACString& aPropertyName,
nsAString& aPriority) {
MOZ_ASSERT(aPriority.IsEmpty());
DeclarationBlock* decl = GetOrCreateCSSDeclaration(eOperation_Read, nullptr);

aPriority.Truncate();
if (decl && decl->GetPropertyIsImportant(aPropertyName)) {
aPriority.AssignLiteral("important");
}
Expand Down

0 comments on commit b1964fa

Please sign in to comment.