Use a set instead of a vector. This makes Unregister() take O(log n) rather than O(n) while Register() now takes O(log n) rather than O(1). Since everything registered is eventually unregistered--as far as I can tell--this is a significant net gain. In practice, the speed up is quite noticable.
This commit is contained in:
@@ -3,7 +3,7 @@
|
|||||||
#ifndef RAGE_UTIL_CACHED_OBJECT_H
|
#ifndef RAGE_UTIL_CACHED_OBJECT_H
|
||||||
#define RAGE_UTIL_CACHED_OBJECT_H
|
#define RAGE_UTIL_CACHED_OBJECT_H
|
||||||
|
|
||||||
#include "Foreach.h"
|
#include <set>
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class CachedObjectPointer;
|
class CachedObjectPointer;
|
||||||
@@ -44,7 +44,7 @@ public:
|
|||||||
static void ClearCacheAll()
|
static void ClearCacheAll()
|
||||||
{
|
{
|
||||||
CachedObjectHelpers::Lock();
|
CachedObjectHelpers::Lock();
|
||||||
for( typename vector<ObjectPointer *>::iterator p = m_apObjectPointers.begin(); p != m_apObjectPointers.end(); ++p )
|
for( typename set<ObjectPointer *>::iterator p = m_spObjectPointers.begin(); p != m_spObjectPointers.end(); ++p )
|
||||||
{
|
{
|
||||||
(*p)->m_pCache = NULL;
|
(*p)->m_pCache = NULL;
|
||||||
(*p)->m_bCacheIsSet = false;
|
(*p)->m_bCacheIsSet = false;
|
||||||
@@ -56,7 +56,7 @@ public:
|
|||||||
static void ClearCacheSpecific( const T *pObject )
|
static void ClearCacheSpecific( const T *pObject )
|
||||||
{
|
{
|
||||||
CachedObjectHelpers::Lock();
|
CachedObjectHelpers::Lock();
|
||||||
for( typename vector<ObjectPointer *>::iterator p = m_apObjectPointers.begin(); p != m_apObjectPointers.end(); ++p )
|
for( typename set<ObjectPointer *>::iterator p = m_spObjectPointers.begin(); p != m_spObjectPointers.end(); ++p )
|
||||||
{
|
{
|
||||||
if( (*p)->m_pCache == pObject )
|
if( (*p)->m_pCache == pObject )
|
||||||
{
|
{
|
||||||
@@ -71,7 +71,7 @@ public:
|
|||||||
static void ClearCacheNegative()
|
static void ClearCacheNegative()
|
||||||
{
|
{
|
||||||
CachedObjectHelpers::Lock();
|
CachedObjectHelpers::Lock();
|
||||||
for( typename vector<ObjectPointer *>::iterator p = m_apObjectPointers.begin(); p != m_apObjectPointers.end(); ++p )
|
for( typename set<ObjectPointer *>::iterator p = m_spObjectPointers.begin(); p != m_spObjectPointers.end(); ++p )
|
||||||
{
|
{
|
||||||
if( (*p)->m_pCache == NULL )
|
if( (*p)->m_pCache == NULL )
|
||||||
(*p)->m_bCacheIsSet = false;
|
(*p)->m_bCacheIsSet = false;
|
||||||
@@ -85,14 +85,14 @@ private:
|
|||||||
|
|
||||||
static void Register( ObjectPointer *p )
|
static void Register( ObjectPointer *p )
|
||||||
{
|
{
|
||||||
m_apObjectPointers.push_back( p );
|
m_spObjectPointers.insert( p );
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Unregister( ObjectPointer *p )
|
static void Unregister( ObjectPointer *p )
|
||||||
{
|
{
|
||||||
typename vector<ObjectPointer *>::iterator it = find( m_apObjectPointers.begin(), m_apObjectPointers.end(), p );
|
typename set<ObjectPointer *>::iterator it = m_spObjectPointers.find( p );
|
||||||
ASSERT( it != m_apObjectPointers.end() );
|
ASSERT( it != m_spObjectPointers.end() );
|
||||||
m_apObjectPointers.erase( it );
|
m_spObjectPointers.erase( it );
|
||||||
}
|
}
|
||||||
|
|
||||||
/* This points to the actual T this object is contained in. This is set
|
/* This points to the actual T this object is contained in. This is set
|
||||||
@@ -102,9 +102,9 @@ private:
|
|||||||
* need to clear cache for an object before any CachedObjectPointers have
|
* need to clear cache for an object before any CachedObjectPointers have
|
||||||
* ever been set for it. */
|
* ever been set for it. */
|
||||||
const T *m_pObject;
|
const T *m_pObject;
|
||||||
static vector<ObjectPointer *> m_apObjectPointers;
|
static set<ObjectPointer *> m_spObjectPointers;
|
||||||
};
|
};
|
||||||
template<typename T> vector<CachedObjectPointer<T> *> CachedObject<T>::m_apObjectPointers = vector<CachedObjectPointer<T> *>();
|
template<typename T> set<CachedObjectPointer<T> *> CachedObject<T>::m_spObjectPointers = set<CachedObjectPointer<T> *>();
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class CachedObjectPointer
|
class CachedObjectPointer
|
||||||
|
|||||||
Reference in New Issue
Block a user