bug 108071 Eliminate 4000 calls to malloc by using copy getters of

registry. r=dveditz, sr=sfraser
This commit is contained in:
dp%netscape.com 2001-11-16 02:38:23 +00:00
Родитель 04fcb88305
Коммит cd2ff2480a
8 изменённых файлов: 255 добавлений и 40 удалений

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

@ -119,6 +119,47 @@ interface nsIRegistryValue : nsISupports
readonly attribute PRUint32 length; readonly attribute PRUint32 length;
}; };
[uuid(3A15FC88-7A61-4Ab4-8E58-31E95fAB3DA8)]
/**
* It sucks that nsIRegistry has to always allocate and return
* strings. nsIRegistryGetter adds in interfaces for non allocating getters
* to registry values.
*/
interface nsIRegistryGetter : nsISupports
{
/**
* Get a string value of attribute valname in widestring or utf8 format
*
* @return
* NS_OK on success.
* buf has the string value copied into it. length is NOT changed.
* NS_ERROR_REG_BUFFER_TOO_SMALL if not enough buffer space.
* length is updated to actual length in chars including
* terminating NULL and buf will be unchanged.
* NS_ERROR_FAILURE if an unknown error happened. state of buf and
* length undefined.
* various failure codes otherwise. buf and length wont be updated.
*/
void getStringUTF8IntoBuffer(in nsRegistryKey baseKey, in string path,
inout char buf, inout PRUint32 length);
/**
* Get a a byte array value of attribute valname
*
* @return
* NS_OK on success. buf has the string value copied into it.
* length is updated to actual number of bytes copied into buf.
* NS_ERROR_REG_BUFFER_TOO_SMALL if not enough buffer space.
* length is updated to actual length in PRUint8s including
* terminating NULL and buf will be unchanged.
* NS_ERROR_FAILURE if an unknown error happened. state of buf and
* length undefined.
* various other failure codes otherwise. buf and length wont be updated.
*/
void getBytesUTF8IntoBuffer(in nsRegistryKey baseKey, in string path,
inout PRUint8 buf, inout PRUint32 length);
};
%{ C++ %{ C++
#include "nsIRegistryUtils.h" #include "nsIRegistryUtils.h"
%} %}

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

