The reason for this question is just my curiosity to determine what could be the best practice on writing a high-performance stream consumer reading large size byte array from multiple channels. (although premature optimization is the root of all evil, this is more of a curiosity). I have read answers about similar senario specific to C
here, but I am requesting answer specific to go, as it is a garbage collected language, and their documentation here says "From a correctness standpoint, you don't need to know where the variable is allocated".
If I have the following code to read from a channel,
for {
select {
case msg := <-stream.Messages():
...snip...
Variable msg
is within the scope of the case statement.
- What happens once it goes out of scope of
case
statement? Since this is declared in the same native function, and the size ofstream
could be a large byte slice, is the variable going to be stored in heap or stack, and if heap, will it be garbage collected, or does stack pointer comes into picture? - Since this is inside an infinite for loop, and the size of
stream
is a large byte slice, is creating the variable and allocating memory every time an overhead,or should I declare the variable ahead, and keeps on over-writing it in every iteration, so that if there is a garbage collection involved, which I am not sure, I could possibly reduce the garbage? - Shouldn't I be bothered about it at all?