dongzj2015 2017-04-11 11:50
浏览 51
已采纳

golang:为什么不直接在rpc服务器中重用实例中的空闲列表

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
", s, s)

    *s = Student{"Frank", 18} // similar to *req = Request{}
    fmt.Printf("Address: %p Content: %v
", 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.

  • 写回答

1条回答 默认 最新

  • duandi8838 2017-04-11 13:09
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程