Dramatically improve performance when sorting vectors of PointerTo objects

This commit is contained in:
rdb 2014-12-30 00:06:15 +01:00
parent 24b8bd8b96
commit 8c0b0990a8
3 changed files with 28 additions and 0 deletions

View File

@ -163,6 +163,20 @@ PUBLISHED:
}; };
// The existence of these functions makes it possible to sort vectors
// of PointerTo objects without incurring the cost of unnecessary
// reference count changes. The performance difference is dramatic!
template <class T>
void swap(PointerTo<T> &one, PointerTo<T> &two) {
one.swap(two);
}
template <class T>
void swap(ConstPointerTo<T> &one, ConstPointerTo<T> &two) {
one.swap(two);
}
// Finally, we'll define a couple of handy abbreviations to save on // Finally, we'll define a couple of handy abbreviations to save on
// all that wasted typing time. // all that wasted typing time.

View File

@ -105,3 +105,13 @@ operator != (const PointerToVoid &other) const {
return _void_ptr != other._void_ptr; return _void_ptr != other._void_ptr;
} }
////////////////////////////////////////////////////////////////////
// Function: PointerToVoid::swap
// Access: Public
// Description: Swaps the contents of this PointerTo with the other,
// without touching the reference counts.
////////////////////////////////////////////////////////////////////
INLINE void PointerToVoid::
swap(PointerToVoid &other) {
std::swap(_void_ptr, other._void_ptr);
}

View File

@ -20,6 +20,8 @@
#include "memoryBase.h" #include "memoryBase.h"
#include "atomicAdjust.h" #include "atomicAdjust.h"
#include <algorithm>
//////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////
// Class : PointerToVoid // Class : PointerToVoid
// Description : This is the non-template part of the base class for // Description : This is the non-template part of the base class for
@ -52,6 +54,8 @@ public:
INLINE bool operator == (const PointerToVoid &other) const; INLINE bool operator == (const PointerToVoid &other) const;
INLINE bool operator != (const PointerToVoid &other) const; INLINE bool operator != (const PointerToVoid &other) const;
INLINE void swap(PointerToVoid &other);
protected: protected:
// Within the PointerToVoid class, we only store a void pointer. // Within the PointerToVoid class, we only store a void pointer.
// This is actually the (To *) pointer that is typecast to (void *) // This is actually the (To *) pointer that is typecast to (void *)