Bug 464838. Try to resolve performance issues in DNS prefetch by putting elements, not URIS, in the queue, and deferring URI creation to pageload complete (or a timer, if we're not in pageload). r+sr=bzbarsky

This commit is contained in:
Patrick McManus 2009-01-08 13:08:20 -05:00
Родитель 81617a07f0
Коммит f488e1baf7
3 изменённых файлов: 72 добавлений и 28 удалений

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

@ -215,11 +215,7 @@ nsHTMLAnchorElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
// Prefetch links // Prefetch links
if (aDocument && nsHTMLDNSPrefetch::IsAllowed(GetOwnerDoc())) { if (aDocument && nsHTMLDNSPrefetch::IsAllowed(GetOwnerDoc())) {
nsCOMPtr<nsIURI> hrefURI; nsHTMLDNSPrefetch::PrefetchLow(this);
GetHrefURI(getter_AddRefs(hrefURI));
if (hrefURI)
nsHTMLDNSPrefetch::PrefetchLow(hrefURI);
} }
return rv; return rv;
} }

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

@ -53,6 +53,8 @@
#include "nsGkAtoms.h" #include "nsGkAtoms.h"
#include "nsIDocument.h" #include "nsIDocument.h"
#include "nsThreadUtils.h" #include "nsThreadUtils.h"
#include "nsGenericHTMLElement.h"
#include "nsITimer.h"
static NS_DEFINE_CID(kDNSServiceCID, NS_DNSSERVICE_CID); static NS_DEFINE_CID(kDNSServiceCID, NS_DNSSERVICE_CID);
static PRBool sDisablePrefetchHTTPSPref; static PRBool sDisablePrefetchHTTPSPref;
@ -138,30 +140,30 @@ nsHTMLDNSPrefetch::IsAllowed (nsIDocument *aDocument)
} }
nsresult nsresult
nsHTMLDNSPrefetch::Prefetch(nsIURI *aURI, PRUint16 flags) nsHTMLDNSPrefetch::Prefetch(nsGenericHTMLElement *aElement, PRUint16 flags)
{ {
if (!(sInitialized && sPrefetches && sDNSService)) if (!(sInitialized && sPrefetches && sDNSService))
return NS_ERROR_NOT_AVAILABLE; return NS_ERROR_NOT_AVAILABLE;
return sPrefetches->Add(flags, aURI); return sPrefetches->Add(flags, aElement);
} }
nsresult nsresult
nsHTMLDNSPrefetch::PrefetchLow(nsIURI *aURI) nsHTMLDNSPrefetch::PrefetchLow(nsGenericHTMLElement *aElement)
{ {
return Prefetch(aURI, nsIDNSService::RESOLVE_PRIORITY_LOW); return Prefetch(aElement, nsIDNSService::RESOLVE_PRIORITY_LOW);
} }
nsresult nsresult
nsHTMLDNSPrefetch::PrefetchMedium(nsIURI *aURI) nsHTMLDNSPrefetch::PrefetchMedium(nsGenericHTMLElement *aElement)
{ {
return Prefetch(aURI, nsIDNSService::RESOLVE_PRIORITY_MEDIUM); return Prefetch(aElement, nsIDNSService::RESOLVE_PRIORITY_MEDIUM);
} }
nsresult nsresult
nsHTMLDNSPrefetch::PrefetchHigh(nsIURI *aURI) nsHTMLDNSPrefetch::PrefetchHigh(nsGenericHTMLElement *aElement)
{ {
return Prefetch(aURI, 0); return Prefetch(aElement, 0);
} }
nsresult nsresult
@ -199,14 +201,21 @@ nsHTMLDNSPrefetch::PrefetchHigh(nsAString &hostname)
nsHTMLDNSPrefetch::nsDeferrals::nsDeferrals() nsHTMLDNSPrefetch::nsDeferrals::nsDeferrals()
: mHead(0), : mHead(0),
mTail(0), mTail(0),
mActiveLoaderCount(0) mActiveLoaderCount(0),
mTimerArmed(PR_FALSE)
{ {
mTimer = do_CreateInstance("@mozilla.org/timer;1");
} }
nsHTMLDNSPrefetch::nsDeferrals::~nsDeferrals() nsHTMLDNSPrefetch::nsDeferrals::~nsDeferrals()
{ {
if (mTimerArmed) {
mTimerArmed = PR_FALSE;
mTimer->Cancel();
}
while (mHead != mTail) { while (mHead != mTail) {
mEntries[mTail].mURI = nsnull; mEntries[mTail].mElement = nsnull;
mTail = (mTail + 1) & sMaxDeferredMask; mTail = (mTail + 1) & sMaxDeferredMask;
} }
} }
@ -217,7 +226,7 @@ NS_IMPL_THREADSAFE_ISUPPORTS3(nsHTMLDNSPrefetch::nsDeferrals,
nsISupportsWeakReference) nsISupportsWeakReference)
nsresult nsresult
nsHTMLDNSPrefetch::nsDeferrals::Add(PRUint16 flags, nsIURI *aURI) nsHTMLDNSPrefetch::nsDeferrals::Add(PRUint16 flags, nsGenericHTMLElement *aElement)
{ {
// The FIFO has no lock, so it can only be accessed on main thread // The FIFO has no lock, so it can only be accessed on main thread
NS_ASSERTION(NS_IsMainThread(), "nsDeferrals::Add must be on main thread"); NS_ASSERTION(NS_IsMainThread(), "nsDeferrals::Add must be on main thread");
@ -226,31 +235,45 @@ nsHTMLDNSPrefetch::nsDeferrals::Add(PRUint16 flags, nsIURI *aURI)
return NS_ERROR_DNS_LOOKUP_QUEUE_FULL; return NS_ERROR_DNS_LOOKUP_QUEUE_FULL;
mEntries[mHead].mFlags = flags; mEntries[mHead].mFlags = flags;
mEntries[mHead].mURI = aURI; mEntries[mHead].mElement = aElement;
mHead = (mHead + 1) & sMaxDeferredMask; mHead = (mHead + 1) & sMaxDeferredMask;
if (!mActiveLoaderCount && !mTimerArmed && mTimer) {
mTimerArmed = PR_TRUE;
mTimer->InitWithFuncCallback(Tick, this, 2000, nsITimer::TYPE_ONE_SHOT);
}
return NS_OK; return NS_OK;
} }
void void
nsHTMLDNSPrefetch::nsDeferrals::SubmitQueue() nsHTMLDNSPrefetch::nsDeferrals::SubmitQueue()
{ {
NS_ASSERTION(NS_IsMainThread(), "nsDeferrals::SubmitQueue must be on main thread");
nsCString hostName; nsCString hostName;
if (!sDNSService) return; if (!sDNSService) return;
while (mHead != mTail) { while (mHead != mTail) {
mEntries[mTail].mURI->GetAsciiHost(hostName); nsCOMPtr<nsIURI> hrefURI;
mEntries[mTail].mElement->GetHrefURIForAnchors(getter_AddRefs(hrefURI));
if (hrefURI)
hrefURI->GetAsciiHost(hostName);
if (!hostName.IsEmpty()) { if (!hostName.IsEmpty()) {
nsCOMPtr<nsICancelable> tmpOutstanding; nsCOMPtr<nsICancelable> tmpOutstanding;
sDNSService->AsyncResolve(hostName, sDNSService->AsyncResolve(hostName,
mEntries[mTail].mFlags, mEntries[mTail].mFlags,
this, nsnull, getter_AddRefs(tmpOutstanding)); this, nsnull, getter_AddRefs(tmpOutstanding));
} }
mEntries[mTail].mURI = nsnull; mEntries[mTail].mElement = nsnull;
mTail = (mTail + 1) & sMaxDeferredMask; mTail = (mTail + 1) & sMaxDeferredMask;
} }
if (mTimerArmed) {
mTimerArmed = PR_FALSE;
mTimer->Cancel();
}
} }
void void
@ -263,6 +286,25 @@ nsHTMLDNSPrefetch::nsDeferrals::Activate()
progress->AddProgressListener(this, nsIWebProgress::NOTIFY_STATE_DOCUMENT); progress->AddProgressListener(this, nsIWebProgress::NOTIFY_STATE_DOCUMENT);
} }
// nsITimer related method
void
nsHTMLDNSPrefetch::nsDeferrals::Tick(nsITimer *aTimer, void *aClosure)
{
nsHTMLDNSPrefetch::nsDeferrals *self = (nsHTMLDNSPrefetch::nsDeferrals *) aClosure;
NS_ASSERTION(NS_IsMainThread(), "nsDeferrals::Tick must be on main thread");
NS_ASSERTION(self->mTimerArmed, "Timer is not armed");
self->mTimerArmed = PR_FALSE;
// If the queue is not submitted here because there are outstanding pages being loaded,
// there is no need to rearm the timer as the queue will be submtited when those
// loads complete.
if (!self->mActiveLoaderCount)
self->SubmitQueue();
}
//////////// nsIDNSListener method //////////// nsIDNSListener method
NS_IMETHODIMP NS_IMETHODIMP

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

