minor optimization

This commit is contained in:
David Rose 2002-03-07 00:30:32 +00:00
parent 1f278496ea
commit 533f6043ca
3 changed files with 66 additions and 6 deletions

View File

@ -66,3 +66,52 @@ INLINE void CullableObject::
operator = (const CullableObject &copy) {
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;
}

View File

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

View File

@ -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 &copy);
INLINE void operator = (const CullableObject &copy);
@ -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;