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
if (aDocument && nsHTMLDNSPrefetch::IsAllowed(GetOwnerDoc())) {
nsCOMPtr<nsIURI> hrefURI;
GetHrefURI(getter_AddRefs(hrefURI));
if (hrefURI)
nsHTMLDNSPrefetch::PrefetchLow(hrefURI);
nsHTMLDNSPrefetch::PrefetchLow(this);
}
return rv;
}

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

@ -53,6 +53,8 @@
#include "nsGkAtoms.h"
#include "nsIDocument.h"
#include "nsThreadUtils.h"
#include "nsGenericHTMLElement.h"
#include "nsITimer.h"
static NS_DEFINE_CID(kDNSServiceCID, NS_DNSSERVICE_CID);
static PRBool sDisablePrefetchHTTPSPref;
@ -138,30 +140,30 @@ nsHTMLDNSPrefetch::IsAllowed (nsIDocument *aDocument)
}
nsresult
nsHTMLDNSPrefetch::Prefetch(nsIURI *aURI, PRUint16 flags)
nsHTMLDNSPrefetch::Prefetch(nsGenericHTMLElement *aElement, PRUint16 flags)
{
if (!(sInitialized && sPrefetches && sDNSService))
return NS_ERROR_NOT_AVAILABLE;
return sPrefetches->Add(flags, aURI);
return sPrefetches->Add(flags, aElement);
}
nsresult
nsHTMLDNSPrefetch::PrefetchLow(nsIURI *aURI)
nsHTMLDNSPrefetch::PrefetchLow(nsGenericHTMLElement *aElement)
{
return Prefetch(aURI, nsIDNSService::RESOLVE_PRIORITY_LOW);
return Prefetch(aElement, nsIDNSService::RESOLVE_PRIORITY_LOW);
}
nsresult
nsHTMLDNSPrefetch::PrefetchMedium(nsIURI *aURI)
nsHTMLDNSPrefetch::PrefetchMedium(nsGenericHTMLElement *aElement)
{
return Prefetch(aURI, nsIDNSService::RESOLVE_PRIORITY_MEDIUM);
return Prefetch(aElement, nsIDNSService::RESOLVE_PRIORITY_MEDIUM);
}
nsresult
nsHTMLDNSPrefetch::PrefetchHigh(nsIURI *aURI)
nsHTMLDNSPrefetch::PrefetchHigh(nsGenericHTMLElement *aElement)
{
return Prefetch(aURI, 0);
return Prefetch(aElement, 0);
}
nsresult
@ -199,14 +201,21 @@ nsHTMLDNSPrefetch::PrefetchHigh(nsAString &hostname)
nsHTMLDNSPrefetch::nsDeferrals::nsDeferrals()
: mHead(0),
mTail(0),
mActiveLoaderCount(0)
mActiveLoaderCount(0),
mTimerArmed(PR_FALSE)
{
mTimer = do_CreateInstance("@mozilla.org/timer;1");
}
nsHTMLDNSPrefetch::nsDeferrals::~nsDeferrals()
{
if (mTimerArmed) {
mTimerArmed = PR_FALSE;
mTimer->Cancel();
}
while (mHead != mTail) {
mEntries[mTail].mURI = nsnull;
mEntries[mTail].mElement = nsnull;
mTail = (mTail + 1) & sMaxDeferredMask;
}
}
@ -217,7 +226,7 @@ NS_IMPL_THREADSAFE_ISUPPORTS3(nsHTMLDNSPrefetch::nsDeferrals,
nsISupportsWeakReference)
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
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;
mEntries[mHead].mFlags = flags;
mEntries[mHead].mURI = aURI;
mEntries[mHead].mElement = aElement;
mHead = (mHead + 1) & sMaxDeferredMask;
if (!mActiveLoaderCount && !mTimerArmed && mTimer) {
mTimerArmed = PR_TRUE;
mTimer->InitWithFuncCallback(Tick, this, 2000, nsITimer::TYPE_ONE_SHOT);
}
return NS_OK;
}
void
nsHTMLDNSPrefetch::nsDeferrals::SubmitQueue()
{
NS_ASSERTION(NS_IsMainThread(), "nsDeferrals::SubmitQueue must be on main thread");
nsCString hostName;
if (!sDNSService) return;
while (mHead != mTail) {
mEntries[mTail].mURI->GetAsciiHost(hostName);
if (!hostName.IsEmpty()) {
nsCOMPtr<nsIURI> hrefURI;
mEntries[mTail].mElement->GetHrefURIForAnchors(getter_AddRefs(hrefURI));
if (hrefURI)
hrefURI->GetAsciiHost(hostName);
if (!hostName.IsEmpty()) {
nsCOMPtr<nsICancelable> tmpOutstanding;
sDNSService->AsyncResolve(hostName,
mEntries[mTail].mFlags,
this, nsnull, getter_AddRefs(tmpOutstanding));
}
mEntries[mTail].mURI = nsnull;
mEntries[mTail].mElement = nsnull;
mTail = (mTail + 1) & sMaxDeferredMask;
}
if (mTimerArmed) {
mTimerArmed = PR_FALSE;
mTimer->Cancel();
}
}
void
@ -263,6 +286,25 @@ nsHTMLDNSPrefetch::nsDeferrals::Activate()
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
NS_IMETHODIMP

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

@ -40,14 +40,16 @@
#define nsHTMLDNSPrefetch_h___
#include "nsCOMPtr.h"
#include "nsAutoPtr.h"
#include "nsString.h"
#include "nsIDNSListener.h"
#include "nsIWebProgressListener.h"
#include "nsWeakReference.h"
class nsIURI;
class nsIDocument;
class nsGenericHTMLElement;
class nsITimer;
class nsHTMLDNSPrefetch
{
@ -68,16 +70,16 @@ public:
// 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.
static nsresult PrefetchHigh(nsIURI *aURI);
static nsresult PrefetchMedium(nsIURI *aURI);
static nsresult PrefetchLow(nsIURI *aURI);
static nsresult PrefetchHigh(nsGenericHTMLElement *aElement);
static nsresult PrefetchMedium(nsGenericHTMLElement *aElement);
static nsresult PrefetchLow(nsGenericHTMLElement *aElement);
static nsresult PrefetchHigh(nsAString &host);
static nsresult PrefetchMedium(nsAString &host);
static nsresult PrefetchLow(nsAString &host);
private:
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);
public:
@ -93,7 +95,7 @@ public:
nsDeferrals();
void Activate();
nsresult Add(PRUint16 flags, nsIURI *aURI);
nsresult Add(PRUint16 flags, nsGenericHTMLElement *aElement);
private:
~nsDeferrals();
@ -104,13 +106,17 @@ public:
PRUint16 mTail;
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 sMaxDeferredMask = (sMaxDeferred - 1);
struct deferred_entry
{
PRUint16 mFlags;
nsCOMPtr<nsIURI> mURI;
nsRefPtr<nsGenericHTMLElement> mElement;
} mEntries[sMaxDeferred];
};
};