Browse Source

minor optimization

David Rose 24 years ago
parent
commit
533f6043ca

+ 49 - 0
panda/src/pgraph/cullableObject.I

@@ -66,3 +66,52 @@ INLINE void CullableObject::
 operator = (const CullableObject &copy) {
 operator = (const CullableObject &copy) {
   nassertv(false);
   nassertv(false);
 }
 }
+
+////////////////////////////////////////////////////////////////////
+//     Function: CullableObject::operator new
+//       Access: Public
+//  Description: Allocates the memory for a new CullableObject.  This
+//               is specialized here to provide for fast allocation of
+//               these things (since we may create and destroy
+//               thousands of these each frame).
+////////////////////////////////////////////////////////////////////
+INLINE void *CullableObject::
+operator new(size_t size) {
+  if (_deleted_chain != (CullableObject *)NULL) {
+    CullableObject *obj = _deleted_chain;
+    _deleted_chain = _deleted_chain->_next;
+    return obj;
+  }
+#ifndef NDEBUG
+  _num_ever_allocated++;
+#endif  // NDEBUG
+  return ::operator new(size);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: CullableObject::operator delete
+//       Access: Public
+//  Description: Frees the memory for a deleted CullableObject.  This
+//               is specialized here to provide for fast allocation of
+//               these things (since we may create and destroy
+//               thousands of these each frame).
+////////////////////////////////////////////////////////////////////
+INLINE void CullableObject::
+operator delete(void *ptr) {
+  CullableObject *obj = (CullableObject *)ptr;
+  obj->_next = _deleted_chain;
+  _deleted_chain = obj;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: CullableObject::get_num_ever_allocated
+//       Access: Published, Static
+//  Description: Returns the number of CullableObject pointers ever
+//               simultaneously allocated; these are now either in
+//               active use or have been recycled into the deleted
+//               CullableObject pool to be used again.
+////////////////////////////////////////////////////////////////////
+INLINE int CullableObject::
+get_num_ever_allocated() {
+  return _num_ever_allocated;
+}

+ 2 - 0
panda/src/pgraph/cullableObject.cxx

@@ -19,6 +19,8 @@
 #include "cullableObject.h"
 #include "cullableObject.h"
 
 
 
 
+CullableObject *CullableObject::_deleted_chain = (CullableObject *)NULL;
+int CullableObject::_num_ever_allocated = 0;
 TypeHandle CullableObject::_type_handle;
 TypeHandle CullableObject::_type_handle;
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////

+ 15 - 6
panda/src/pgraph/cullableObject.h

@@ -43,11 +43,6 @@ public:
                         qpGeomNode *geom_node, int i,
                         qpGeomNode *geom_node, int i,
                         CullableObject *next = NULL);
                         CullableObject *next = NULL);
 
 
-  // We will allocate and destroy hundreds or thousands of these a
-  // frame during the normal course of rendering.  As an optimization,
-  // then, we should consider implementing operator new and delete
-  // here to minimize this overhead.  Should be simple.
-
 private:
 private:
   INLINE CullableObject(const CullableObject &copy);
   INLINE CullableObject(const CullableObject &copy);
   INLINE void operator = (const CullableObject &copy);
   INLINE void operator = (const CullableObject &copy);
@@ -55,13 +50,27 @@ private:
 public:
 public:
   ~CullableObject();
   ~CullableObject();
 
 
+  // We will allocate and destroy hundreds or thousands of these a
+  // frame during the normal course of rendering.  As an optimization,
+  // then, we implement operator new and delete here to minimize this
+  // overhead.
+  INLINE void *operator new(size_t size);
+  INLINE void operator delete(void *ptr);
   void output(ostream &out) const;
   void output(ostream &out) const;
-  
+
+PUBLISHED:
+  INLINE static int get_num_ever_allocated();
+
+public:
   PT(Geom) _geom;
   PT(Geom) _geom;
   CPT(RenderState) _state;
   CPT(RenderState) _state;
   CPT(TransformState) _transform;
   CPT(TransformState) _transform;
   CullableObject *_next;
   CullableObject *_next;
 
 
+private:
+  static CullableObject *_deleted_chain;
+  static int _num_ever_allocated;
+
 public:
 public:
   static TypeHandle get_class_type() {
   static TypeHandle get_class_type() {
     return _type_handle;
     return _type_handle;