nokia / restful

A powerful RESTful framework for Go.
BSD 3-Clause "New" or "Revised" License
17 stars 14 forks source link

Memory efficiency #44

Closed Som-Som-CC closed 1 year ago

Som-Som-CC commented 1 year ago

When client sends a request with SendRequest, SendRecv, Post, ... functions, then it receives request data structure as an interface. Somewhere a byte slice is created for sending. When that slice is created, the source structure is not needed any longer. Still, it is kept till the function returns. It cannot be freed, because there are references to the struct memory area. So the GC won't free that.

A possible solution is to check whether the received request interface is a pointer to a pointer. If that is the case, then the data can be freed, actually.

req := f() // Pointer to struct.
client.Post(ctx, target, &req, &resp) // Pointer to pointer passed.

Somewhere in the depths of sending functions req could be nilled.

It is important to see that moving the data from stack to heap may be counterproductive. Function f() in the above example may better rely on escape analysis.

Som-Som-CC commented 1 year ago

51 improves some aspects of this issue.