Moderators: Despellanion, Dr. Best

Oop - if-less programming

Despellanion
Forum God
Joined: March 26th, 2006, 8:41 pm

May 19th, 2018, 11:13 am #1

Lately I've become more and more interested in programming without if-conditionals, switch statements and ternary operators. I can grasp the basic principle of "code as data", but how worthwhile is it for game development and is it a common practice?
Quote
Like
Share

Dr. Best
Forum God
Dr. Best
Forum God
Joined: March 16th, 2005, 8:47 pm

May 20th, 2018, 10:52 am #2

If you write data-parallel code for SIMD architectures, especially for GPUs, you want to avoid all sorts of branching (if, for, while, switch, etc.) that could be divergent across threads. Threads run in groups (on NVIDIA GPUs they are called warps and consist of 32 threads). If one of these threads executes an instruction, all threads in the warp pay the cost for this instruction, whether they execute it or not.

Thus, under these circumstances it is a common practice. However, if you do not have thousands of threads running in parallel to process similar data, the benefits should be much smaller. Conditionals do incur some overhead. If you can avoid them without doing much more work overall, that is often a good idea. But don't go out of your way to avoid conditionals in code that is not performance sensitive.

Also, avoiding conditionals is one of very many things that you should be doing if you write cryptographic code that must not be susceptible to timing attacks.
Quote
Like
Share

Despellanion
Forum God
Joined: March 26th, 2006, 8:41 pm

May 20th, 2018, 12:22 pm #3

Thanks, that puts it into better perspective.
Also, there are some instances in game development where it's more or less impossible to not use conditionals - at least to my knowledge.
Quote
Like
Share

Dr. Best
Forum God
Dr. Best
Forum God
Joined: March 16th, 2005, 8:47 pm

May 24th, 2018, 9:08 am #4

Nah, it's totally possible, just look at branchless Doom.
wrote:The mov-only DOOM renders approximately one frame every 7 hours, so playing this version requires somewhat increased patience.
Quote
Like
Share

Despellanion
Forum God
Joined: March 26th, 2006, 8:41 pm

May 25th, 2018, 10:22 pm #5

Dr. Best wrote:Nah, it's totally possible, just look at branchless Doom.
wrote:The mov-only DOOM renders approximately one frame every 7 hours, so playing this version requires somewhat increased patience.
So there's basically no upsides to it except that it runs super-safe?
Quote
Like
Share

Dr. Best
Forum God
Dr. Best
Forum God
Joined: March 16th, 2005, 8:47 pm

May 26th, 2018, 4:14 pm #6

Despellanion wrote:
Dr. Best wrote:Nah, it's totally possible, just look at branchless Doom.
wrote:The mov-only DOOM renders approximately one frame every 7 hours, so playing this version requires somewhat increased patience.
So there's basically no upsides to it except that it runs super-safe?
And it's funny :) .
Quote
Like
Share