Skip to content

Commit

Permalink
Plug Tests : Test _precomputedHash more robustly
Browse files Browse the repository at this point in the history
Our hash caching is now so aggressive that even if the precomputed hash was ignored, we'd still be pulling the hash out of the cache. So we now clear the cache to be sure that if the precomputed hash was ignored, that would trigger another call to `ComputeNode::hash()`.
  • Loading branch information
johnhaddon committed Nov 2, 2023
1 parent 03a2875 commit 4c1745e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 26 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

0 comments on commit 4c1745e

Please sign in to comment.