@ -357,7 +357,7 @@ static void reginfo2Length( const REGINFO &in, PRUint32 &out ) {
| This code generates the implementation of the nsISupports member functions | | This code generates the implementation of the nsISupports member functions |
| for each class implemented in this file. | | for each class implemented in this file. |
------------------------------------------------------------------------------*/ ------------------------------------------------------------------------------*/
NS_IMPL_THREADSAFE_ISUPPORTS1( nsRegistry, nsIRegistry ) NS_IMPL_THREADSAFE_ISUPPORTS2(nsRegistry, nsIRegistry, nsIRegistryGetter)
NS_IMPL_ISUPPORTS2( nsRegSubtreeEnumerator, nsIEnumerator, NS_IMPL_ISUPPORTS2( nsRegSubtreeEnumerator, nsIEnumerator,
nsIRegistryEnumerator) nsIRegistryEnumerator)
NS_IMPL_ISUPPORTS1( nsRegistryNode, nsIRegistryNode ) NS_IMPL_ISUPPORTS1( nsRegistryNode, nsIRegistryNode )
@ -733,7 +733,8 @@ NS_IMETHODIMP nsRegistry::GetStringUTF8( nsRegistryKey baseKey, const char *path
// Attempt to get string into our fixed buffer // Attempt to get string into our fixed buffer
PR_Lock(mregLock); PR_Lock(mregLock);
err = NR_RegGetEntryString( mReg,(RKEY)baseKey,(char*)path, regStr, sizeof regStr ); err = NR_RegGetEntryString( mReg,(RKEY)baseKey,(char*)path, regStr,
sizeof(regStr) );
PR_Unlock(mregLock); PR_Unlock(mregLock);
if ( err == REGERR_OK ) if ( err == REGERR_OK )
@ -783,6 +784,30 @@ NS_IMETHODIMP nsRegistry::GetStringUTF8( nsRegistryKey baseKey, const char *path
return rv; return rv;
} }
NS_IMETHODIMP
nsRegistry::GetStringUTF8IntoBuffer( nsRegistryKey baseKey, const char *path,
char *buf, PRUint32 *length )
{
REGERR err = REGERR_OK;
// Attempt to get string into our fixed buffer
PR_Lock(mregLock);
err = NR_RegGetEntryString( mReg,(RKEY)baseKey,(char*)path, buf, *length );
PR_Unlock(mregLock);
// Convert status.
nsresult rv = regerr2nsresult( err );
if (rv == NS_ERROR_REG_BUFFER_TOO_SMALL) {
// fill length with the actual length
nsresult rv1 = GetValueLength( baseKey, path, length );
if(NS_FAILED(rv1))
return rv1;
}
return rv;
}
/*--------------------------- nsRegistry::SetString ---------------------------- /*--------------------------- nsRegistry::SetString ----------------------------
| Simply sets the registry contents using NR_RegSetEntryString. | | Simply sets the registry contents using NR_RegSetEntryString. |
------------------------------------------------------------------------------*/ ------------------------------------------------------------------------------*/
@ -884,6 +909,41 @@ NS_IMETHODIMP nsRegistry::GetBytesUTF8( nsRegistryKey baseKey, const char *path,
return rv; return rv;
} }
NS_IMETHODIMP
nsRegistry::GetBytesUTF8IntoBuffer( nsRegistryKey baseKey, const char *path,
PRUint8 *buf, PRUint32* length )
{
REGERR err = REGERR_OK;
// Get info about the requested entry.
PRUint32 type;
nsresult rv = GetValueType( baseKey, path, &type );
// See if that worked.
if(NS_FAILED(rv))
return rv;
// Make sure we are dealing with bytes
if (type != Bytes)
return NS_ERROR_REG_BADTYPE;
// Attempt to get bytes into our fixed buffer
PR_Lock(mregLock);
err = NR_RegGetEntry( mReg,(RKEY)baseKey,NS_CONST_CAST(char*,path),
buf, (unsigned long *)length );
PR_Unlock(mregLock);
rv = regerr2nsresult(rv);
if (rv == NS_ERROR_REG_BUFFER_TOO_SMALL) {
// fill length with the actual length
nsresult rv1 = GetValueLength( baseKey, path, length );
if(NS_FAILED(rv1))
return rv1;
}
return rv;
}
/*---------------------------- nsRegistry::GetInt ------------------------------ /*---------------------------- nsRegistry::GetInt ------------------------------
| This function is just shorthand for fetching a 1-element PRInt32 array. We | | This function is just shorthand for fetching a 1-element PRInt32 array. We |
| implement it "manually" using NR_RegGetEntry | | implement it "manually" using NR_RegGetEntry |

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

@ -43,13 +43,16 @@
#include "nsIRegistry.h" #include "nsIRegistry.h"
#include "NSReg.h" #include "NSReg.h"
struct nsRegistry : public nsIRegistry { struct nsRegistry : public nsIRegistry, nsIRegistryGetter {
// This class implements the nsISupports interface functions. // This class implements the nsISupports interface functions.
NS_DECL_ISUPPORTS NS_DECL_ISUPPORTS
// This class implements the nsIRegistry interface functions. // This class implements the nsIRegistry interface functions.
NS_DECL_NSIREGISTRY NS_DECL_NSIREGISTRY
// Fast registry getters
NS_DECL_NSIREGISTRYGETTER
// ctor/dtor // ctor/dtor
nsRegistry(); nsRegistry();
virtual ~nsRegistry(); virtual ~nsRegistry();

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

@ -736,7 +736,7 @@ nsComponentManagerImpl::PlatformInit(void)
// Set larger-than-standard buffer size to speed startup. // Set larger-than-standard buffer size to speed startup.
// This will be re-set at the end of PrePopulateRegistry() // This will be re-set at the end of PrePopulateRegistry()
mRegistry->SetBufferSize(500*1024); ((nsRegistry *)mRegistry)->SetBufferSize(500*1024);
// Check the version of registry. Nuke old versions. // Check the version of registry. Nuke old versions.
nsRegistryKey xpcomRoot; nsRegistryKey xpcomRoot;
@ -1118,11 +1118,17 @@ nsComponentManagerImpl::PlatformCLSIDToContractID(const nsCID *aClass,
nsresult nsComponentManagerImpl::PlatformPrePopulateRegistry() nsresult nsComponentManagerImpl::PlatformPrePopulateRegistry()
{ {
nsresult rv; nsresult rv;
char buf[MAXREGPATHLEN];
PRUint32 bufLength = sizeof(buf);
if (mPrePopulationDone) if (mPrePopulationDone)
return NS_OK; return NS_OK;
(void)mRegistry->SetBufferSize( 500*1024 ); (void)((nsRegistry *)mRegistry)->SetBufferSize( 500*1024 );
nsCOMPtr<nsIRegistryGetter> regGetter = do_QueryInterface(mRegistry);
if (!regGetter.get())
return NS_ERROR_FAILURE;
// Read in all CID entries and populate the mFactories // Read in all CID entries and populate the mFactories
nsCOMPtr<nsIEnumerator> cidEnum; nsCOMPtr<nsIEnumerator> cidEnum;
@ -1148,30 +1154,32 @@ nsresult nsComponentManagerImpl::PlatformPrePopulateRegistry()
rv = regEnum->CurrentItemInPlaceUTF8(&cidKey, &cidString); rv = regEnum->CurrentItemInPlaceUTF8(&cidKey, &cidString);
if (NS_FAILED(rv)) continue; if (NS_FAILED(rv)) continue;
// Create the CID entry // Figure out the component type
nsXPIDLCString library; // Use the non allocating registry getter. Our buffer is big enough
PRUint32 tmp; // We wont get NS_ERROR_REG_BUFFER_TOO_SMALL
rv = mRegistry->GetBytesUTF8(cidKey, inprocServerValueName, bufLength = sizeof(buf);
&tmp, (PRUint8**)getter_Copies(library).operator char**()); rv = regGetter->GetStringUTF8IntoBuffer(cidKey, componentTypeValueName, buf, &bufLength);
if (NS_FAILED(rv)) continue; if (NS_FAILED(rv))
nsCID aClass;
if (!(aClass.Parse(cidString))) continue;
nsXPIDLCString componentType;
if (NS_FAILED(mRegistry->GetStringUTF8(cidKey, componentTypeValueName,
getter_Copies(componentType))))
continue; continue;
int loadertype = GetLoaderType(componentType); int loadertype = GetLoaderType(buf);
if (loadertype < 0) { if (loadertype < 0) {
loadertype = AddLoaderType(componentType); loadertype = AddLoaderType(buf);
} }
nsFactoryEntry* entry =
new nsFactoryEntry(aClass, library, loadertype);
if (!entry) // Create the CID entry
bufLength = sizeof(buf);
rv = regGetter->GetBytesUTF8IntoBuffer(cidKey, inprocServerValueName,
(PRUint8 *) buf, &bufLength);
if (NS_FAILED(rv))
continue; continue;
nsCID aClass;
if (!(aClass.Parse(cidString)))
continue;
nsFactoryEntry *entry = new nsFactoryEntry(aClass, buf, loadertype);
if (!entry)
return NS_ERROR_OUT_OF_MEMORY;
nsAutoMonitor mon(mMon); nsAutoMonitor mon(mMon);
@ -1208,22 +1216,21 @@ nsresult nsComponentManagerImpl::PlatformPrePopulateRegistry()
* It is also faster, and less painful in the allocation department. * It is also faster, and less painful in the allocation department.
*/ */
rv = regEnum->CurrentItemInPlaceUTF8(&contractidKey, &contractidString); rv = regEnum->CurrentItemInPlaceUTF8(&contractidKey, &contractidString);
if (NS_FAILED(rv)) continue; if (NS_FAILED(rv))
continue;
nsXPIDLCString cidString;
rv = mRegistry->GetStringUTF8(contractidKey, classIDValueName, // Use the non allocating registry getter. Our buffer is big enough
getter_Copies(cidString)); // We wont get NS_ERROR_REG_BUFFER_TOO_SMALL
if (NS_FAILED(rv)) continue; bufLength = sizeof(buf);
rv = regGetter->GetStringUTF8IntoBuffer(contractidKey, classIDValueName, buf, &bufLength);
nsCID aClass; if (NS_FAILED(rv))
if (!(aClass.Parse(cidString))) continue;
{ nsCID aClass;
if (!aClass.Parse(buf))
continue; continue;
}
// put the {contractid, Cid} mapping into our map // put the {contractid, Cid} mapping into our map
HashContractID(contractidString, aClass); HashContractID(contractidString, aClass);
// printf("Populating [ %s, %s ]\n", cidString, contractidString);
} }
//(void)mRegistry->SetBufferSize( 10*1024 ); //(void)mRegistry->SetBufferSize( 10*1024 );
@ -2830,7 +2837,7 @@ nsresult
nsComponentManagerImpl::AutoRegister(PRInt32 when, nsIFile *inDirSpec) nsComponentManagerImpl::AutoRegister(PRInt32 when, nsIFile *inDirSpec)
{ {
nsresult rv; nsresult rv;
mRegistry->SetBufferSize( 500*1024 ); ((nsRegistry *)mRegistry)->SetBufferSize( 500*1024 );
rv = AutoRegisterImpl(when, inDirSpec); rv = AutoRegisterImpl(when, inDirSpec);
mRegistry->Flush(); mRegistry->Flush();
//mRegistry->SetBufferSize( 10*1024 ); //mRegistry->SetBufferSize( 10*1024 );

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

@ -196,7 +196,7 @@ protected:
PLDHashTable mFactories; PLDHashTable mFactories;
PLDHashTable mContractIDs; PLDHashTable mContractIDs;
PRMonitor* mMon; PRMonitor* mMon;
nsRegistry* mRegistry; nsIRegistry* mRegistry;
nsRegistryKey mXPCOMKey; nsRegistryKey mXPCOMKey;
nsRegistryKey mClassesKey; nsRegistryKey mClassesKey;
nsRegistryKey mCLSIDKey; nsRegistryKey mCLSIDKey;

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

@ -119,6 +119,47 @@ interface nsIRegistryValue : nsISupports
readonly attribute PRUint32 length; readonly attribute PRUint32 length;
}; };
[uuid(3A15FC88-7A61-4Ab4-8E58-31E95fAB3DA8)]
/**
* It sucks that nsIRegistry has to always allocate and return
* strings. nsIRegistryGetter adds in interfaces for non allocating getters
* to registry values.
*/
interface nsIRegistryGetter : nsISupports
{
/**
* Get a string value of attribute valname in widestring or utf8 format
*
* @return
* NS_OK on success.
* buf has the string value copied into it. length is NOT changed.
* NS_ERROR_REG_BUFFER_TOO_SMALL if not enough buffer space.
* length is updated to actual length in chars including
* terminating NULL and buf will be unchanged.
* NS_ERROR_FAILURE if an unknown error happened. state of buf and
* length undefined.
* various failure codes otherwise. buf and length wont be updated.
*/
void getStringUTF8IntoBuffer(in nsRegistryKey baseKey, in string path,
inout char buf, inout PRUint32 length);
/**
* Get a a byte array value of attribute valname
*
* @return
* NS_OK on success. buf has the string value copied into it.
* length is updated to actual number of bytes copied into buf.
* NS_ERROR_REG_BUFFER_TOO_SMALL if not enough buffer space.
* length is updated to actual length in PRUint8s including
* terminating NULL and buf will be unchanged.
* NS_ERROR_FAILURE if an unknown error happened. state of buf and
* length undefined.
* various other failure codes otherwise. buf and length wont be updated.
*/
void getBytesUTF8IntoBuffer(in nsRegistryKey baseKey, in string path,
inout PRUint8 buf, inout PRUint32 length);
};
%{ C++ %{ C++
#include "nsIRegistryUtils.h" #include "nsIRegistryUtils.h"
%} %}

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

