From 3d2853cfbffb8d3cf0396c558a8654cbef3efd77 Mon Sep 17 00:00:00 2001 From: "dbaron%fas.harvard.edu" Date: Wed, 6 Nov 2002 12:58:05 +0000 Subject: [PATCH] Miscellaneous performance improvements, mostly related to image loads. Cache a copy of the IO service on the Pres Context for use in starting image loads. b=170116 r=darin sr=bzbarsky --- layout/base/nsPresContext.cpp | 45 ++++++++++++++++-------- layout/base/nsPresContext.h | 8 ++++- layout/base/public/nsIPresContext.h | 8 ++++- layout/base/public/nsPresContext.h | 8 ++++- layout/base/src/nsPresContext.cpp | 45 ++++++++++++++++-------- layout/base/src/nsPresContext.h | 3 ++ layout/generic/nsImageFrame.cpp | 12 ++++--- layout/generic/nsImageFrame.h | 3 +- layout/html/base/src/nsImageFrame.cpp | 12 ++++--- layout/html/base/src/nsImageFrame.h | 3 +- layout/xul/base/src/nsMenuPopupFrame.cpp | 3 +- 11 files changed, 105 insertions(+), 45 deletions(-) diff --git a/layout/base/nsPresContext.cpp b/layout/base/nsPresContext.cpp index 0f255c4f8cc1..73da34298b01 100644 --- a/layout/base/nsPresContext.cpp +++ b/layout/base/nsPresContext.cpp @@ -888,24 +888,42 @@ nsPresContext::SetImageAnimationMode(PRUint16 aMode) return NS_OK; } -/* This function has now been deprecated. It is no longer necesary to - * hold on to presContext just to get a nsLookAndFeel. nsLookAndFeel is - * now a service provided by ServiceManager. +/* + * It is no longer necesary to hold on to presContext just to get a + * nsILookAndFeel, which can now be obtained through the service + * manager. However, this cached copy can be used when a pres context + * is available, for faster performance. */ NS_IMETHODIMP nsPresContext::GetLookAndFeel(nsILookAndFeel** aLookAndFeel) { NS_PRECONDITION(aLookAndFeel, "null out param"); - nsresult result = NS_OK; if (! mLookAndFeel) { - mLookAndFeel = do_GetService(kLookAndFeelCID,&result); + nsresult rv; + mLookAndFeel = do_GetService(kLookAndFeelCID, &rv); + NS_ENSURE_SUCCESS(rv, rv); } *aLookAndFeel = mLookAndFeel; - NS_IF_ADDREF(*aLookAndFeel); - return result; + NS_ADDREF(*aLookAndFeel); + return NS_OK; } - +/* + * Get the cached IO service, faster than the service manager could. + */ +NS_IMETHODIMP +nsPresContext::GetIOService(nsIIOService** aIOService) +{ + NS_PRECONDITION(aIOService, "null out param"); + if (! mIOService) { + nsresult rv; + mIOService = do_GetIOService(&rv); + NS_ENSURE_SUCCESS(rv, rv); + } + *aIOService = mIOService; + NS_ADDREF(*aIOService); + return NS_OK; +} NS_IMETHODIMP nsPresContext::GetBaseURL(nsIURI** aResult) @@ -1517,10 +1535,7 @@ nsPresContext::GetContainer(nsISupports** aResult) NS_IMETHODIMP nsPresContext::GetEventStateManager(nsIEventStateManager** aManager) { - NS_PRECONDITION(nsnull != aManager, "null ptr"); - if (nsnull == aManager) { - return NS_ERROR_NULL_POINTER; - } + NS_PRECONDITION(aManager, "null ptr"); if (!mEventManager) { nsresult rv; @@ -1528,10 +1543,10 @@ nsPresContext::GetEventStateManager(nsIEventStateManager** aManager) if (NS_FAILED(rv)) { return rv; } - } - //Not refcnted, set null in destructor - mEventManager->SetPresContext(this); + //Not refcnted, set null in destructor + mEventManager->SetPresContext(this); + } *aManager = mEventManager; NS_IF_ADDREF(*aManager); diff --git a/layout/base/nsPresContext.h b/layout/base/nsPresContext.h index d0a94e9ec931..55224b37a4cb 100644 --- a/layout/base/nsPresContext.h +++ b/layout/base/nsPresContext.h @@ -67,6 +67,7 @@ class nsString; class nsIEventStateManager; class nsIURI; class nsILookAndFeel; +class nsIIOService; class nsICSSPseudoComparator; class nsILanguageAtom; class nsITheme; @@ -161,10 +162,15 @@ public: NS_IMETHOD GetImageLoadFlags(nsLoadFlags& aLoadFlags) = 0; /** - * Get look and feel object + * Get cached look and feel service. */ NS_IMETHOD GetLookAndFeel(nsILookAndFeel** aLookAndFeel) = 0; + /** + * Get cached IO service. + */ + NS_IMETHOD GetIOService(nsIIOService** aIOService) = 0; + /** * Get base url for presentation */ diff --git a/layout/base/public/nsIPresContext.h b/layout/base/public/nsIPresContext.h index d0a94e9ec931..55224b37a4cb 100644 --- a/layout/base/public/nsIPresContext.h +++ b/layout/base/public/nsIPresContext.h @@ -67,6 +67,7 @@ class nsString; class nsIEventStateManager; class nsIURI; class nsILookAndFeel; +class nsIIOService; class nsICSSPseudoComparator; class nsILanguageAtom; class nsITheme; @@ -161,10 +162,15 @@ public: NS_IMETHOD GetImageLoadFlags(nsLoadFlags& aLoadFlags) = 0; /** - * Get look and feel object + * Get cached look and feel service. */ NS_IMETHOD GetLookAndFeel(nsILookAndFeel** aLookAndFeel) = 0; + /** + * Get cached IO service. + */ + NS_IMETHOD GetIOService(nsIIOService** aIOService) = 0; + /** * Get base url for presentation */ diff --git a/layout/base/public/nsPresContext.h b/layout/base/public/nsPresContext.h index d0a94e9ec931..55224b37a4cb 100644 --- a/layout/base/public/nsPresContext.h +++ b/layout/base/public/nsPresContext.h @@ -67,6 +67,7 @@ class nsString; class nsIEventStateManager; class nsIURI; class nsILookAndFeel; +class nsIIOService; class nsICSSPseudoComparator; class nsILanguageAtom; class nsITheme; @@ -161,10 +162,15 @@ public: NS_IMETHOD GetImageLoadFlags(nsLoadFlags& aLoadFlags) = 0; /** - * Get look and feel object + * Get cached look and feel service. */ NS_IMETHOD GetLookAndFeel(nsILookAndFeel** aLookAndFeel) = 0; + /** + * Get cached IO service. + */ + NS_IMETHOD GetIOService(nsIIOService** aIOService) = 0; + /** * Get base url for presentation */ diff --git a/layout/base/src/nsPresContext.cpp b/layout/base/src/nsPresContext.cpp index 0f255c4f8cc1..73da34298b01 100644 --- a/layout/base/src/nsPresContext.cpp +++ b/layout/base/src/nsPresContext.cpp @@ -888,24 +888,42 @@ nsPresContext::SetImageAnimationMode(PRUint16 aMode) return NS_OK; } -/* This function has now been deprecated. It is no longer necesary to - * hold on to presContext just to get a nsLookAndFeel. nsLookAndFeel is - * now a service provided by ServiceManager. +/* + * It is no longer necesary to hold on to presContext just to get a + * nsILookAndFeel, which can now be obtained through the service + * manager. However, this cached copy can be used when a pres context + * is available, for faster performance. */ NS_IMETHODIMP nsPresContext::GetLookAndFeel(nsILookAndFeel** aLookAndFeel) { NS_PRECONDITION(aLookAndFeel, "null out param"); - nsresult result = NS_OK; if (! mLookAndFeel) { - mLookAndFeel = do_GetService(kLookAndFeelCID,&result); + nsresult rv; + mLookAndFeel = do_GetService(kLookAndFeelCID, &rv); + NS_ENSURE_SUCCESS(rv, rv); } *aLookAndFeel = mLookAndFeel; - NS_IF_ADDREF(*aLookAndFeel); - return result; + NS_ADDREF(*aLookAndFeel); + return NS_OK; } - +/* + * Get the cached IO service, faster than the service manager could. + */ +NS_IMETHODIMP +nsPresContext::GetIOService(nsIIOService** aIOService) +{ + NS_PRECONDITION(aIOService, "null out param"); + if (! mIOService) { + nsresult rv; + mIOService = do_GetIOService(&rv); + NS_ENSURE_SUCCESS(rv, rv); + } + *aIOService = mIOService; + NS_ADDREF(*aIOService); + return NS_OK; +} NS_IMETHODIMP nsPresContext::GetBaseURL(nsIURI** aResult) @@ -1517,10 +1535,7 @@ nsPresContext::GetContainer(nsISupports** aResult) NS_IMETHODIMP nsPresContext::GetEventStateManager(nsIEventStateManager** aManager) { - NS_PRECONDITION(nsnull != aManager, "null ptr"); - if (nsnull == aManager) { - return NS_ERROR_NULL_POINTER; - } + NS_PRECONDITION(aManager, "null ptr"); if (!mEventManager) { nsresult rv; @@ -1528,10 +1543,10 @@ nsPresContext::GetEventStateManager(nsIEventStateManager** aManager) if (NS_FAILED(rv)) { return rv; } - } - //Not refcnted, set null in destructor - mEventManager->SetPresContext(this); + //Not refcnted, set null in destructor + mEventManager->SetPresContext(this); + } *aManager = mEventManager; NS_IF_ADDREF(*aManager); diff --git a/layout/base/src/nsPresContext.h b/layout/base/src/nsPresContext.h index 3d1795231e57..a906f8dcb808 100644 --- a/layout/base/src/nsPresContext.h +++ b/layout/base/src/nsPresContext.h @@ -50,6 +50,7 @@ #include "nsIEventStateManager.h" #include "nsIObserver.h" #include "nsILookAndFeel.h" +#include "nsIIOService.h" #ifdef IBMBIDI #include "nsBidiUtils.h" #endif @@ -78,6 +79,7 @@ public: NS_IMETHOD SetImageAnimationMode(PRUint16 aMode); NS_IMETHOD GetImageLoadFlags(nsLoadFlags& aLoadFlags); NS_IMETHOD GetLookAndFeel(nsILookAndFeel** aLookAndFeel); + NS_IMETHOD GetIOService(nsIIOService** aIOService); NS_IMETHOD GetBaseURL(nsIURI** aURLResult); NS_IMETHOD GetMedium(nsIAtom** aMediumResult) = 0; NS_IMETHOD ClearStyleDataAndReflow(void); @@ -213,6 +215,7 @@ protected: nsILinkHandler* mLinkHandler; // [WEAK] nsISupports* mContainer; // [WEAK] nsCOMPtr mLookAndFeel; + nsCOMPtr mIOService; nsFont mDefaultVariableFont; nsFont mDefaultFixedFont; diff --git a/layout/generic/nsImageFrame.cpp b/layout/generic/nsImageFrame.cpp index b24737bb4f60..27fa1e8c7ac5 100644 --- a/layout/generic/nsImageFrame.cpp +++ b/layout/generic/nsImageFrame.cpp @@ -1966,7 +1966,9 @@ nsImageFrame::RealLoadImage(const nsAString& aSpec, nsIPresContext *aPresContext nsCOMPtr realURI; /* don't load the image if some security check fails... */ - GetRealURI(aSpec, getter_AddRefs(realURI)); + nsCOMPtr ioService; + aPresContext->GetIOService(getter_AddRefs(ioService)); + GetRealURI(aSpec, ioService, getter_AddRefs(realURI)); if (aCheckContentPolicy) if (!CanLoadImage(realURI)) return NS_ERROR_FAILURE; @@ -2052,12 +2054,14 @@ nsImageFrame::GetURI(const nsAString& aSpec, nsIURI **aURI) newURI.Assign(NS_LITERAL_STRING("resource:/res/html/gopher-") + Substring(aSpec, INTERNAL_GOPHER_LENGTH, aSpec.Length() - INTERNAL_GOPHER_LENGTH) + NS_LITERAL_STRING(".gif")); - GetRealURI(newURI, aURI); + // XXXldb Using GetRealURI seems silly. + GetRealURI(newURI, nsnull, aURI); } } void -nsImageFrame::GetRealURI(const nsAString& aSpec, nsIURI **aURI) +nsImageFrame::GetRealURI(const nsAString& aSpec, nsIIOService *aIOService, + nsIURI **aURI) { nsCOMPtr baseURI; GetBaseURI(getter_AddRefs(baseURI)); @@ -2065,7 +2069,7 @@ nsImageFrame::GetRealURI(const nsAString& aSpec, nsIURI **aURI) GetDocumentCharacterSet(charset); NS_NewURI(aURI, aSpec, charset.IsEmpty() ? nsnull : NS_ConvertUCS2toUTF8(charset).get(), - baseURI); + baseURI, aIOService); } void diff --git a/layout/generic/nsImageFrame.h b/layout/generic/nsImageFrame.h index 1caf36331b13..7cb1c50ea456 100644 --- a/layout/generic/nsImageFrame.h +++ b/layout/generic/nsImageFrame.h @@ -215,7 +215,8 @@ protected: inline PRBool CanLoadImage(nsIURI *aURI); inline void GetURI(const nsAString& aSpec, nsIURI **aURI); - inline void GetRealURI(const nsAString& aSpec, nsIURI **aURI); + inline void GetRealURI(const nsAString& aSpec, nsIIOService *aIOService, + nsIURI **aURI); inline void GetBaseURI(nsIURI **uri); inline void GetLoadGroup(nsIPresContext *aPresContext, nsILoadGroup **aLoadGroup); diff --git a/layout/html/base/src/nsImageFrame.cpp b/layout/html/base/src/nsImageFrame.cpp index b24737bb4f60..27fa1e8c7ac5 100644 --- a/layout/html/base/src/nsImageFrame.cpp +++ b/layout/html/base/src/nsImageFrame.cpp @@ -1966,7 +1966,9 @@ nsImageFrame::RealLoadImage(const nsAString& aSpec, nsIPresContext *aPresContext nsCOMPtr realURI; /* don't load the image if some security check fails... */ - GetRealURI(aSpec, getter_AddRefs(realURI)); + nsCOMPtr ioService; + aPresContext->GetIOService(getter_AddRefs(ioService)); + GetRealURI(aSpec, ioService, getter_AddRefs(realURI)); if (aCheckContentPolicy) if (!CanLoadImage(realURI)) return NS_ERROR_FAILURE; @@ -2052,12 +2054,14 @@ nsImageFrame::GetURI(const nsAString& aSpec, nsIURI **aURI) newURI.Assign(NS_LITERAL_STRING("resource:/res/html/gopher-") + Substring(aSpec, INTERNAL_GOPHER_LENGTH, aSpec.Length() - INTERNAL_GOPHER_LENGTH) + NS_LITERAL_STRING(".gif")); - GetRealURI(newURI, aURI); + // XXXldb Using GetRealURI seems silly. + GetRealURI(newURI, nsnull, aURI); } } void -nsImageFrame::GetRealURI(const nsAString& aSpec, nsIURI **aURI) +nsImageFrame::GetRealURI(const nsAString& aSpec, nsIIOService *aIOService, + nsIURI **aURI) { nsCOMPtr baseURI; GetBaseURI(getter_AddRefs(baseURI)); @@ -2065,7 +2069,7 @@ nsImageFrame::GetRealURI(const nsAString& aSpec, nsIURI **aURI) GetDocumentCharacterSet(charset); NS_NewURI(aURI, aSpec, charset.IsEmpty() ? nsnull : NS_ConvertUCS2toUTF8(charset).get(), - baseURI); + baseURI, aIOService); } void diff --git a/layout/html/base/src/nsImageFrame.h b/layout/html/base/src/nsImageFrame.h index 1caf36331b13..7cb1c50ea456 100644 --- a/layout/html/base/src/nsImageFrame.h +++ b/layout/html/base/src/nsImageFrame.h @@ -215,7 +215,8 @@ protected: inline PRBool CanLoadImage(nsIURI *aURI); inline void GetURI(const nsAString& aSpec, nsIURI **aURI); - inline void GetRealURI(const nsAString& aSpec, nsIURI **aURI); + inline void GetRealURI(const nsAString& aSpec, nsIIOService *aIOService, + nsIURI **aURI); inline void GetBaseURI(nsIURI **uri); inline void GetLoadGroup(nsIPresContext *aPresContext, nsILoadGroup **aLoadGroup); diff --git a/layout/xul/base/src/nsMenuPopupFrame.cpp b/layout/xul/base/src/nsMenuPopupFrame.cpp index 4b4c27e618ec..0d25776cf465 100644 --- a/layout/xul/base/src/nsMenuPopupFrame.cpp +++ b/layout/xul/base/src/nsMenuPopupFrame.cpp @@ -176,8 +176,7 @@ nsMenuPopupFrame::Init(nsIPresContext* aPresContext, // lookup if we're allowed to overlap the OS bar (menubar/taskbar) from the // look&feel object nsCOMPtr lookAndFeel; - nsComponentManager::CreateInstance(kLookAndFeelCID, nsnull, NS_GET_IID(nsILookAndFeel), - getter_AddRefs(lookAndFeel)); + aPresContext->GetLookAndFeel(getter_AddRefs(lookAndFeel)); if ( lookAndFeel ) { PRBool tempBool; lookAndFeel->GetMetric(nsILookAndFeel::eMetric_MenusCanOverlapOSBar, tempBool);