Skip to content

Commit 5d80a2e

Browse files
committed
Merge pull request #121 from ldmoser/recomputationBug
Bug fix: preventing a Computation result to be recomputed everytime
2 parents 9a00041 + 8d3509c commit 5d80a2e

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

include/IECore/ComputationCache.inl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#ifndef IECORE_COMPUTATIONCACHE_INL
3636
#define IECORE_COMPUTATIONCACHE_INL
3737

38+
#include "IECore/MessageHandler.h"
39+
3840
namespace IECore
3941
{
4042

@@ -123,6 +125,13 @@ ConstObjectPtr ComputationCache<T>::get( const T &args, ComputationCache::Missin
123125
if ( obj )
124126
{
125127
obj = m_objectPool->store( obj, ObjectPool::StoreReference );
128+
MurmurHash h = obj->hash();
129+
if ( h != objectHash )
130+
{
131+
/// the computation returned a different object for some reason, so we have to update the hash
132+
m_cache.set( computationHash, h, 1 );
133+
msg( Msg::Warning, "ComputationCache::get", "Inconsistent hash detected." );
134+
}
126135
}
127136
}
128137
}

test/IECore/ComputationCacheTest.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ namespace IECore
5151
struct ComputationCacheTest
5252
{
5353

54+
static int getCount;
5455
typedef int ComputationParams;
5556
typedef ComputationCache< ComputationParams > Cache;
5657

@@ -65,6 +66,7 @@ struct ComputationCacheTest
6566
static IntDataPtr get( const ComputationParams &params )
6667
{
6768
int id = params;
69+
getCount++;
6870
return new IntData( id );
6971
}
7072

@@ -150,6 +152,23 @@ struct ComputationCacheTest
150152
v->writable() = 42;
151153
BOOST_CHECK( *v != *cache.get( ComputationParams(1), Cache::NullIfMissing ) );
152154

155+
// test when the computation function does not match the already registered computation hash....
156+
IntDataPtr weirdValue = new IntData(666);
157+
cache.clear();
158+
cache.set( ComputationParams(1), weirdValue, ObjectPool::StoreReference );
159+
ConstObjectPtr v0 = cache.get( ComputationParams(1) );
160+
BOOST_CHECK( *weirdValue == *cache.get( ComputationParams(1), Cache::NullIfMissing ) );
161+
pool->clear();
162+
int c1 = ComputationCacheTest::getCount;
163+
ConstObjectPtr v1 = cache.get( ComputationParams(1) );
164+
int c2 = ComputationCacheTest::getCount;
165+
BOOST_CHECK_EQUAL( 1, static_cast< const IntData * >(v1.get())->readable() );
166+
BOOST_CHECK_EQUAL( c1 + 1, c2 );
167+
ConstObjectPtr v2 = cache.get( ComputationParams(1) );
168+
int c3 = ComputationCacheTest::getCount;
169+
BOOST_CHECK_EQUAL( 1, static_cast< const IntData * >(v2.get())->readable() );
170+
/// garantee that there was no recomputation.
171+
BOOST_CHECK_EQUAL( c2, c3 );
153172
}
154173

155174
struct GetFromCache
@@ -192,6 +211,7 @@ struct ComputationCacheTest
192211

193212
};
194213

214+
int ComputationCacheTest::getCount(0);
195215

196216
struct ComputationCacheTestSuite : public boost::unit_test::test_suite
197217
{

0 commit comments

Comments
 (0)