@ -40,14 +40,16 @@
#define nsHTMLDNSPrefetch_h___ #define nsHTMLDNSPrefetch_h___
#include "nsCOMPtr.h" #include "nsCOMPtr.h"
#include "nsAutoPtr.h"
#include "nsString.h" #include "nsString.h"
#include "nsIDNSListener.h" #include "nsIDNSListener.h"
#include "nsIWebProgressListener.h" #include "nsIWebProgressListener.h"
#include "nsWeakReference.h" #include "nsWeakReference.h"
class nsIURI;
class nsIDocument; class nsIDocument;
class nsGenericHTMLElement;
class nsITimer;
class nsHTMLDNSPrefetch class nsHTMLDNSPrefetch
{ {
@ -68,16 +70,16 @@ public:
// weight, but its request is also more likely to be dropped due to a // weight, but its request is also more likely to be dropped due to a
// full queue and it may only be used from the main thread. // full queue and it may only be used from the main thread.
static nsresult PrefetchHigh(nsIURI *aURI); static nsresult PrefetchHigh(nsGenericHTMLElement *aElement);
static nsresult PrefetchMedium(nsIURI *aURI); static nsresult PrefetchMedium(nsGenericHTMLElement *aElement);
static nsresult PrefetchLow(nsIURI *aURI); static nsresult PrefetchLow(nsGenericHTMLElement *aElement);
static nsresult PrefetchHigh(nsAString &host); static nsresult PrefetchHigh(nsAString &host);
static nsresult PrefetchMedium(nsAString &host); static nsresult PrefetchMedium(nsAString &host);
static nsresult PrefetchLow(nsAString &host); static nsresult PrefetchLow(nsAString &host);
private: private:
static nsresult Prefetch(nsAString &host, PRUint16 flags); static nsresult Prefetch(nsAString &host, PRUint16 flags);
static nsresult Prefetch(nsIURI *aURI, PRUint16 flags); static nsresult Prefetch(nsGenericHTMLElement *aElement, PRUint16 flags);
static PRBool IsSecureBaseContext(nsIDocument *aDocument); static PRBool IsSecureBaseContext(nsIDocument *aDocument);
public: public:
@ -93,7 +95,7 @@ public:
nsDeferrals(); nsDeferrals();
void Activate(); void Activate();
nsresult Add(PRUint16 flags, nsIURI *aURI); nsresult Add(PRUint16 flags, nsGenericHTMLElement *aElement);
private: private:
~nsDeferrals(); ~nsDeferrals();
@ -103,14 +105,18 @@ public:
PRUint16 mHead; PRUint16 mHead;
PRUint16 mTail; PRUint16 mTail;
PRUint32 mActiveLoaderCount; PRUint32 mActiveLoaderCount;
nsCOMPtr<nsITimer> mTimer;
PRBool mTimerArmed;
static void Tick(nsITimer *aTimer, void *aClosure);
static const int sMaxDeferred = 512; // keep power of 2 for masking static const int sMaxDeferred = 512; // keep power of 2 for masking
static const int sMaxDeferredMask = (sMaxDeferred - 1); static const int sMaxDeferredMask = (sMaxDeferred - 1);
struct deferred_entry struct deferred_entry
{ {
PRUint16 mFlags; PRUint16 mFlags;
nsCOMPtr<nsIURI> mURI; nsRefPtr<nsGenericHTMLElement> mElement;
} mEntries[sMaxDeferred]; } mEntries[sMaxDeferred];
}; };
}; };