Closed Bug 1850645 Opened 2 years ago Closed 2 years ago

Make `AutoTextControlHandlingState` stop using `nsString`

Categories

(Core :: DOM: Forms, defect)

defect

Tracking

()

RESOLVED FIXED
119 Branch
Tracking Status
firefox119 --- fixed

People

(Reporter: masayuki, Assigned: masayuki)

References

(Blocks 1 open bug)

Details

(Whiteboard: [sp3])

Attachments

(1 file, 1 obsolete file)

The profile commented in bug 1845185 comment 5 has a lot of samples at the construction of AutoTextControlHandlingState::mSettingValue. I considered its type to nsString because if given string has refcountable buffer, it can avoid copying the buffer. However, refcountable buffer is currently created only in nsStringBuffer::ToString, and the users are a few. Therefore, we could improve the score with changing it to nsAutoString.

However, I don't think longer value than the auto buffer of nsAutoString is rare. Therefore, the approach could be a bad optimization for specific benchmarks. I found nsContentUtils::PopulateStringFromStringBuffer() which makes the buffer forcibly refcountable. Using this could be better solution.

Whiteboard: [sp3]

Surprisingly, it's faster in other tests if making AutoTextControlHandlingState::mSettingValue have refcountable buffer. It means that the further string copy cost is more expensive than allocation at the constructor. Keep investigating what should I do.

Currently, the type of AutoTextControlHandlingState::mSettingValue is
nsString, but it's a stack only class, and if the string buffer is
refcountable, nsTAutoStringN(const substring_type&) avoid copying the
memory anyway.
https://searchfox.org/mozilla-central/rev/fccd8c0ac998c4c624b6c255e621d36f72cabe01/xpcom/string/nsTString.h#295
https://searchfox.org/mozilla-central/rev/fccd8c0ac998c4c624b6c255e621d36f72cabe01/xpcom/string/nsTSubstring.cpp#504,513-514,517

So, it should be nsAutoString in any cases. Therefore, this patch changes the
type and fix similar points in TextControlState.cpp.

Now, AutoTextControlHandlingState::mSettingValue is nsAutoString. However,
if the value length equals or is longer than 64 characters, it needs to allocate
the storage in the heap unless the given string buffer is refcountable.

So, in any cases, we need to pay an allocation cost (even before creating
AutoTextControlHandlingState). Therefore, once the constructor uses
refcountable string buffer for mSettingValue, the users of
AutoTextControlHandlingState::GetSettingValue() can skip another
allocations for receiving the copy.

Depends on D188682

Okay, with a lot of testing, I believe that AutoTextControlHandlingState::mSettingValue should be nsAutoString to avoid allocation of the string buffer in most cases.

Additionally, if the setting value requires to allocate the buffer in the heap, it should be refcountable buffer because the mSettingValue may be copied during setting the value and we can skip another allocation with using refcountable buffer.

The final result is:
https://treeherder.mozilla.org/perfherder/comparesubtest?originalProject=try&newProject=try&newRevision=0de546d7bb0b9fb1cf7fb239c5f951e627db317a&originalSignature=4586009&newSignature=4586009&framework=13&application=firefox&originalRevision=78735d10fbb9b5b62a46d787e4acab2ce4e23cdb&page=1

If we make the buffer always refcountable, we get better performance in a lot of cases, but it makes some benchmark score slower. I guess that it's caused by the allocation cost which is not required if we use nsAutoString.

Attachment #9354052 - Attachment is obsolete: true
Attachment #9354051 - Attachment description: Bug 1850645 - part 1: Make `TextControlState` use `nsAutoString` r=smaug! → Bug 1850645 - Make `TextControlState` use `nsAutoString` r=smaug!
Pushed by masayuki@d-toybox.com: https://hg.mozilla.org/integration/autoland/rev/95ff3c89e929 Make `TextControlState` use `nsAutoString` r=smaug
Status: ASSIGNED → RESOLVED
Closed: 2 years ago
Resolution: --- → FIXED
Target Milestone: --- → 119 Branch
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: