Skip to content

Commit

Permalink
Merge pull request #5522 from johnhaddon/precomputedHashFix
Browse files Browse the repository at this point in the history
ValuePlug : Stop `getValueInternal()` ignoring `precomputedHash`
  • Loading branch information
johnhaddon authored Nov 2, 2023
2 parents ee92804 + bdf288f commit 8a6c1bd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 27 deletions.
13 changes: 5 additions & 8 deletions python/GafferTest/NumericPlugTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,17 +450,14 @@ def testPrecomputedHash( self ) :
self.assertEqual( n.numComputeCalls, 1 )

h = n["sum"].hash()
numHashCalls = n.numHashCalls
# Accept either 1 or 2 - it would be reasonable for the ValuePlug
# to have either cached the hash or not, but that's not what we're
# testing here.
self.assertTrue( numHashCalls == 1 or numHashCalls == 2 )
self.assertEqual( n.numHashCalls, 1 )
self.assertEqual( n.numComputeCalls, 1 )

# What we care about is that calling getValue() with a precomputed hash
# definitely doesn't recompute the hash again.
# Calling `getValue()`` with a precomputed hash shouldn't recompute the
# hash again, even if it has been cleared from the cache.
Gaffer.ValuePlug.clearHashCache()
self.assertEqual( n["sum"].getValue( _precomputedHash = h ), 30 )
self.assertEqual( n.numHashCalls, numHashCalls )
self.assertEqual( n.numHashCalls, 1 )
self.assertEqual( n.numComputeCalls, 1 )

def testIsSetToDefault( self ) :
Expand Down
13 changes: 5 additions & 8 deletions python/GafferTest/TypedPlugTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,17 +208,14 @@ def testPrecomputedHash( self ) :
self.assertEqual( n.numComputeCalls, 1 )

h = n["out"].hash()
numHashCalls = n.numHashCalls
# Accept either 1 or 2 - it would be reasonable for the ValuePlug
# to have either cached the hash or not, but that's not what we're
# testing here.
self.assertTrue( numHashCalls == 1 or numHashCalls == 2 )
self.assertEqual( n.numHashCalls, 1 )
self.assertEqual( n.numComputeCalls, 1 )

# What we care about is that calling getValue() with a precomputed hash
# definitely doesn't recompute the hash again.
# Calling `getValue()` with a precomputed hash shouldn't recompute the
# hash again, even if it has been cleared from the cache.
Gaffer.ValuePlug.clearHashCache()
self.assertEqual( n["out"].getValue( _precomputedHash = h ), "hi" )
self.assertEqual( n.numHashCalls, numHashCalls )
self.assertEqual( n.numHashCalls, 1 )
self.assertEqual( n.numComputeCalls, 1 )

def testBoolPlugStringConnections( self ) :
Expand Down
16 changes: 6 additions & 10 deletions python/GafferTest/ValuePlugTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,22 +286,18 @@ def testPrecomputedHashOptimisation( self ) :
self.assertEqual( a1, IECore.StringData( "a" ) )
self.assertEqual( n.numHashCalls, 1 )

# We apply some leeway in our test for how many hash calls are
# made - a good ValuePlug implementation will probably avoid
# unecessary repeated calls in most cases, but it's not
# what this unit test is about.
a2 = n["out"].getValue( _copy = False )
self.assertTrue( a2.isSame( a1 ) )
self.assertTrue( n.numHashCalls == 1 or n.numHashCalls == 2 )
self.assertEqual( n.numHashCalls, 1 )

h = n["out"].hash()
self.assertTrue( n.numHashCalls >= 1 and n.numHashCalls <= 3 )
numHashCalls = n.numHashCalls
self.assertEqual( n.numHashCalls, 1 )

# What we care about is that calling getValue() with a precomputed hash
# definitely doesn't recompute the hash again.
# Calling `getValue()` with a precomputed hash shouldn't recompute the
# hash again, even if it has been cleared from the cache.
Gaffer.ValuePlug.clearHashCache()
a3 = n["out"].getValue( _copy = False, _precomputedHash = h )
self.assertEqual( n.numHashCalls, numHashCalls )
self.assertEqual( n.numHashCalls, 1 )
self.assertTrue( a3.isSame( a1 ) )

def testSerialisationOfChildValues( self ) :
Expand Down
2 changes: 1 addition & 1 deletion src/Gaffer/ValuePlug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ class ValuePlug::ComputeProcess : public Process
// > `StringPlug::hash()` account for additional processing (such as
// > substitutions) performed in public `getValue()` methods _after_
// > calling `getValueInternal()`.
const IECore::MurmurHash hash = p->ValuePlug::hash();
const IECore::MurmurHash hash = precomputedHash ? *precomputedHash : p->ValuePlug::hash();

if( !Process::forceMonitoring( threadState, plug, staticType ) )
{
Expand Down

0 comments on commit 8a6c1bd

Please sign in to comment.