@ -357,7 +357,7 @@ static void reginfo2Length( const REGINFO &in, PRUint32 &out ) {
| This code generates the implementation of the nsISupports member functions | | This code generates the implementation of the nsISupports member functions |
| for each class implemented in this file. | | for each class implemented in this file. |
------------------------------------------------------------------------------*/ ------------------------------------------------------------------------------*/
NS_IMPL_THREADSAFE_ISUPPORTS1( nsRegistry, nsIRegistry ) NS_IMPL_THREADSAFE_ISUPPORTS2(nsRegistry, nsIRegistry, nsIRegistryGetter)
NS_IMPL_ISUPPORTS2( nsRegSubtreeEnumerator, nsIEnumerator, NS_IMPL_ISUPPORTS2( nsRegSubtreeEnumerator, nsIEnumerator,
nsIRegistryEnumerator) nsIRegistryEnumerator)
NS_IMPL_ISUPPORTS1( nsRegistryNode, nsIRegistryNode ) NS_IMPL_ISUPPORTS1( nsRegistryNode, nsIRegistryNode )
@ -733,7 +733,8 @@ NS_IMETHODIMP nsRegistry::GetStringUTF8( nsRegistryKey baseKey, const char *path
// Attempt to get string into our fixed buffer // Attempt to get string into our fixed buffer
PR_Lock(mregLock); PR_Lock(mregLock);
err = NR_RegGetEntryString( mReg,(RKEY)baseKey,(char*)path, regStr, sizeof regStr ); err = NR_RegGetEntryString( mReg,(RKEY)baseKey,(char*)path, regStr,
sizeof(regStr) );
PR_Unlock(mregLock); PR_Unlock(mregLock);
if ( err == REGERR_OK ) if ( err == REGERR_OK )
@ -783,6 +784,30 @@ NS_IMETHODIMP nsRegistry::GetStringUTF8( nsRegistryKey baseKey, const char *path
return rv; return rv;
} }
NS_IMETHODIMP
nsRegistry::GetStringUTF8IntoBuffer( nsRegistryKey baseKey, const char *path,
char *buf, PRUint32 *length )
{
REGERR err = REGERR_OK;
// Attempt to get string into our fixed buffer
PR_Lock(mregLock);
err = NR_RegGetEntryString( mReg,(RKEY)baseKey,(char*)path, buf, *length );
PR_Unlock(mregLock);
// Convert status.
nsresult rv = regerr2nsresult( err );
if (rv == NS_ERROR_REG_BUFFER_TOO_SMALL) {
// fill length with the actual length
nsresult rv1 = GetValueLength( baseKey, path, length );
if(NS_FAILED(rv1))
return rv1;
}
return rv;
}
/*--------------------------- nsRegistry::SetString ---------------------------- /*--------------------------- nsRegistry::SetString ----------------------------
| Simply sets the registry contents using NR_RegSetEntryString. | | Simply sets the registry contents using NR_RegSetEntryString. |
------------------------------------------------------------------------------*/ ------------------------------------------------------------------------------*/
@ -884,6 +909,41 @@ NS_IMETHODIMP nsRegistry::GetBytesUTF8( nsRegistryKey baseKey, const char *path,
return rv; return rv;
} }
NS_IMETHODIMP
nsRegistry::GetBytesUTF8IntoBuffer( nsRegistryKey baseKey, const char *path,
PRUint8 *buf, PRUint32* length )
{
REGERR err = REGERR_OK;
// Get info about the requested entry.
PRUint32 type;
nsresult rv = GetValueType( baseKey, path, &type );
// See if that worked.
if(NS_FAILED(rv))
return rv;
// Make sure we are dealing with bytes
if (type != Bytes)
return NS_ERROR_REG_BADTYPE;
// Attempt to get bytes into our fixed buffer
PR_Lock(mregLock);
err = NR_RegGetEntry( mReg,(RKEY)baseKey,NS_CONST_CAST(char*,path),
buf, (unsigned long *)length );
PR_Unlock(mregLock);
rv = regerr2nsresult(rv);
if (rv == NS_ERROR_REG_BUFFER_TOO_SMALL) {
// fill length with the actual length
nsresult rv1 = GetValueLength( baseKey, path, length );
if(NS_FAILED(rv1))
return rv1;
}
return rv;
}
/*---------------------------- nsRegistry::GetInt ------------------------------ /*---------------------------- nsRegistry::GetInt ------------------------------
| This function is just shorthand for fetching a 1-element PRInt32 array. We | | This function is just shorthand for fetching a 1-element PRInt32 array. We |
| implement it "manually" using NR_RegGetEntry | | implement it "manually" using NR_RegGetEntry |

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

@ -43,13 +43,16 @@
#include "nsIRegistry.h" #include "nsIRegistry.h"
#include "NSReg.h" #include "NSReg.h"
struct nsRegistry : public nsIRegistry { struct nsRegistry : public nsIRegistry, nsIRegistryGetter {
// This class implements the nsISupports interface functions. // This class implements the nsISupports interface functions.
NS_DECL_ISUPPORTS NS_DECL_ISUPPORTS
// This class implements the nsIRegistry interface functions. // This class implements the nsIRegistry interface functions.
NS_DECL_NSIREGISTRY NS_DECL_NSIREGISTRY
// Fast registry getters
NS_DECL_NSIREGISTRYGETTER
// ctor/dtor // ctor/dtor
nsRegistry(); nsRegistry();
virtual ~nsRegistry(); virtual ~nsRegistry();