Using a Pointer to a Channel

Is it good to use a pointer to a pipe? For example, I am reading data at the same time and transmitting that data map[string]sting

using a channel and processing that channel internally getSameValues()

.

func getSameValues(results *chan map[string]string) []string {
    var datas = make([]map[string]string, len(*results))
    i := 0
    for values := range *results {
        datas[i] = values
        i++
    }
}

      

The reason I am doing this is because there chan map[string]string

will be about a million data in the map, and it will be more than one map.

So, I think it would be a good approach if I could pass a pointer to a function so that it doesn't copy the data to save some memory resource.

I have not found a good practice for an efficient option . So I doubt my approach.

+3


source to share


2 answers


Channel pointers are generally not recommended.

The size of the channel value is equal to the size of the pointer. The size does not depend on the number of values ​​in the channel.



You are not reducing copying by using a pointer to a channel, because copying a pointer has the same cost as copying a pipe.

+5


source


Go has five categories of values, which are passed by reference, not by value. These are pointers, fragments, maps, channels and interfaces.

Reference value copying and pointer copying should be considered equal in terms of what the CPU is supposed to do (at least in a good approximation).

Therefore, it is almost never useful to use pipe pointers, just as it is rarely useful to use map pointers.



Since your channel contains maps, the channel is a reference type as well as maps, so the whole processor does copy pointers around the heap. In the case of a channel, goroutine synchronization is also performed.

For further reading, open Efficient Path and look for a page for the word "link".

0


source







All Articles