From 8c0b0990a8327429de741b89ea5c4df4344163df Mon Sep 17 00:00:00 2001 From: rdb Date: Tue, 30 Dec 2014 00:06:15 +0100 Subject: [PATCH] Dramatically improve performance when sorting vectors of PointerTo objects --- panda/src/express/pointerTo.h | 14 ++++++++++++++ panda/src/express/pointerToVoid.I | 10 ++++++++++ panda/src/express/pointerToVoid.h | 4 ++++ 3 files changed, 28 insertions(+) diff --git a/panda/src/express/pointerTo.h b/panda/src/express/pointerTo.h index b989b0f944..144bc0eb87 100644 --- a/panda/src/express/pointerTo.h +++ b/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 +void swap(PointerTo &one, PointerTo &two) { + one.swap(two); +} + +template +void swap(ConstPointerTo &one, ConstPointerTo &two) { + one.swap(two); +} + + // Finally, we'll define a couple of handy abbreviations to save on // all that wasted typing time. diff --git a/panda/src/express/pointerToVoid.I b/panda/src/express/pointerToVoid.I index 0834b25f60..108e959323 100644 --- a/panda/src/express/pointerToVoid.I +++ b/panda/src/express/pointerToVoid.I @@ -105,3 +105,13 @@ operator != (const PointerToVoid &other) const { 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); +} diff --git a/panda/src/express/pointerToVoid.h b/panda/src/express/pointerToVoid.h index 7b1a9ff335..2556b1eb8c 100644 --- a/panda/src/express/pointerToVoid.h +++ b/panda/src/express/pointerToVoid.h @@ -20,6 +20,8 @@ #include "memoryBase.h" #include "atomicAdjust.h" +#include + //////////////////////////////////////////////////////////////////// // Class : PointerToVoid // 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 void swap(PointerToVoid &other); + protected: // Within the PointerToVoid class, we only store a void pointer. // This is actually the (To *) pointer that is typecast to (void *)