From f25a8a3827ed5716b976bae2939f84565f4d6c35 Mon Sep 17 00:00:00 2001 From: Olli Pettay Date: Thu, 15 Apr 2010 12:38:32 +0300 Subject: [PATCH] Bug 558795 - speed up nsEventTargetChainItem (de)allocation, r=jst --- content/events/src/nsEventDispatcher.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/content/events/src/nsEventDispatcher.cpp b/content/events/src/nsEventDispatcher.cpp index bf779d73592e..3a4b7ff4a8d8 100644 --- a/content/events/src/nsEventDispatcher.cpp +++ b/content/events/src/nsEventDispatcher.cpp @@ -54,6 +54,8 @@ #define NS_TARGET_CHAIN_WANTS_WILL_HANDLE_EVENT (1 << 1) #define NS_TARGET_CHAIN_MAY_HAVE_MANAGER (1 << 2) +static nsEventTargetChainItem* gCachedETCI = nsnull; + // nsEventTargetChainItem represents a single item in the event target chain. class nsEventTargetChainItem { @@ -66,7 +68,13 @@ public: nsPIDOMEventTarget* aTarget, nsEventTargetChainItem* aChild = nsnull) { - void* place = aAllocator->Alloc(sizeof(nsEventTargetChainItem)); + void* place = nsnull; + if (gCachedETCI) { + place = gCachedETCI; + gCachedETCI = gCachedETCI->mNext; + } else { + place = aAllocator->Alloc(sizeof(nsEventTargetChainItem)); + } return place ? ::new (place) nsEventTargetChainItem(aTarget, aChild) : nsnull; @@ -84,7 +92,8 @@ public: while (item) { nsEventTargetChainItem* parent = item->mParent; item->~nsEventTargetChainItem(); - aAllocator->Free(item, sizeof(nsEventTargetChainItem)); + item->mNext = gCachedETCI; + gCachedETCI = item; --sCurrentEtciCount; item = parent; } @@ -220,7 +229,11 @@ public: nsCOMPtr mTarget; nsEventTargetChainItem* mChild; - nsEventTargetChainItem* mParent; + union { + nsEventTargetChainItem* mParent; + // This is used only when caching ETCI objects. + nsEventTargetChainItem* mNext; + }; PRUint16 mFlags; PRUint16 mItemFlags; nsCOMPtr mItemData; @@ -414,6 +427,7 @@ public: } if (!sEtciPoolUsers) { if (nsEventTargetChainItem::MaxEtciCount() > NS_CHAIN_POOL_SIZE) { + gCachedETCI = nsnull; delete sEtciPool; sEtciPool = nsnull; nsEventTargetChainItem::ResetMaxEtciCount(); @@ -424,6 +438,7 @@ public: static void Shutdown() { if (!sEtciPoolUsers) { + gCachedETCI = nsnull; delete sEtciPool; sEtciPool = nsnull; nsEventTargetChainItem::ResetMaxEtciCount();