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
*****************************************************************************/
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;

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

@ -465,11 +465,14 @@ class TextControlState final : public SupportsWeakPtr<TextControlState> {
/**
* For avoiding allocation cost of the instance, we should reuse instances
* 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">`
* elements.
*
* FYI: `25` is just a magic number considered without enough investigation,
* 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;
friend class AutoTextControlHandlingState;