gobwas / ws

Tiny WebSocket library for Go.
MIT License
6.1k stars 373 forks source link

Is it necessary to use sync.Pool to reuse Reader Object in readData? #154

Open Jekinnnnnn opened 2 years ago

Jekinnnnnn commented 2 years ago

Recently,I used go tool trace to test my project which uses ws v1.0.4.The log shows readData function triggers gc easily when creates "Reader".

runtime.mallocgc:1166
runtime.newobject:1177
github.com/gobwas/ws/wsutil.readData:250
github.com/gobwas/ws/wsutil.ReadData:88
github.com/gobwas/ws/wsutil.ReadClientData:97

In my project,I use a goroutine to recieve remote data.

for {
    select {
    case _, ok := <-c.Stop:
        if !ok {
            return
        }
    default:
        message, msgType, err := websocketUtil.ReadClientData(*c.Conn)
        if err != nil {
            break
        }
        c.Received <- model.ClientMessage{Type: msgType, Data: message}
    }
}

Can optimize performance if use sync.Pool to reuse "Reader"? Reader contains many members,how to reset them properly?

TomYang1024 commented 2 years ago

I have the same problem as you image