more robust ShapeManager garbage collector

This commit is contained in:
Andrew Meadows 2019-02-08 13:12:10 -08:00
parent 10e5e9e0c3
commit 17ab0cb92e
3 changed files with 13 additions and 3 deletions

View file

@ -57,7 +57,14 @@ bool ShapeManager::releaseShapeByKey(const HashKey& key) {
if (shapeRef->refCount > 0) {
shapeRef->refCount--;
if (shapeRef->refCount == 0) {
_pendingGarbage.push_back(key);
// look for existing entry in _pendingGarbage, starting from the back
for (int32_t i = _pendingGarbage.size() - 1; i > -1; --i) {
if (_pendingGarbage[i] == key.getHash64()) {
// already on the list, don't add it again
return true;
}
}
_pendingGarbage.push_back(key.getHash64());
const int MAX_SHAPE_GARBAGE_CAPACITY = 255;
if (_pendingGarbage.size() > MAX_SHAPE_GARBAGE_CAPACITY) {
collectGarbage();
@ -89,7 +96,7 @@ bool ShapeManager::releaseShape(const btCollisionShape* shape) {
void ShapeManager::collectGarbage() {
int numShapes = _pendingGarbage.size();
for (int i = 0; i < numShapes; ++i) {
HashKey& key = _pendingGarbage[i];
HashKey key(_pendingGarbage[i]);
ShapeReference* shapeRef = _shapeMap.find(key);
if (shapeRef && shapeRef->refCount == 0) {
ShapeFactory::deleteShape(shapeRef->shape);

View file

@ -75,7 +75,7 @@ private:
// btHashMap is required because it supports memory alignment of the btCollisionShapes
btHashMap<HashKey, ShapeReference> _shapeMap;
btAlignedObjectArray<HashKey> _pendingGarbage;
btAlignedObjectArray<uint64_t> _pendingGarbage;
};
#endif // hifi_ShapeManager_h

View file

@ -32,6 +32,9 @@ class HashKey {
public:
static float getNumQuantizedValuesPerMeter();
HashKey() {}
HashKey(uint64_t hash) : _hash(hash) {}
// These two methods are required by btHashMap.
bool equals(const HashKey& other) const { return _hash == other._hash; }
int32_t getHash() const { return (int32_t)((uint32_t)_hash); }