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

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Masayuki Nakano 2019-11-25 06:33:53 +00:00
Родитель e423bcde30
Коммит 384ee1a2e7
2 изменённых файлов: 25 добавлений и 15 удалений

Просмотреть файл

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

Просмотреть файл

@ -465,11 +465,14 @@ class TextControlState final : public SupportsWeakPtr<TextControlState> {
/** /**
* For avoiding allocation cost of the instance, we should reuse instances * For avoiding allocation cost of the instance, we should reuse instances
* as far as possible. * as far as possible.
* TODO: Maybe, we should cache more instances with array. Then, it must *
* be faster to load pages which have a lot of `<input type="text">` * FYI: `25` is just a magic number considered without enough investigation,
* elements. * but at least, this value must not make damage for footprint.
* Feel free to change it if you find better number.
*/ */
static TextControlState* sReleasedInstance; static const size_t kMaxCountOfCacheToReuse = 25;
static AutoTArray<TextControlState*, kMaxCountOfCacheToReuse>*
sReleasedInstances;
static bool sHasShutDown; static bool sHasShutDown;
friend class AutoTextControlHandlingState; friend class AutoTextControlHandlingState;