0

I am recently trying to improve the code quality of a side-project and I was wondering something about cache...

Let's suppose we have the following code:

if(a || b)

Now, let's imagine a and b are actually very long operations e.g: element.getValue(anIndexContainer[anotherSubItemCauseReasons])

What I would do is of course to make it readable as, for instance:

bool shouldDoSomething = a || b
// the bool will NEVER be used after the if

if(shouldDoSomething)

My question is: does this consume more cache?

My guess is that since the bool is declared right before the if and they are never re-used, the compiling optimizes it as it is in the first case.

Is there any evidence/proof/spec about the behaviour C# compiler would have?

FOR FUTURE READERS:

This link is the example of what the compiler does, credits to @stefano balzarotti for the link :)

Tails128
  • 320
  • 4
  • 15
  • Which cache did you mention here? There is only one point here, that `&&` operator evaluates the right side only when necessary – Pavel Anikhouski Dec 09 '19 at 14:41
  • 1
    Why do you call it "cache"? And why do you bother with a handful of instructions or memory allocations, where your CPU can do millions of those per second? Readability generally matters more than performance, unless you notice a performance problem, which you should've found out if you'd done some research. Also, you probably want parentheses around `e || f`. – CodeCaster Dec 09 '19 at 14:41
  • 5
    [you know that first code is different than second?](https://dotnetfiddle.net/UnK6hS) – Selvin Dec 09 '19 at 14:42
  • The point about the && is 100% right, I used a poor example, sorry! I will edit the example – Tails128 Dec 09 '19 at 14:50
  • https://stackoverflow.com/questions/16711051/execution-order-of-conditions-in-c-sharp-if-statement check this question and answers - probably it can help in improving performance – demo Dec 09 '19 at 14:51
  • @CodeCaster I know it can, but as I wrote below (D Stanley's answer) I would like to extend the knowledge / the approaches to other people as well and I would like to be able to provide more infos in case doubts are arised. – Tails128 Dec 09 '19 at 14:59
  • @demo yeah, i posted a bad example, sorry! – Tails128 Dec 09 '19 at 15:01
  • 1
    Improving readability is more important than adding a few variables which should not really affect performance in this case. – Ahmed Aboumalek Dec 09 '19 at 15:02
  • 1
    If you want play and learn how compiler optimize code, take a look there: https://sharplab.io/#gist:f72b582d30c265ded816eb23e8baf882 – Stefano Balzarotti Dec 09 '19 at 15:14
  • @StefanoBalzarotti that is awesome! :D – Tails128 Dec 09 '19 at 15:17
  • 1
    @Tails128 you can also set result to c# and see optimized c# without take a look to IL optimizations. – Stefano Balzarotti Dec 09 '19 at 15:20

1 Answers1

2

Is there any evidence/proof/spec about the behaviour C# compiler would have?

To my knowledge, the compiler and the JITter (which transforms compiled byte-code into machine code) can do pretty much anything they want to improve performance that does not change the functional effect of the code. There are flags that will keep the compiler from making these types of optimizations, but that's more for debugging purposes by keeping the byte code more aligned with the source code. Once you tell the compiler to optimize, it's out of your control.

So yes, the compiler (or JITter) might detect that these variables can be in-lined and do so, but why do you care? Write the source code in a way that makes logical sense, so others that maintain it (or you after a few months) know what the heck you intended to do. Only if you have measurable performance problems should you try to outsmart the compiler.

This might be applicable in lower-level languages where registers or stack space is taken up, but in high-level managed languages these aren't huge concerns.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • I agree that it is overkill to worry about it, but I would like to extend the knowledge on some works where I am working with other people and in order to do that I would prefer to be sure that I am suggesting the correct thing to do... so that is why I care "^^ – Tails128 Dec 09 '19 at 14:57
  • @Tails128 fair enough - yes the compiler might optimize it, but if it doesn't it shouldn't be a significant problem. – D Stanley Dec 09 '19 at 15:06
  • I would agree with you, since I read clean code I try to always pay care to this kind of stuff... Even because generally speaking the great majority of the exec time is due to complexity, not to single instructions :) (just sometimes it can be hard to convince others) – Tails128 Dec 09 '19 at 15:09
  • @Tails I really don't know what you mean by your last comment. As I said under your question, your CPU can execute a handful of boolean comparisons by the **millions per second**. What you're doing here is not "complex". – CodeCaster Dec 09 '19 at 15:13
  • @CodeCaster I think there's miscommunication, I agreed to that point, I wrote "generally speaking" as in "if the code is slow this is not what you would focus" And the millions per second unfortunately is something I cannot use consistently to enforce this point, depending on the project people may still complain that the code could run on old processors and bla bla bla – Tails128 Dec 09 '19 at 15:17
  • @Tails there have been gigahertz processors for over two decades now. It's also not a statement that's to be taken literally. People who think removing a variable for the sake of speeding up a C# program is a good thing are doing it so wrong that you can just tell them that. If they require proof, create a properly optimized (i.e. a release build) benchmark. – CodeCaster Dec 09 '19 at 15:19
  • @CodeCaster I agree that it is an extremely wrong point to enforce the single-instruction concern, but before telling them that it is wrong, I would still prefer to mediate and to give more points of view so that the justification can sound better than a "it's too small to worry about it" to the other person! – Tails128 Dec 09 '19 at 15:29
  • @Tails128, this most [excellent performance rant](https://ericlippert.com/2012/12/17/performance-rant/) may be what you need to persuade other people that discussing this stuff in abstract is pointless. Make a performance budget, and only start comparing options when the performance budget is exceeded. – HTTP 410 Dec 09 '19 at 15:51