From 533f6043ca1ef96ad14540e92bdab4f6d47ebb6e Mon Sep 17 00:00:00 2001 From: David Rose Date: Thu, 7 Mar 2002 00:30:32 +0000 Subject: [PATCH] minor optimization --- panda/src/pgraph/cullableObject.I | 49 +++++++++++++++++++++++++++++ panda/src/pgraph/cullableObject.cxx | 2 ++ panda/src/pgraph/cullableObject.h | 21 +++++++++---- 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/panda/src/pgraph/cullableObject.I b/panda/src/pgraph/cullableObject.I index d22b6fe0f1..e13d4c015a 100644 --- a/panda/src/pgraph/cullableObject.I +++ b/panda/src/pgraph/cullableObject.I @@ -66,3 +66,52 @@ INLINE void CullableObject:: operator = (const CullableObject ©) { 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; +} diff --git a/panda/src/pgraph/cullableObject.cxx b/panda/src/pgraph/cullableObject.cxx index 35fbbf537d..ccb7db990e 100644 --- a/panda/src/pgraph/cullableObject.cxx +++ b/panda/src/pgraph/cullableObject.cxx @@ -19,6 +19,8 @@ #include "cullableObject.h" +CullableObject *CullableObject::_deleted_chain = (CullableObject *)NULL; +int CullableObject::_num_ever_allocated = 0; TypeHandle CullableObject::_type_handle; //////////////////////////////////////////////////////////////////// diff --git a/panda/src/pgraph/cullableObject.h b/panda/src/pgraph/cullableObject.h index ce69e160ea..658ce1e852 100644 --- a/panda/src/pgraph/cullableObject.h +++ b/panda/src/pgraph/cullableObject.h @@ -43,11 +43,6 @@ public: qpGeomNode *geom_node, int i, 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: INLINE CullableObject(const CullableObject ©); INLINE void operator = (const CullableObject ©); @@ -55,13 +50,27 @@ private: public: ~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; - + +PUBLISHED: + INLINE static int get_num_ever_allocated(); + +public: PT(Geom) _geom; CPT(RenderState) _state; CPT(TransformState) _transform; CullableObject *_next; +private: + static CullableObject *_deleted_chain; + static int _num_ever_allocated; + public: static TypeHandle get_class_type() { return _type_handle;