Great video! I really like to see such things, cause like nobody knows this, but it can be useful in different scenarios. There is just one question I couldn't answer myself Let's say we have a string and want to do a null check on it. Would it be better for branch prediction to do the null check like if (c == null) return; Or would it better to write the hot path inside of the if-statement, so... if (c != null) ... I just checked on sharplab and variant 2 seems better, but I would like to get some opinion by an expert :)
That depends on a lots of things but you normal code flow should not jump and since the JIT compiler treats branches as taken by default (unless you have to throw exceptions) the second code would be better.
So. If I'm understanding this correctly then you should aim to have the path that is expected to be taken within the if statement, and then the "fail" path outside? Yikes, that's basically the reverse of what I usually do!
@@LevelUppp Thanks for that. It still has some impact in some areas of my code so at least I know something I can change now. Thanks for making these sorts of videos, this information is difficult to find elsewhere.
How often do you use a throw helper over a simple in-place throw?
Great video! I really like to see such things, cause like nobody knows this, but it can be useful in different scenarios.
There is just one question I couldn't answer myself
Let's say we have a string and want to do a null check on it.
Would it be better for branch prediction to do the null check like
if (c == null) return;
Or would it better to write the hot path inside of the if-statement, so...
if (c != null) ...
I just checked on sharplab and variant 2 seems better, but I would like to get some opinion by an expert :)
That depends on a lots of things but you normal code flow should not jump and since the JIT compiler treats branches as taken by default (unless you have to throw exceptions) the second code would be better.
@@LevelUppp Thanks for the fast response, hope to see more of this series in the future :D
So. If I'm understanding this correctly then you should aim to have the path that is expected to be taken within the if statement, and then the "fail" path outside?
Yikes, that's basically the reverse of what I usually do!
Only when two branch targets are returns. If one branch target is a return it will be assumed not taken.
@@LevelUppp Thanks for that. It still has some impact in some areas of my code so at least I know something I can change now.
Thanks for making these sorts of videos, this information is difficult to find elsewhere.
Hi, can you share the test methods you used? Or are you using a framework? I also want to test the duration of some things, how can I do that?