Skip to content

Commit

Permalink
Bug 1828356 - Consider to have two EventTargetChainItem array caches,…
Browse files Browse the repository at this point in the history
… r=masayuki

Differential Revision: https://phabricator.services.mozilla.com/D175570
  • Loading branch information
Olli Pettay authored and Olli Pettay committed Apr 18, 2023
1 parent b2d70e4 commit 9452f38
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions dom/events/EventDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,12 +636,19 @@ void EventTargetChainItem::HandleEventTargetChain(
}
}

static nsTArray<EventTargetChainItem>* sCachedMainThreadChain = nullptr;
// There are often 2 nested event dispatches ongoing at the same time, so
// have 2 separate caches.
static const uint32_t kCachedMainThreadChainSize = 128;
struct CachedChains {
nsTArray<EventTargetChainItem> mChain1;
nsTArray<EventTargetChainItem> mChain2;
};
static CachedChains* sCachedMainThreadChains = nullptr;

/* static */
void EventDispatcher::Shutdown() {
delete sCachedMainThreadChain;
sCachedMainThreadChain = nullptr;
delete sCachedMainThreadChains;
sCachedMainThreadChains = nullptr;
}

EventTargetChainItem* EventTargetChainItemForChromeTarget(
Expand Down Expand Up @@ -848,11 +855,19 @@ nsresult EventDispatcher::Dispatch(nsISupports* aTarget,
ELMCreationDetector cd;
nsTArray<EventTargetChainItem> chain;
if (cd.IsMainThread()) {
if (!sCachedMainThreadChain) {
sCachedMainThreadChain = new nsTArray<EventTargetChainItem>();
if (!sCachedMainThreadChains) {
sCachedMainThreadChains = new CachedChains();
}

if (sCachedMainThreadChains->mChain1.Capacity() ==
kCachedMainThreadChainSize) {
chain = std::move(sCachedMainThreadChains->mChain1);
} else if (sCachedMainThreadChains->mChain2.Capacity() ==
kCachedMainThreadChainSize) {
chain = std::move(sCachedMainThreadChains->mChain2);
} else {
chain.SetCapacity(kCachedMainThreadChainSize);
}
chain = std::move(*sCachedMainThreadChain);
chain.SetCapacity(128);
}

// Create the event target chain item for the event target.
Expand Down Expand Up @@ -1197,9 +1212,17 @@ nsresult EventDispatcher::Dispatch(nsISupports* aTarget,
*aEventStatus = preVisitor.mEventStatus;
}

if (cd.IsMainThread() && chain.Capacity() == 128 && sCachedMainThreadChain) {
chain.ClearAndRetainStorage();
chain.SwapElements(*sCachedMainThreadChain);
if (cd.IsMainThread() && chain.Capacity() == kCachedMainThreadChainSize &&
sCachedMainThreadChains) {
if (sCachedMainThreadChains->mChain1.Capacity() !=
kCachedMainThreadChainSize) {
chain.ClearAndRetainStorage();
chain.SwapElements(sCachedMainThreadChains->mChain1);
} else if (sCachedMainThreadChains->mChain2.Capacity() !=
kCachedMainThreadChainSize) {
chain.ClearAndRetainStorage();
chain.SwapElements(sCachedMainThreadChains->mChain2);
}
}

return rv;
Expand Down

0 comments on commit 9452f38

Please sign in to comment.