Search code examples
gomemory-managementrpc

golang: Why not the free list in rpc server reuse instance directly


RPC server in net/rpc package holds two free lists for Request struct and Response struct. Request struct maintains this list via its next field.

// Server represents an RPC Server.
type Server struct {
    // ...
    freeReq    *Request // header node of Request free list
    freeResp   *Response // header node of Response free list
}

type Request struct {
    ServiceMethod string   // format: "Service.Method"
    Seq           uint64   // sequence number chosen by client
    next          *Request // for free list in Server
}

The free list in rpc server seems to be a object pool. When handling rpc request, server calls getRequest to get a request instance from free list. After handling request, server calls freeRequest to put request instance back to free list.

func (server *Server) getRequest() *Request {
    server.reqLock.Lock()
    req := server.freeReq
    if req == nil {
        req = new(Request) // free list is empty
    } else {
        server.freeReq = req.next // free list isn't empty
        *req = Request{} // Why not reuse instance directly?
    }
    server.reqLock.Unlock()
    return req
}

func (server *Server) freeRequest(req *Request) {
    server.reqLock.Lock()
    req.next = server.freeReq
    server.freeReq = req
    server.reqLock.Unlock()
}

I'm confused about the getRequest function. When free list is empty, it creates a new instance as expected. When free list isn't empty, it executes *req = Request{}. I think Request{} also creates a new insance. So what's the point of holding this free list?


In addition, I wrote a demo to show the effect of the *req = Request{} format statement.

type Student struct {
    Name string
    Age int
}

func main() {
    s := &Student{"John", 20}
    fmt.Printf("Address: %p Content: %v\n", s, s)

    *s = Student{"Frank", 18} // similar to *req = Request{}
    fmt.Printf("Address: %p Content: %v\n", s, s)
}

The output is:

Address: 0xc42000a4c0 Content: &{John 20}
Address: 0xc42000a4c0 Content: &{Frank 18}

So statement *req = Request{} doesn't change the address of pointer, but it does change the content.


Solution

  • The idea of the freelist is to reduce the amount of dynamic memory allocations, by reusing object instances that have already been created.

    It works like this:

    When the first request is made the freelist is empty, therefore a new structures will be allocated on the heap. When these are no longer required they are put in the freelist for reuse. If you wouldn't have the freelist then the next request would need to create new Request/Response structures on the heap, which might be costly to do over-and-over again. With the freelist this is avoided, as the next request can simply reuse the already allocated (and parked) objects.

    I guess you are confused about this line:

    *req = Request{}
    

    Opposed to the case where the freelist is empty and a new object is created on the heap with req = new(Request), this does not allocate an object on the heap.

    It instead just resets the already allocated object (which was dequed from the freelist) to it's default state, by copying the default values.

    You could decompose the line into the following:

    r := Request{} // Create a request with default content on stack (not heap!)
    *req = r // Copy all fields from default request to req
    

    Whatever path is taken in getRequest(), it always returns a default initialized request object - with no leftovers from the previous request.