Skip to content

Commit

Permalink
Merge pull request #1041 from johnhaddon/expressionHash
Browse files Browse the repository at this point in the history
Expression optimisations
  • Loading branch information
andrewkaufman committed Oct 14, 2014
2 parents 1d4d49b + 1b96bb8 commit 185adee
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
9 changes: 8 additions & 1 deletion include/Gaffer/Expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Expression : public ComputeNode
virtual void inPlugs( std::vector<std::string> &plugPaths ) = 0;
/// Must fill names with the names of all context values the expression
/// wishes to read.
virtual void contextNames( std::vector<std::string> &names ) = 0;
virtual void contextNames( std::vector<IECore::InternedString> &names ) = 0;
/// Must execute the expression in the specified context, using the values
/// provided by proxyInputs and setting the result in proxyOutput.
virtual void execute( const Context *context, const std::vector<const ValuePlug *> &proxyInputs, ValuePlug *proxyOutput ) = 0;
Expand All @@ -106,12 +106,19 @@ class Expression : public ComputeNode

private :

CompoundPlug *inPlug();
const CompoundPlug *inPlug() const;

ValuePlug *outPlug();
const ValuePlug *outPlug() const;

void plugSet( Plug *plug );
void parentChanged( GraphComponent *child, GraphComponent *oldParent );

void updatePlugs( const std::string &outPlugPath, std::vector<std::string> &inPlugPaths );

EnginePtr m_engine;
std::vector<IECore::InternedString> m_contextNames;

};

Expand Down
53 changes: 38 additions & 15 deletions src/Gaffer/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@

using namespace Gaffer;

static IECore::InternedString g_inPlugName( "in" );
static IECore::InternedString g_outPlugName( "out" );

//////////////////////////////////////////////////////////////////////////
// Expression implementation
//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -106,8 +109,8 @@ void Expression::affects( const Plug *input, AffectedPlugsContainer &outputs ) c
{
ComputeNode::affects( input, outputs );

const CompoundPlug *in = getChild<CompoundPlug>( "in" );
const ValuePlug *out = getChild<ValuePlug>( "out" );
const CompoundPlug *in = inPlug();
const ValuePlug *out = outPlug();
if( in && out )
{
if( input->parent<CompoundPlug>() == in )
Expand All @@ -127,20 +130,18 @@ void Expression::affects( const Plug *input, AffectedPlugsContainer &outputs ) c
void Expression::hash( const ValuePlug *output, const Context *context, IECore::MurmurHash &h ) const
{
ComputeNode::hash( output, context, h );
if( output == getChild<ValuePlug>( "out" ) )
if( output == outPlug() )
{
enginePlug()->hash( h );
expressionPlug()->hash( h );
const CompoundPlug *in = getChild<CompoundPlug>( "in" );
const CompoundPlug *in = inPlug();
if( in )
{
in->hash( h );
}
if( m_engine )
{
std::vector<std::string> contextNames;
m_engine->contextNames( contextNames );
for( std::vector<std::string>::const_iterator it = contextNames.begin(); it != contextNames.end(); it++ )
for( std::vector<IECore::InternedString>::const_iterator it = m_contextNames.begin(); it != m_contextNames.end(); it++ )
{
const IECore::Data *d = context->get<IECore::Data>( *it, 0 );
if( d )
Expand All @@ -158,11 +159,11 @@ void Expression::hash( const ValuePlug *output, const Context *context, IECore::

void Expression::compute( ValuePlug *output, const Context *context ) const
{
if( output == getChild<ValuePlug>( "out" ) )
if( output == outPlug() )
{
if( m_engine )
{
const CompoundPlug *in = getChild<CompoundPlug>( "in" );
const CompoundPlug *in = inPlug();
std::vector<const ValuePlug *> inputs;
for( ChildContainer::const_iterator it = in->children().begin(); it!=in->children().end(); it++ )
{
Expand All @@ -181,6 +182,26 @@ void Expression::compute( ValuePlug *output, const Context *context ) const
ComputeNode::compute( output, context );
}

CompoundPlug *Expression::inPlug()
{
return getChild<CompoundPlug>( g_inPlugName );
}

const CompoundPlug *Expression::inPlug() const
{
return getChild<CompoundPlug>( g_inPlugName );
}

ValuePlug *Expression::outPlug()
{
return getChild<ValuePlug>( g_outPlugName );
}

const ValuePlug *Expression::outPlug() const
{
return getChild<ValuePlug>( g_outPlugName );
}

void Expression::plugSet( Plug *plug )
{
if( !parent<Node>() )
Expand Down Expand Up @@ -209,6 +230,8 @@ void Expression::plugSet( Plug *plug )
{
m_engine->inPlugs( inPlugPaths );
outPlugPath = m_engine->outPlug();
m_contextNames.clear();
m_engine->contextNames( m_contextNames );
}

updatePlugs( outPlugPath, inPlugPaths );
Expand Down Expand Up @@ -246,12 +269,12 @@ void Expression::updatePlugs( const std::string &dstPlugPath, std::vector<std::s
// if the expression was invalid, remove our plugs
if( !dstPlugPath.size() )
{
Plug *in = getChild<Plug>( "in" );
Plug *in = inPlug();
if( in )
{
removeChild( in );
}
Plug *out = getChild<Plug>( "out" );
Plug *out = outPlug();
if( out )
{
removeChild( out );
Expand All @@ -267,8 +290,8 @@ void Expression::updatePlugs( const std::string &dstPlugPath, std::vector<std::s
throw IECore::Exception( boost::str( boost::format( "Destination plug \"%s\" does not exist" ) % dstPlugPath ) );
}

CompoundPlugPtr inPlugs = new CompoundPlug( "in", Plug::In, Plug::Default | Plug::Dynamic );
setChild( "in", inPlugs );
CompoundPlugPtr inPlugs = new CompoundPlug( g_inPlugName, Plug::In, Plug::Default | Plug::Dynamic );
setChild( g_inPlugName, inPlugs );
for( std::vector<std::string>::const_iterator it = srcPlugPaths.begin(); it!=srcPlugPaths.end(); it++ )
{
ValuePlug *srcPlug = p->descendant<ValuePlug>( *it );
Expand All @@ -281,8 +304,8 @@ void Expression::updatePlugs( const std::string &dstPlugPath, std::vector<std::s
inPlug->setInput( srcPlug );
}

PlugPtr outPlug = dstPlug->createCounterpart( "out", Plug::Out );
setChild( "out", outPlug );
PlugPtr outPlug = dstPlug->createCounterpart( g_outPlugName, Plug::Out );
setChild( g_outPlugName, outPlug );
dstPlug->setInput( outPlug );
}

Expand Down
2 changes: 1 addition & 1 deletion src/GafferBindings/ExpressionBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class EngineWrapper : public Expression::Engine, public IECorePython::Wrapper<Ex
}
}

virtual void contextNames( std::vector<std::string> &names )
virtual void contextNames( std::vector<IECore::InternedString> &names )
{
IECorePython::ScopedGILLock gilLock;
try
Expand Down

0 comments on commit 185adee

Please sign in to comment.