Cycles applies a number of shader node optimizations both at compile time and runtime.
By exploiting them it is possible to design complicated “Uber Shader”
style node groups that incur minimal render time overhead for unused features.

Detect Mix RGB, Math and Vector Math nodes that become no-op (without Clamp)
or evaluate to 0 as a result of addition, subtraction, multiplication,
division or dot/cross product with a known constant 0 or 1 input,
and replace with the appropriate input link or constant result.

Eliminate Mix RGB Mix (without Clamp) and Mix Shader nodes when
Factor is known to be 0 or 1 by replacing with the appropriate input value or link.

If Open Shading Language is chosen as the rendering back-end,
node shaders are translated to OSL code and then compiled and executed by the OSL runtime.
In the process it applies its own extensive set of optimizations, both at compile time and runtime.

Open Shading Language can optimize out Script nodes if their outputs are unused or constant,
even if their OSL shaders have side effects like debug tracing and message passing,
which may be confusing. For that reason message passing with setmessage and getmessage
should generally not be used for passing information forward in the graph;
explicitly passing information through sockets should be preferred.