Bug 1597679 - part 1: Cache multiple TextControlState instances for reuse r=Ehsan

Previously, we cache only one `TextControlState` instance to reuse.  However,
`HTMLTextAreaElement` also started to allocate it in the heap rather than
a part of its instance.  Therefore, we should have more room to cache the
instances for saving the allocation cost.

Differential Revision: https://phabricator.services.mozilla.com/D54326
This commit is contained in:
Masayuki Nakano
2019-11-25 06:33:53 +00:00
parent 2b2436d9ba
commit 9805f25f95
2 changed files with 25 additions and 15 deletions

View File

@@ -1334,7 +1334,8 @@ class MOZ_STACK_CLASS AutoTextControlHandlingState {
* mozilla::TextControlState
*****************************************************************************/
TextControlState* TextControlState::sReleasedInstance = nullptr;
AutoTArray<TextControlState*, TextControlState::kMaxCountOfCacheToReuse>*
TextControlState::sReleasedInstances = nullptr;
bool TextControlState::sHasShutDown = false;
TextControlState::TextControlState(nsITextControlElement* aOwningElement)
@@ -1357,10 +1358,9 @@ TextControlState::TextControlState(nsITextControlElement* aOwningElement)
TextControlState* TextControlState::Construct(
nsITextControlElement* aOwningElement) {
if (sReleasedInstance) {
MOZ_ASSERT(!sReleasedInstance->IsBusy());
TextControlState* state = sReleasedInstance;
sReleasedInstance = nullptr;
if (sReleasedInstances && !sReleasedInstances->IsEmpty()) {
TextControlState* state = sReleasedInstances->LastElement();
sReleasedInstances->RemoveLastElement();
state->mTextCtrlElement = aOwningElement;
state->mBoundFrame = nullptr;
state->mSelectionProperties = SelectionProperties();
@@ -1387,9 +1387,11 @@ TextControlState::~TextControlState() {
void TextControlState::Shutdown() {
sHasShutDown = true;
if (sReleasedInstance) {
sReleasedInstance->DeleteOrCacheForReuse();
sReleasedInstance = nullptr;
if (sReleasedInstances) {
for (TextControlState* textControlState : *sReleasedInstances) {
textControlState->DeleteOrCacheForReuse();
}
delete sReleasedInstances;
}
}
@@ -1406,9 +1408,14 @@ void TextControlState::DeleteOrCacheForReuse() {
MOZ_ASSERT(!IsBusy());
// If we can cache this instance, we should do it instead of deleting it.
if (!sHasShutDown && !sReleasedInstance) {
sReleasedInstance = this;
sReleasedInstance->PrepareForReuse();
if (!sHasShutDown && (!sReleasedInstances || sReleasedInstances->Length() <
kMaxCountOfCacheToReuse)) {
PrepareForReuse();
if (!sReleasedInstances) {
sReleasedInstances =
new AutoTArray<TextControlState*, kMaxCountOfCacheToReuse>;
}
sReleasedInstances->AppendElement(this);
return;
}
delete this;