N
N
nakem2021-12-23 02:10:50
go
nakem, 2021-12-23 02:10:50

Is passing a variable by pointer expensive?

I recently learned that in some cases passing a variable to a function by pointer can be more labor intensive than passing it by value. It's all because of the escape analysis. To be honest, I don’t really understand how it works at all, I just know that the collector needs this analysis to analyze the visibility of pointers or something like that. I learned about this feature from a report here jump to habr
So here it is. If in c\c++ we can just pass a pointer to an arbitrarily huge object and this operation will happen instantly, then can we also do it in go? I mean, perhaps, with adequate structure sizes, this escape analysis works very quickly and you don’t even have to think about it, or maybe it still costs something and it’s better to avoid unnecessary variable transfers.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Mamonov, 2021-12-23
@EvgenyMamonov

I saw the same or similar benchmarks in this article a year or two ago.
It is important to note here that the testing methodology in the article does not take into account a very important point - there the value is passed only once, i.e. The function was called once and that's it.
But it often happens that the data has to be transferred repeatedly.
For example, there is a function that retrieves data from the database, the second function processes this data, while the second function is running, several more different functions are called and all this data is transferred to each of them, and then all this data is transferred to the JSON serializer, etc. d.
Those. the same data is passed many times from function to function.
If you do not use passing by pointer, the data will be copied every time the function is called.
In this case, passing data by pointer will definitely work faster even taking into account the fact that the garbage collector will be involved.
In general, if the structures are small in size and are used only once or twice, the transfer / creation of such a structure by value will work out faster, as it is written in the article.
But if this data needs to be passed to many different functions, then in most cases passing through a pointer will be faster, even taking into account the fact that the garbage collector is resource-intensive.

U
uvelichitel, 2021-12-23
@uvelichitel

In Go, all parameters are passed to functions by copying. That is, the function cannot change the original argument, but works with a copy. If you want to change it in a function, you have two options

  • pass by reference, then the function can change the value at the passed address
  • or return value

func myFunc(arg type) type
...
bigArg = myFunc(bigArg)

In the first case, you increase the load on the collector by reference counting.
In the second case, you do an allocation and two copies, which is also expensive, especially if the argument takes up a lot of memory space.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question