Răsfoiți Sursa

Dramatically improve performance when sorting vectors of PointerTo objects

rdb 11 ani în urmă
părinte
comite
8c0b0990a8

+ 14 - 0
panda/src/express/pointerTo.h

@@ -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.
 
 

+ 10 - 0
panda/src/express/pointerToVoid.I

@@ -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);
+}

+ 4 - 0
panda/src/express/pointerToVoid.h

@@ -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 *)