Evaluating Unneeded Branches on Switch Node

john's Avatar

john

03 Feb, 2020 01:01 AM

Another long-standing issue for the record...

If you feed multiple chains into the ports of a switch node, only one will be selected for work downstream. But all of them will be evaluated nonetheless.

This is an annoyance when dealing with division by zero. If you have a divide node with a variable denominator that can sometimes be a zero this will trigger a division by zero error. The simple-minded way around this is to test that variable: if it equals zero, provide an alternative value, otherwise let the division proceed.

But this doesn't work. If the value is zero, even though the divide node will not be used, it is still evaluated - thereby triggering the very error you were trying to avoid. The only way around this is to test the variable further upstream which may force you to rearrange other nodes. A puzzle for newbies and a frequent annoyance for daily users like me.

Today I realized, though, that evaluating unneeded branches has another, more serious downside: performance. A node I was building was performing much slower than it should have. This node allows user to chose five options from a menu. Inside the node I pull in various shapes and values, use them to construct all five options, and then use a switch node to choose the option to render.

Each option executed almost instantaneously when the option matched the incoming values. But some options slowed dramatically when constructed using incoming values not intended for that option. I hadn't paid much attention to this because those options would never be rendered. But every time the user chose an option ALL the other options were constructed and then tossed aside, wasted work that, in this case, cause a significant performance hit.

Looking back I see that this happens all the time. Any time you use a switch node you are adding wasted calculations. The more branches you feed into a switch node, the more waste. And sometimes options which are efficient when chosen are very inefficient when not chosen.

A number of users have complained about the slowness of NodeBox compared with other languages. I have wondered at this. In theory, NodeBox should be faster than it is. I now wonder if one factor in this is all those wasted calculations from the unchosen branches of every switch node.

Frederick, if you're still there, did you have some reason for evaluating these unneeded branches? Why not evaluate the index port of the switch node before evaluating anything upstream?

Fixing this might instantly make many of our networks run noticeably faster without any other changes needed.

John

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

Recent Discussions

10 Sep, 2024 03:41 AM
09 Sep, 2024 02:15 PM
07 Sep, 2024 05:16 AM
05 Sep, 2024 02:21 AM
04 Sep, 2024 05:01 AM

 

03 Sep, 2024 10:07 AM
02 Sep, 2024 05:56 AM
31 Aug, 2024 11:07 PM
27 Aug, 2024 12:08 AM
26 Aug, 2024 01:02 PM
26 Aug, 2024 07:18 AM