Skip to content

Commit cb682d6

Browse files
committed
Merge pull request #359 from johnhaddon/memoryAccumulatorOptimisation
Optimised Object::memoryUsage() David has no objections, and Andrew isn't here today to object, going ahead and pulling this one.
2 parents c2e0b27 + 209a9dd commit cb682d6

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/IECore/Object.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,19 @@ void Object::MemoryAccumulator::accumulate( size_t bytes )
285285

286286
void Object::MemoryAccumulator::accumulate( const Object *object )
287287
{
288-
if( m_accumulated.find( object )==m_accumulated.end() )
288+
if( object->refCount() > 1 )
289289
{
290-
m_accumulated.insert( object );
290+
// object may occur multiple times in the data structure
291+
// being counted - ensure that we don't count it twice.
292+
if( m_accumulated.insert( object ).second )
293+
{
294+
object->memoryUsage( *this );
295+
}
296+
}
297+
else
298+
{
299+
// object can only occur once in the data structure being
300+
// counted - avoid unnecessary bookkeeping overhead.
291301
object->memoryUsage( *this );
292302
}
293303
}

test/IECore/MemoryUsage.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ def testMultipleReferences( self ) :
5353
c["b"] = d
5454
self.assert_( c.memoryUsage() < m + dm )
5555

56+
def testMultipleReferencesToStringData( self ) :
57+
58+
c = CompoundObject()
59+
d = StringData( " " * 10000 )
60+
61+
c["a"] = d
62+
63+
m = c.memoryUsage()
64+
dm = d.memoryUsage()
65+
66+
c["b"] = d
67+
self.assert_( c.memoryUsage() < m + dm )
68+
5669
def testCopiedDataReferences( self ) :
5770

5871
"""Copied data shouldn't use additional memory unless the copies have

0 commit comments

Comments
 (0)