imageworks/OpenShadingLanguage — pull request #281 by lgritz (2013-08-13 17:40:05+00:00)
Title: Optimize closure construction idiom

For the very common idiom of:

closure A = weight * bsdf(...);

underneath, this becomes oso assembly sort of like this:

closure tmp "bsdf" ...args...
mul A weight tmp

and the eventual code generated constructed will allocate a 'ClosureComponent' structure for the bsdf, then separately allocate a 'ClosureMul', and hook them up.

This optimization aims to detect this pattern and simplify it to a single weighted closure constructor:

closure A weight "bsdf" ...args...

which will be implemented in a way that does a single allocation that will contain the ClosureMul and ClosureComponent.

This produces a moderate savings (maybe 1-2%) across many scenes.

Along the way, I also found a bug (which had never been reported, maybe never encountered) involving failure to properly initialize arrays of closures (it only initialized the first element), and some other minor refactoring that moves the is_zero and is_one tests around in such a way as to simplify some of these tests.